Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Infobox: Difference between revisions

From Chop Wiki
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 = {}
local t = {}
  for part in mw.text.gsplit(s or "", "%s*,%s*") do
for part in mw.text.gsplit(s or "", "%s*,%s*") do
    if part ~= "" then table.insert(t, part) end
if part ~= "" then table.insert(t, part) end
  end
end
  return t
return t
end
end


function p.render(frame)
function p.render(frame)
  local parent = frame:getParent()
local parent = frame:getParent()
  local args = parent and parent.args or frame.args
local args = parent and parent.args or frame.args
  local title = args.title or mw.title.getCurrentTitle().text
local title = args.title or mw.title.getCurrentTitle().text
  local width = tonumber(args.width) or 300
local width = tonumber(args.width) or 300
  local order = split(args.order or "")
local order = split(args.order or "")
  local reserved = { title=true, image=true, width=true, order=true }
local reserved = { title=true, image=true, width=true, order=true }


  local root = mw.html.create("div"):addClass("infobox")
local root = mw.html.create("div"):addClass("infobox")
  root:tag("div"):addClass("infobox-title"):wikitext(title)
root:tag("div"):addClass("infobox-title"):wikitext(title)


  local img = args.image
local img = args.image
  if img and img ~= "" then
if img and img ~= "" then
    local imgDiv = root:tag("div"):addClass("infobox-image")
    local imgDiv = root:tag("div"):addClass("infobox-image")
    if img:match("^https?://") then
    if img:match("^https?://") then
      imgDiv:tag("img"):attr("src", img):attr("alt", title):css("width", width .. "px")
        imgDiv:tag("img")
    else
            :attr("src", img)
      imgDiv:wikitext(string.format("[[File:%s|%dpx]]", img, width))
            :attr("alt", title)
    end
            :css("width", width .. "px")
  end
    else
        imgDiv:wikitext(string.format("[[File:%s|%dpx]]", img, width))
    end
end


  local tbl = root:tag("table"):addClass("infobox-table")
local tbl = root:tag("table"):addClass("infobox-table")
  local used = {}
local used = {}


  for _, key in ipairs(order) do
for _, key in ipairs(order) do
    local val = args[key]
local val = args[key]
    if val and val ~= "" then
if val and val ~= "" then
      used[key] = true
used[key] = true
      local tr = tbl:tag("tr")
local tr = tbl:tag("tr")
      tr:tag("th"):wikitext(key)
tr:tag("th"):wikitext(key)
      tr:tag("td"):wikitext(val)
tr:tag("td"):wikitext(val)
    end
end
  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
    if not reserved[k] and v and v ~= "" and not used[k] then
      table.insert(keys, k)
    table.insert(keys, k)
    end
    end
   end
   end
   table.sort(keys)
   table.sort(keys)


  for _, k in ipairs(keys) do
for _, k in ipairs(keys) do
    local tr = tbl:tag("tr")
    local tr = tbl:tag("tr")
    tr:tag("th"):wikitext(k)
    tr:tag("th"):wikitext(k)
    tr:tag("td"):wikitext(args[k])
    tr:tag("td"):wikitext(args[k])
  end
end


  return tostring(root)
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