Modulo:AppuntiNav
Aspetto
La documentazione per questo modulo può essere creata in Modulo:AppuntiNav/man
-- Module:AppuntiNav
local p = {}
local function cleanLink(txt)
if not txt or txt == "" then return "" end
local target = mw.ustring.match(txt, "%[%[([^%]|%]]+)")
return mw.text.trim(target or txt)
end
local function extractLinks(wikitext)
local list = {}
if not wikitext or wikitext == "" then return list end
for target in mw.ustring.gmatch(wikitext, "%[%[([^%]|%]]+)[^%]]*%]%]") do
target = mw.text.trim(target)
if target ~= "" then table.insert(list, target) end
end
return list
end
local function startsWith(str, prefix)
if not str then return false end
prefix = prefix or ""
return prefix == "" or mw.ustring.sub(str, 1, mw.ustring.len(prefix)) == prefix
end
local function getTemplateParamFromPage(pageTitle, tmplName, paramName)
local t = mw.title.new(pageTitle or "")
if not t then return nil end
local content = t:getContent() or ""
if content == "" then return nil end
local pattern = "{{%s*" .. tmplName:gsub("%s","%s+") .. "[^}]*" ..
"[|]%s*" .. paramName .. "%s*=%s*([^\n|}]+)"
local val = mw.ustring.match(content, pattern)
if val then
val = mw.text.trim(val)
val = mw.ustring.gsub(val, "%s*<!%-%-.*%-%->%s*$", "")
end
return val
end
function p.nav(frame)
local args = frame:getParent() and frame:getParent().args or frame.args
local index = mw.text.trim(args.index or "")
local prefix = mw.text.trim(args.prefix or "")
local show = (args.show or "full")
local curMark = args.current_prefix or "• "
local exam = cleanLink(args.exam or "") -- pagina ESAME (non più corso)
local cur = mw.title.getCurrentTitle().fullText
-- Se index assente, prova a leggerlo dall'esame: {{Esame universitario | indice_appunti = ...}}
if index == "" and exam ~= "" then
local inferred = getTemplateParamFromPage(exam, "Esame universitario", "indice_appunti")
if inferred and inferred ~= "" then
index = inferred
end
end
-- Leggi indice
local content = ""
if index ~= "" then
local t = mw.title.new(index)
if t then content = t:getContent() or "" end
end
-- Estrarre tutti i wikilink e filtrare per prefix
local rawList = extractLinks(content)
local list = {}
for _, page in ipairs(rawList) do
if startsWith(page, prefix) then
local tt = mw.title.new(page)
if tt then table.insert(list, tt.fullText) end
end
end
-- Posizione della pagina corrente
local pos
for i, page in ipairs(list) do
if page == cur then pos = i; break end
end
-- Render
local box = mw.html.create('div')
:addClass('appunti-nav')
:css{ border='1px solid #aaa', padding='0.6em', background='#fdfdfd', ['margin-top']='1em' }
local function link(page, label)
return string.format('[[%s|%s]]', page, label)
end
local top = mw.html.create('div')
if pos then
local left = (pos > 1) and link(list[pos-1], '← Pagina precedente') or ''
local right = (pos < #list) and link(list[pos+1], 'Pagina successiva →') or ''
local sep = (left ~= '' and right ~= '') and ' • ' or ''
top:wikitext(left .. sep .. right)
else
if index == "" then
top:wikitext("<span style='color:#666'>(Nessun indice definito)</span>")
else
top:wikitext("<span style='color:#666'>(Questa pagina non è nell’indice)</span>")
end
end
box:node(top)
if show ~= 'compact' and #list > 0 then
local ul = mw.html.create('ul'):css('margin','0.5em 0 0 1.2em')
for i, page in ipairs(list) do
local li = mw.html.create('li')
local label = mw.title.new(page).subpageText or page
if i == pos then
li:tag('strong'):wikitext(curMark .. link(page, label))
else
li:wikitext(link(page, label))
end
ul:node(li)
end
box:node(ul)
end
return tostring(box)
end
return p