Module:Infobox: Difference between revisions
From Chop Wiki
More actions
Created page with "local p = {} local function split(s) local t = {} for part in mw.text.gsplit(s or "", "%s*,%s*") do if part ~= "" then table.insert(t, part) end end return t end function p.render(frame) local parent = frame:getParent() local args = parent and parent.args or frame.args local title = args.title or mw.title.getCurrentTitle().text local width = tonumber(args.width) or 300 local order = split(args.order or "") local reserved = { title=true, image=tr..." |
No edit summary |
||
Line 2: | Line 2: | ||
local function split(s) | local function split(s) | ||
local t = {} | |||
for part in mw.text.gsplit(s or "", "%s*,%s*") do | |||
if part ~= "" then table.insert(t, part) end | |||
end | |||
return t | |||
end | end | ||
function p.render(frame) | function p.render(frame) | ||
local parent = frame:getParent() | |||
local args = parent and parent.args or frame.args | |||
local title = args.title or mw.title.getCurrentTitle().text | |||
local width = tonumber(args.width) or 300 | |||
local order = split(args.order or "") | |||
local reserved = { title=true, image=true, width=true, order=true } | |||
local root = mw.html.create("div"):addClass("infobox") | |||
root:tag("div"):addClass("infobox-title"):wikitext(title) | |||
local img = args.image | |||
if img and img ~= "" then | |||
local imgDiv = root:tag("div"):addClass("infobox-image") | |||
if img:match("^https?://") then | |||
imgDiv:tag("img") | |||
:attr("src", img) | |||
:attr("alt", title) | |||
:css("width", width .. "px") | |||
else | |||
imgDiv:wikitext(string.format("[[File:%s|%dpx]]", img, width)) | |||
end | |||
end | |||
local tbl = root:tag("table"):addClass("infobox-table") | |||
local used = {} | |||
for _, key in ipairs(order) do | |||
local val = args[key] | |||
if val and val ~= "" then | |||
used[key] = true | |||
local tr = tbl:tag("tr") | |||
tr:tag("th"):wikitext(key) | |||
tr:tag("td"):wikitext(val) | |||
end | |||
end | |||
local keys = {} | local keys = {} | ||
for k, v in pairs(args) do | for k, v in pairs(args) do | ||
if not reserved[k] and v and v ~= "" and not used[k] then | |||
table.insert(keys, k) | |||
end | |||
end | end | ||
table.sort(keys) | table.sort(keys) | ||
for _, k in ipairs(keys) do | |||
local tr = tbl:tag("tr") | |||
tr:tag("th"):wikitext(k) | |||
tr:tag("td"):wikitext(args[k]) | |||
end | |||
return tostring(root) | |||
end | end | ||
return p | return p |
Latest revision as of 23:17, 9 August 2025
Documentation for this module may be created at Module:Infobox/doc
local p = {}
local function split(s)
local t = {}
for part in mw.text.gsplit(s or "", "%s*,%s*") do
if part ~= "" then table.insert(t, part) end
end
return t
end
function p.render(frame)
local parent = frame:getParent()
local args = parent and parent.args or frame.args
local title = args.title or mw.title.getCurrentTitle().text
local width = tonumber(args.width) or 300
local order = split(args.order or "")
local reserved = { title=true, image=true, width=true, order=true }
local root = mw.html.create("div"):addClass("infobox")
root:tag("div"):addClass("infobox-title"):wikitext(title)
local img = args.image
if img and img ~= "" then
local imgDiv = root:tag("div"):addClass("infobox-image")
if img:match("^https?://") then
imgDiv:tag("img")
:attr("src", img)
:attr("alt", title)
:css("width", width .. "px")
else
imgDiv:wikitext(string.format("[[File:%s|%dpx]]", img, width))
end
end
local tbl = root:tag("table"):addClass("infobox-table")
local used = {}
for _, key in ipairs(order) do
local val = args[key]
if val and val ~= "" then
used[key] = true
local tr = tbl:tag("tr")
tr:tag("th"):wikitext(key)
tr:tag("td"):wikitext(val)
end
end
local keys = {}
for k, v in pairs(args) do
if not reserved[k] and v and v ~= "" and not used[k] then
table.insert(keys, k)
end
end
table.sort(keys)
for _, k in ipairs(keys) do
local tr = tbl:tag("tr")
tr:tag("th"):wikitext(k)
tr:tag("td"):wikitext(args[k])
end
return tostring(root)
end
return p