Modulo:Vedi anche: differenze tra le versioni

Da Tematiche di genere.
Vai alla navigazione Vai alla ricerca
m (una versione importata)
Nessun oggetto della modifica
 
Riga 2: Riga 2:
* Modulo che implementa il template {{Vedi anche}}.
* Modulo che implementa il template {{Vedi anche}}.
]]
]]
require('strict')


-- Parsifica un argomento del template (rappresenta il titolo di una pagina) e ne restituisce il wikilink.
-- Parsifica un argomento del template (rappresenta il titolo di una pagina) e ne restituisce il wikilink.

Versione attuale delle 14:21, 23 nov 2022

La documentazione per questo modulo può essere creata in Modulo:Vedi anche/man

--[[
* Modulo che implementa il template {{Vedi anche}}.
]]

-- Parsifica un argomento del template (rappresenta il titolo di una pagina) e ne restituisce il wikilink.
--
-- @param {string} arg
-- @return {string}
local function parseArg(arg)
	local dest, count, obj, content

	-- restituisce errore con wikilink a categorie
	if arg:match('^%s*:%s*[Cc][Aa][Tt]') then
		return '<span class="error">(usare il [[Template:Vedi categoria]])</span>'
	end

	-- rimuove eventuali pipe inserite tramite {{!}}
	arg = arg:match('(.-)|') or arg
	-- sostituisce le HTML entity (per esempio &#39; generato da {{PAGENAME}} quando il titolo contiene l'apostrofo)
	arg = mw.text.decode(arg)
	-- crea oggetto per il titolo
	obj = mw.title.new(mw.uri.decode(arg, 'PATH'))
	-- sostituisce # con §, se trovato crea un piped wikilink
	dest = arg
	arg, count = arg:gsub('#', ' § ')
	dest = count == 1 and (dest .. '|') or ''

	-- converte la prima lettera in maiuscola o minuscola a seconda che la voce contenga il template Titolo minuscolo
	content = obj and obj:getContent()
	if content and content:match('{{%s*[Tt]itolo minuscolo%s*[|}]') then
		arg = mw.getContentLanguage():lcfirst(arg)
	else
		arg = mw.getContentLanguage():ucfirst(arg)
	end

	return string.format("'''[[%s%s]]'''", dest, arg)
end

-- Costruisce l'HTML per contenere i wikilink alle pagine.
--
-- @param {string} wikitext
-- @return {string}
local function buildHTML(wikitext)
	local tableNode = mw.html.create('table')
	tableNode
		:addClass('vedi-anche noprint')
		:tag('tr')
			:tag('td')
				:wikitext('[[File:Magnifying glass icon mgx2.svg|20px|class=noviewer|link=]]')
				:done()
			:tag('td')
				:wikitext(string.format("''Lo stesso argomento in dettaglio: %s''.", wikitext))
				:done()

	return tostring(tableNode)
end

-- =============================================================================
--                            Funzioni esportate
-- =============================================================================

local p = {}

-- Funzione per il template {{Vedi anche}}.
function p.main(frame)
	local lastArg, conjunction
	local styles = 'Modulo:Vedi anche/styles.css'
	local args = {}

	for _, val in ipairs(frame:getParent().args) do
		if mw.text.trim(val) ~= '' then
			table.insert(args, parseArg(val))
			lastArg = val
		end
	end
	if #args > 1 and lastArg then
		conjunction = mw.ustring.lower(lastArg:sub(1, 1)) == 'e' and 'ed' or 'e'
		conjunction = '&#32;' .. conjunction .. '&#32;'
	end

	return frame:extensionTag{
			name = 'templatestyles',
			args = {src = styles}
		} .. buildHTML(mw.text.listToText(args, ',&#32;', conjunction))
end

return p