Module:Infobox: Difference between revisions
Appearance
Reduce padding between fields |
m fixed little naming error |
||
| (14 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local function filter_sections(components) | |||
local out = {} | |||
local buffer = {} | |||
local has_fields = false | |||
for i, comp in ipairs(components) do | |||
if comp[1] == 's' then | |||
if has_fields then | |||
for _, c in ipairs(buffer) do table.insert(out, c) end | |||
end | |||
buffer = { comp } | |||
has_fields = false | |||
elseif comp[1] == 'f' then | |||
if comp[3] and comp[3] ~= '' then | |||
has_fields = true | |||
table.insert(buffer, comp) | |||
end | |||
else | |||
table.insert(out, comp) | |||
end | |||
end | |||
if buffer[1][1] ~= "s" or #buffer > 1 then | |||
for _, c in ipairs(buffer) do table.insert(out, c) end | |||
end | |||
return out | |||
end | |||
function p.infobox(frame) | function p.infobox(frame) | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
args = filter_sections(args) | |||
local out = {} | local out = {} | ||
table.insert(out, | table.insert(out,'{| class="infobox"') | ||
local function add_title(data) | local function add_title(data) | ||
if not data then return end | if not data then return end | ||
table.insert(out, '|-\n! | table.insert(out, '|-\n! class="infobox-title" colspan="2" | ' .. data) | ||
end | end | ||
local function add_section(data) | local function add_section(data) | ||
if not data then return end | if not data then return end | ||
table.insert(out, '|-\n! | table.insert(out, '|-\n! class="infobox-section" colspan="2" | ' .. data) | ||
end | end | ||
local function add_field(label, data) | local function add_field(label, data) | ||
if not data then return end | if not data then return end | ||
local left = ' | local left = 'class="infobox-leftfield" | ' .. label | ||
local right = ' | local right = 'class="infobox-rightfield" | ' .. data | ||
table.insert(out, '|-\n|'..left..'||'..right) | table.insert(out, '|-\n|'..left..'||'..right) | ||
end | end | ||
| Line 37: | Line 56: | ||
local function add_image(data) | local function add_image(data) | ||
if not data then return end | if not data then return end | ||
table.insert(out, '|-\n| | table.insert(out, '|-\n| class="infobox-image" colspan="2" | ' .. data) | ||
end | end | ||
for i, | for i, comp in ipairs(args) do | ||
if | if comp[1] == 't' then add_title(comp[2]) | ||
elseif | elseif comp[1] == 's' then add_section(comp[2]) | ||
elseif | elseif comp[1] == 'f' then add_field(comp[2], comp[3]) | ||
elseif | elseif comp[1] == 'i' then add_image(comp[2]) | ||
end | end | ||
end | end | ||
Latest revision as of 20:35, 23 June 2025
Documentation for this module may be created at Module:Infobox/doc
local p = {}
local function filter_sections(components)
local out = {}
local buffer = {}
local has_fields = false
for i, comp in ipairs(components) do
if comp[1] == 's' then
if has_fields then
for _, c in ipairs(buffer) do table.insert(out, c) end
end
buffer = { comp }
has_fields = false
elseif comp[1] == 'f' then
if comp[3] and comp[3] ~= '' then
has_fields = true
table.insert(buffer, comp)
end
else
table.insert(out, comp)
end
end
if buffer[1][1] ~= "s" or #buffer > 1 then
for _, c in ipairs(buffer) do table.insert(out, c) end
end
return out
end
function p.infobox(frame)
local args = frame:getParent().args
args = filter_sections(args)
local out = {}
table.insert(out,'{| class="infobox"')
local function add_title(data)
if not data then return end
table.insert(out, '|-\n! class="infobox-title" colspan="2" | ' .. data)
end
local function add_section(data)
if not data then return end
table.insert(out, '|-\n! class="infobox-section" colspan="2" | ' .. data)
end
local function add_field(label, data)
if not data then return end
local left = 'class="infobox-leftfield" | ' .. label
local right = 'class="infobox-rightfield" | ' .. data
table.insert(out, '|-\n|'..left..'||'..right)
end
local function add_image(data)
if not data then return end
table.insert(out, '|-\n| class="infobox-image" colspan="2" | ' .. data)
end
for i, comp in ipairs(args) do
if comp[1] == 't' then add_title(comp[2])
elseif comp[1] == 's' then add_section(comp[2])
elseif comp[1] == 'f' then add_field(comp[2], comp[3])
elseif comp[1] == 'i' then add_image(comp[2])
end
end
table.insert(out, '|}')
return table.concat(out, '\n')
end
return p