Modulo:Avviso archivio
Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Avviso archivio/man (modifica · cronologia)
Sandbox: Modulo:Avviso archivio/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Avviso archivio/test (modifica · cronologia · esegui)
Modulo a supporto del template {{Avviso archivio}}.
--[[
* Modulo a supporto del template Avviso archivio.
]]--
require('strict')
local getArgs = require('Modulo:Arguments').getArgs
-- =============================================================================
-- classe TitleBuilder
-- =============================================================================
local TitleBuilder = {
currTitle = mw.title.getCurrentTitle(),
existingBasePageTitle = nil,
splitTitle = nil
}
function TitleBuilder:new()
local self = {}
local lazyFields = {
existingBasePageTitle = TitleBuilder.getExistingBasePageTitle,
splitTitle = TitleBuilder.getSplitTitle
}
setmetatable(self, {
__index = function (t, key)
if lazyFields[key] then
return lazyFields[key](self)
else
return TitleBuilder[key]
end
end
})
self.number = nil
self.subpageText = nil
return self
end
function TitleBuilder:getExistingBasePageTitle()
local basePageTitle = self.currTitle.basePageTitle
while TitleBuilder.existingBasePageTitle == nil do
if basePageTitle and basePageTitle.exists then
TitleBuilder.existingBasePageTitle = basePageTitle
elseif not basePageTitle or basePageTitle == basePageTitle.basePageTitle then
TitleBuilder.existingBasePageTitle = self.currTitle
else
basePageTitle = basePageTitle.basePageTitle
end
end
return TitleBuilder.existingBasePageTitle
end
function TitleBuilder:getSplitTitle()
if TitleBuilder.splitTitle == nil then
local pre, post = self.currTitle.prefixedText:match('^(.-)(%d+)$')
TitleBuilder.splitTitle = { pre = pre, post = post }
end
return TitleBuilder.splitTitle
end
function TitleBuilder:setSubpageText(subpageText)
self.subpageText = subpageText
return self
end
function TitleBuilder:setNumber(distance)
if self.splitTitle.post then
local currNumber = tonumber(self.splitTitle.post)
if currNumber + distance >= 1 then
self.number = currNumber + distance
end
end
return self
end
function TitleBuilder:build()
local title
if self.subpageText then
title = self.existingBasePageTitle:subPageTitle(self.subpageText)
else
local titleTexts = {}
if self.number and self.splitTitle.pre and self.splitTitle.post then
local newPost = tostring(self.number)
local pad0 = string.rep('0', #self.splitTitle.post - #newPost)
table.insert(titleTexts, self.splitTitle.pre .. pad0 .. newPost)
table.insert(titleTexts, self.splitTitle.pre .. newPost)
end
for _, titleText in ipairs(titleTexts) do
local possibleTitle = mw.title.new(titleText)
if possibleTitle.exists then
title = possibleTitle
break
end
end
end
if title then
local wlLabel
if #title.text > #self.existingBasePageTitle.text + 1 then
wlLabel = title.text:sub(#self.existingBasePageTitle.text + 2)
else
wlLabel = title.subpageText
end
local customTitleFields = {
wikilink = string.format('[[%s|%s]]', title.prefixedText, wlLabel)
}
return setmetatable(customTitleFields, { __index = title })
end
end
-- =============================================================================
-- Funzioni esportate
-- =============================================================================
local p = {}
-- Genera i link di navigazione tra le pagine di archiviazione
function p.navlinks(frame)
local args = getArgs(frame, { parentOnly = true })
local noRedLinks = args.noredlinks == 'y'
local wikilinks = {}
local wikilinkConf = {
{ subpageText = args.prima, distance = -1 },
{ subpageText = args.dopo, distance = 1 }
}
for i, item in ipairs(wikilinkConf) do
local titleBuilder = TitleBuilder:new()
if item.subpageText then
titleBuilder:setSubpageText(item.subpageText)
else
titleBuilder:setNumber(item.distance)
end
local title = titleBuilder:build()
if title and (noRedLinks == false or title.exists) then
wikilinks[i] = title.wikilink
end
end
if next(wikilinks) then
local div = mw.html.create('div')
div
:css('margin', 'auto')
:css('min-width', '20em')
:css('width', 'fit-content')
:wikitext(frame:expandTemplate{
title = 'Precedente e successivo',
args = wikilinks
})
return tostring(div)
end
end
return p