Module:Infobox: Difference between revisions
From Piñata Journal
More actions
Admin Jeremy (talk | contribs) No edit summary |
Admin Jeremy (talk | contribs) No edit summary |
||
| Line 94: | Line 94: | ||
local image = trim(args.image) | local image = trim(args.image) | ||
-- | -- reserved params | ||
local reserved = { | local reserved = { | ||
name = true, | name = true, | ||
| Line 101: | Line 101: | ||
} | } | ||
for k, _ in pairs(args) do | for k, _ in pairs(args) do | ||
if k:match("^journal_") then | if type(k) == "string" and k:match("^journal_") then | ||
reserved[k] = true | reserved[k] = true | ||
end | end | ||
end | end | ||
local | -- build table | ||
local tableNode = mw.html.create('table') | |||
tableNode:addClass('infobox') | |||
tableNode:addClass('infobox-generic') | |||
-- header | |||
tableNode:tag('tr') | |||
:tag('th') | |||
:attr('colspan', 2) | |||
:addClass('infobox-header') | |||
:wikitext(name) | |||
-- | -- image | ||
if image then | if image then | ||
tableNode:tag('tr') | |||
:tag('td') | |||
' | :attr('colspan', 2) | ||
:addClass('infobox-image') | |||
:wikitext(string.format('[[File:%s|250px|center|%s]]', image, name)) | |||
end | end | ||
-- | -- journal | ||
local journalHTML = renderJournal(args) | local journalHTML = renderJournal(args) | ||
if journalHTML then | if journalHTML then | ||
tableNode:tag('tr') | |||
' | :tag('td') | ||
:attr('colspan', 2) | |||
:addClass('infobox-section') | |||
:addClass('infobox-journal-cell') | |||
:node(journalHTML) -- ← IMPORTANT | |||
end | end | ||
-- | -- rows | ||
for | for k, v in pairs(args) do | ||
if not reserved[k] then | |||
local val = trim(v) | |||
' | if val then | ||
' | local row = tableNode:tag('tr') | ||
row:tag('td') | |||
:addClass('infobox-section') | |||
:addClass('infobox-label-cell') | |||
:tag('span') | |||
:addClass('infobox-label') | |||
:wikitext(titleCase(k:gsub("_", " "))) | |||
row:tag('td') | |||
:addClass('infobox-section') | |||
:addClass('infobox-value-cell') | |||
:wikitext(val) | |||
end | |||
end | |||
end | end | ||
return tostring(tableNode) | |||
end | |||
return p | return p | ||
Revision as of 12:33, 6 February 2026
Documentation for this module may be created at Module:Infobox/doc
-- Utility: Title Case (capitalize every word)
local function titleCase(s)
if not s then return nil end
local lang = mw.language.getContentLanguage()
s = s:gsub("(%S+)", function(word)
return lang:ucfirst(word)
end)
return s
end
local p = {}
local journalLabels = {
journal_tip = "Trouble in Paradise",
journal_pp = "Pocket Paradise",
journal_vp1 = "Viva Piñata"
}
-- Utility: trim whitespace
local function trim(s)
if not s then return nil end
s = mw.text.trim(s)
if s == "" then return nil end
return s
end
-- Utility: escape HTML
local function esc(s)
return mw.text.nowiki(s)
end
-- Render journal section
local function renderJournal(args)
-- collect journal_* params
local journals = {}
for k, v in pairs(args) do
if type(k) == "string" and k:match("^journal_") then
local val = trim(v)
if val then
table.insert(journals, {
key = k,
label = journalLabels[k] or titleCase(k:gsub("^journal_", ""):gsub("_", " ")),
text = val
})
end
end
end
-- legacy single journal
if #journals == 0 and trim(args.journal) then
return args.journal
end
if #journals == 0 then
return nil
end
-- single journal: no <details>
if #journals == 1 then
return journals[1].text
end
-- multiple journals: details blocks
local container = mw.html.create('div')
container:addClass('vp-journal-multi')
for i, j in ipairs(journals) do
local details = container:tag('details')
details:addClass('vp-journal-block')
if i == 1 then
details:attr('open', 'open')
end
details:tag('summary')
:wikitext(titleCase(j.label))
details:tag('div')
:addClass('vp-journal-text')
:wikitext(j.text)
end
return tostring(container)
end
-- Main renderer
function p.render(frame)
local args = frame:getParent().args
local name = trim(args.name) or mw.title.getCurrentTitle().text
local image = trim(args.image)
-- reserved params
local reserved = {
name = true,
image = true,
journal = true
}
for k, _ in pairs(args) do
if type(k) == "string" and k:match("^journal_") then
reserved[k] = true
end
end
-- build table
local tableNode = mw.html.create('table')
tableNode:addClass('infobox')
tableNode:addClass('infobox-generic')
-- header
tableNode:tag('tr')
:tag('th')
:attr('colspan', 2)
:addClass('infobox-header')
:wikitext(name)
-- image
if image then
tableNode:tag('tr')
:tag('td')
:attr('colspan', 2)
:addClass('infobox-image')
:wikitext(string.format('[[File:%s|250px|center|%s]]', image, name))
end
-- journal
local journalHTML = renderJournal(args)
if journalHTML then
tableNode:tag('tr')
:tag('td')
:attr('colspan', 2)
:addClass('infobox-section')
:addClass('infobox-journal-cell')
:node(journalHTML) -- ← IMPORTANT
end
-- rows
for k, v in pairs(args) do
if not reserved[k] then
local val = trim(v)
if val then
local row = tableNode:tag('tr')
row:tag('td')
:addClass('infobox-section')
:addClass('infobox-label-cell')
:tag('span')
:addClass('infobox-label')
:wikitext(titleCase(k:gsub("_", " ")))
row:tag('td')
:addClass('infobox-section')
:addClass('infobox-value-cell')
:wikitext(val)
end
end
end
return tostring(tableNode)
end
return p