Modulo:Vedi anche/sandbox

Versione del 13 mag 2015 alle 10:19 di Rotpunkt (discussione | contributi) (aggiunta funzione "sezione" per il template "Vedi anche sezione")
--[[
* Modulo che implementa le funzionalità dei template {{Vedi anche}} e {{Vedi anche sezione}}.
]]

require('Modulo:No globals')

local p = {}

-- Parsifica un argomento del template (rappresenta il titolo di una pagina) e ne ritorna il wikilink.
local function parseArg(arg)
	local dest, count
	
	-- rimuove eventuali pipe inserite tramite {{!}}
	arg = arg:match('(.*)|') or arg
	-- sostituisce le HTML entity (per esempio ' generato da {{PAGENAME}} quando il titolo contiene l'apostrofo)
	arg = mw.text.decode(arg)
	-- sostituisce # con §, se trovato crea un piped wikilink
	dest = arg
	arg, count = arg:gsub('#', ' § ')
	dest = count == 1 and (dest .. '|') or ''

	return "'''[[" .. dest .. arg .. "]]'''"
end

-- Costruisce l'HTML per contenere i wikilink alle pagine.
local function buildHTML(wikitext, with_noprint)
	local tableStyle = {
		['margin-bottom'] = '.5em',
		border = '1px solid #CCC',
		['text-align'] = 'left',
		['font-size'] = '95%',
		background = 'transparent'
	}
	local tableNode = mw.html.create('table')
	tableNode
		:addClass(with_noprint and 'noprint' or nil)
		:css(tableStyle)
		:tag('tr')
			:tag('td')
				:css('padding', '0 .5em')
				:wikitext('[[File:Exquisite-kfind.png|20px|class=noviewer]]')
				:done()
			:tag('td')
				:css('width', '100%')
				:wikitext("''Lo stesso argomento in dettaglio: " .. wikitext  .. "''.")
				:done()

	return tostring(tableNode)	
end

-- Entry-point per {{Vedi anche sezione}}
function p.sezione(frame)
	local arg = frame:getParent().args[1]
	local wlink = arg and ("'''[[#" .. arg .. "|" .. arg .. "]]'''") or ''
	return buildHTML('sezione ' .. wlink)
end

-- Entry-point per {{Vedi anche}}
function p.main(frame)
	local args = {}

	for _, val in ipairs(frame:getParent().args) do
		table.insert(args, parseArg(val))
	end

	return buildHTML(mw.text.listToText(args, ', ', ' e '), true)
end

return p