Modulo:Avviso archivio

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


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

require('strict')

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

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

-- Genera i link di navigazione tra le pagine di archiviazione
function p.navlinks(frame)
	local args = getArgs(frame, { parentOnly = true })
	local title =  mw.title.getCurrentTitle()
	local prefix, n, ret, prevTitle, nextTitle

	if args.prima then
		prevTitle = mw.title.new(tostring(title.basePageTitle) .. '/' .. args.prima)
	end
	if args.dopo then
		nextTitle = mw.title.new(tostring(title.basePageTitle) .. '/' .. args.dopo)
	end

	prefix, n = title.prefixedText:match('^(.-)(%d+)$')
	if prefix and n then
		local pad0 = (tonumber(n) < 10 and #n == 2) and '0' or ''
		if not args.prima then
			prevTitle = mw.title.new(prefix  .. pad0 .. (n - 1))
		end
		if not args.dopo then
			nextTitle = mw.title.new(prefix  .. pad0 .. (n + 1))
		end
	end

	if (prevTitle and prevTitle.exists) or (nextTitle and nextTitle.exists) then
		return '<div style="margin:auto;width:20em">' .. 
			mw.getCurrentFrame():expandTemplate { title='Precedente e successivo', args={
				(args.prima or tonumber(n) > 1) and getWlink(prevTitle, args) or nil,
				getWlink(nextTitle, args) or nil
				} } ..'</div>'
	end
	return ''
end

return p