Modulo:Avviso archivio

Da Wikiversità, l'apprendimento libero.

Modulo a supporto del template {{Avviso archivio}}.


--[[
* Modulo a supporto del template Avviso archivio.
]]--

require('Modulo:No globals')

local getArgs = require('Modulo:Arguments').getArgs
local p = {}

local function getWlink(title, args, prepend, append)
	return (title.exists or args.noredlinks ~= 'y') and
		   string.format('[[%s|%s %s %s]]', title.prefixedText, prepend, title.subpageText, append) or nil
end

-- Genera i link di navigazione tra le pagine di archiviazione
function p.navlinks(frame)
	local tableStyle = {
		margin = '0 auto 0.5em',
		width = '32em',
		['text-align'] = 'center'
	}
	local args = getArgs(frame, { parentOnly = true })
	local title =  mw.title.getCurrentTitle()
	local prefix, n, ret

	prefix, n = title.prefixedText:match('^(.-)(%d+)$')
	if prefix and n then
		local pad0 = (tonumber(n) < 10 and #n == 2) and '0' or ''
		local prevTitle = mw.title.new(prefix  .. pad0 .. (n - 1))
		local nextTitle = mw.title.new(prefix  .. pad0 .. (n + 1))
		if prevTitle.exists or nextTitle.exists then
			local tableNode = mw.html.create('table'):css(tableStyle)
			tableNode
				:tag('tr')
					:tag('td'):wikitext(tonumber(n) > 1 and getWlink(prevTitle, args, '&larr;', '') or nil)
					:tag('td'):wikitext(string.format("'''%s'''", title.subpageText))
					:tag('td'):wikitext(getWlink(nextTitle, args, '', '&rarr;') or nil)
			ret = tostring(tableNode)
		end
	end

	return ret
end

return p