Module:Infobox: Difference between revisions
No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
local origArgs = {} | local origArgs = {} | ||
local root | local root | ||
local has_rows = false | local has_rows = false | ||
-- Helper function: clean up table rows | |||
local function cleanInfobox() | local function cleanInfobox() | ||
root = tostring(root) | root = tostring(root) | ||
Line 61: | Line 13: | ||
end | end | ||
local function addRow( | -- Helper function: add a row to the infobox | ||
if | local function addRow(label, data) | ||
if data and data:match('%S') then | |||
has_rows = true | has_rows = true | ||
local row = root:tag('tr') | local row = root:tag('tr') | ||
if | if label then | ||
row:tag('th') | row:tag('th') | ||
:attr('scope', 'row') | :attr('scope', 'row') | ||
:addClass('infobox-label') | :addClass('infobox-label') | ||
:wikitext( | :wikitext(label) | ||
:done() | :done() | ||
end | end | ||
row:tag('td') | row:tag('td') | ||
:addClass('infobox-data') | :addClass('infobox-data') | ||
:wikitext( | :wikitext(data) | ||
end | end | ||
end | end | ||
-- Helper function: render the title | |||
local function renderTitle() | local function renderTitle() | ||
if args.title then | if args.title then | ||
Line 87: | Line 41: | ||
end | end | ||
-- Helper function: render the image | |||
local function renderImage() | |||
local function | |||
if args.image then | if args.image then | ||
local imageRow = root:tag('tr') | local imageRow = root:tag('tr') | ||
Line 113: | Line 52: | ||
end | end | ||
local function | -- Main rendering logic | ||
local function renderInfobox() | |||
root = mw.html.create('table') | root = mw.html.create('table') | ||
:addClass('infobox') | :addClass('infobox') | ||
:css('width', '350px') | :css('width', '350px') | ||
renderTitle() | renderTitle() | ||
renderImage() | |||
for i = 1, 50 do | |||
local label = args['label' .. i] | |||
local data = args['data' .. i] | |||
if not label and not data then break end | |||
addRow(label, data) | |||
end | |||
cleanInfobox() | cleanInfobox() | ||
end | |||
-- Entry point for the module | |||
function p.infobox(frame) | |||
origArgs = frame:getParent().args | |||
args = {} | |||
-- Preprocess arguments | |||
for k, v in pairs(origArgs) do | |||
args[k] = mw.text.trim(v) | |||
end | |||
renderInfobox() | |||
return tostring(root) | return tostring(root) | ||
end | end | ||
return p | return p |