« Module:Référence » : différence entre les versions

De Les Archives de Vault-Tec
Kharmitch (discussion | contributions)
Aucun résumé des modifications
Kharmitch (discussion | contributions)
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
local p = {}
local p = {}


--------------------------------------
-- Adapte le style de la citation en fonction de la langue
-----    Fonctions communes    -----
function sentence_lang(sentence, lang)
--------------------------------------
if lang ~= "fr" then
 
return string.format("''%s''", sentence)
function formatSentence(sentence, lang)
if lang ~= 'fr' then
return "''" .. sentence .. "''"
else
else
return sentence
return sentence
Ligne 13 : Ligne 10 :
end
end


function getArgs(frame)
-- Retourne les arguments entrés par l'utilisateur
function get_args(frame)
local args = {}
local args = {}


Ligne 26 : Ligne 24 :
end
end


--------------------------------------
-- Génère une référence mise en forme automatiquement en
-- fonction du type sélectionné par l'utilisateur.
function p.dialogue(frame)
local args = get_args(frame)
-- Paramètres d'identification et de regroupement
-- de la référence
local attributes = {
name  = args["nom"],
group = args["groupe"],
}
local lang = args["langue"] or "fr"


function p.buildOneReference(frame)
local source = args["source"]
local args = getArgs(frame)
if not source then
error("Paramètre source absent")
end
local type = args['type']
local game = args["jeu"]
if not type then
if game then
return 'Le paramètre type n\'est pas renseigné.'
game = require("Module:Abréviation").name(game)
if not game or game == "" then
error("Valeur inconnue du paramètre jeu")
end
else
error("Paramètre jeu absent")
end
end
local type = type:lower()
local attributes = {}
local characters = {}
 
local character
local name = args['nom']
for i = 1, 6 do
if name then
character = args['personnage' .. i]
attributes['name'] = name
if not character then
break
end
table.insert(characters, character)
end
end
local group = args['groupe']
if #characters == 0 then
if group then
error("Paramètre personnage absent")
attributes['group'] = group
end
end
local content = ''
local sentences = ""
local lang = args['langue'] or 'fr'
local sentence
 
local character_index
if type == 'conversation' then
local game = args['jeu']
-- Maximum de 10 phrases possibles
if game then
for i = 1, 10 do
game = require( 'Module:Abréviation' ).name(game)
-- phrasei.p indique l'index du personnage
if game == '' then
-- phrasei.p indique le texte
game = 'jeu ?'
character_index = args[string.format("phrase%d.p", i)]
else
sentence = args[string.format("phrase%d.t", i)]
game = "''[[" .. game .. "]]''"
end
if not sentence or not character_index then
else
break
game = 'jeu ?'
end
end
 
character_index = tonumber(character_index)
local character1 = args['personnage1'] or 'personnage1 ?'
if not character_index then
local character2 = args['personnage2'] or 'personnage2 ?'
error(string.format("Paramètre phrase%d.p doit être un nombre", i))
 
content = content .. 'Conversation entre ' .. character1 .. ' et ' .. character2 .. ' :'
content = content .. '<br/>'
for i = 1, 6 do
local sentence = args['phrase' .. i]
if sentence then
content = content .. '— ' .. formatSentence(sentence, lang) .. '<br/>'
else
break
end
end
end
character = characters[character_index]
local file = args['fichier'] or 'fichier ?'
if not character then
 
error("Personnage non reconnu")
content = content .. 'Extrait du fichier ' .. file .. ', ' .. game
elseif type == 'document' then
local quotation = args['citation']
if quotation then
content = content .. '«&nbsp;' .. formatSentence(quotation, lang) .. '&nbsp;»<br/>'
end
end
 
local document = args['ouvrage'] or 'ouvrage ?'
sentences = sentences .. tostring(mw.html.create("li")
local author = args['auteur'] or 'auteur ?'
:addClass("reference-dial-row")
local date = args['date'] or 'date ?'
:tag("span")
local publisher = args['éditeur'] or 'éditeur ?'
:addClass("reference-dial-character")
local page = args['page'] or 'page ?'
:wikitext(character .. " —&nbsp;")
:done()
:tag("span")
:addClass("reference-dial-text")
:wikitext(sentence_lang(sentence, lang))
:done()
:allDone()
)
end
content = content .. author .. '. \'\'' .. document .. '\'\'. ' .. publisher .. ', ' .. date .. '. pp. ' .. page
if sentences == "" then
error("Paramètre phrase absent")
end
end
 
return frame:extensionTag( 'ref', content, attributes )
sentences = '<ul class="reference-dial">' .. sentences .. "</ul>"
local header = string.format(
"<p>Conversation extraite du fichier %s, ''[[%s]]''.</p>",
source,
game
)
local content = header .. sentences
return tostring(mw.getCurrentFrame():extensionTag( 'ref', content, attributes ))
end
end


return p
return p

Version du 5 février 2023 à 15:27

La documentation pour ce module peut être créée à Module:Référence/doc

local p = {}

-- Adapte le style de la citation en fonction de la langue
function sentence_lang(sentence, lang)
	if lang ~= "fr" then
		return string.format("''%s''", sentence)
	else
		return sentence
	end
end

-- Retourne les arguments entrés par l'utilisateur
function get_args(frame)
	local args = {}

	local argsParent = frame:getParent().args
	for cle, val in pairs(argsParent) do
		if val then
			args[cle] = mw.text.trim(val)
		end
	end
	
	return args
end

-- Génère une référence mise en forme automatiquement en
-- fonction du type sélectionné par l'utilisateur.
function p.dialogue(frame)
	local args = get_args(frame)
	
	-- Paramètres d'identification et de regroupement
	-- de la référence
	local attributes = {
		name  = args["nom"],
		group = args["groupe"],
	}
	
	local lang = args["langue"] or "fr"

	local source = args["source"]
	if not source then
		error("Paramètre source absent")
	end
	
	local game = args["jeu"]
	if game then
		game = require("Module:Abréviation").name(game)
		if not game or game == "" then
			error("Valeur inconnue du paramètre jeu")
		end
	else
		error("Paramètre jeu absent")
	end
	
	local characters = {}
	local character
	for i = 1, 6 do
		character = args['personnage' .. i]
		if not character then
			break
		end
		table.insert(characters, character)
	end
	
	if #characters == 0 then
		error("Paramètre personnage absent")
	end
	
	local sentences = ""
	local sentence
	local character_index
	
	-- Maximum de 10 phrases possibles
	for i = 1, 10 do
		-- phrasei.p indique l'index du personnage
		-- phrasei.p indique le texte
		character_index = args[string.format("phrase%d.p", i)]
		sentence = args[string.format("phrase%d.t", i)]
		
		if not sentence or not character_index then
			break
		end
		character_index = tonumber(character_index)
		if not character_index then
			error(string.format("Paramètre phrase%d.p doit être un nombre", i))
		end
		character = characters[character_index]
		if not character then
			error("Personnage non reconnu")
		end
	
		sentences = sentences .. tostring(mw.html.create("li")
			:addClass("reference-dial-row")
			:tag("span")
				:addClass("reference-dial-character")
				:wikitext(character .. " —&nbsp;")
				:done()
			:tag("span")
				:addClass("reference-dial-text")
				:wikitext(sentence_lang(sentence, lang))
				:done()
			:allDone()
		)
	end
		
	if sentences == "" then
		error("Paramètre phrase absent")
	end
	
	sentences = '<ul class="reference-dial">' .. sentences .. "</ul>"
	local header = string.format(
		"<p>Conversation extraite du fichier %s, ''[[%s]]''.</p>",
		source,
		game
	)
	local content = header .. sentences
	
	return tostring(mw.getCurrentFrame():extensionTag( 'ref', content, attributes ))
end

return p