Module:infobox: Difference between revisions

From Laenkea
Jump to navigation Jump to search
(Created page with "local export = {} function export.make_infobox(data) local ret = '{| class="infobox"' if data.title then ret = ret .. '\n|-' ret = ret .. '\n! style="font-size:125%;font-weight:bold;text-align:center;" colspan=2 | ' .. data.title end if data.sections then for _, section in ipairs(data.sections) do if section.items then local num_items = #section.items for i, item in ipairs(section.items) do local row_class = "" if i == 1 then row_cla...")
 
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 5: Line 5:
if data.title then
if data.title then
ret = ret .. '\n|-'
ret = ret .. '\n|-'
ret = ret .. '\n! style="font-size:125%;font-weight:bold;text-align:center;" colspan=2 | ' .. data.title
ret = ret .. '\n! class="infobox-title" colspan=2 | ' .. data.title
end
if data.image then
ret = ret .. '\n|-'
ret = ret .. '\n| class="infobox-image" colspan=2 | ' .. data.image
end
end
if data.sections then
if data.sections then
for _, section in ipairs(data.sections) do
for _, section in ipairs(data.sections) do
if section.items then
local num_items = #section
local num_items = #section.items
for i, item in ipairs(section) do
for i, item in ipairs(section.items) do
local row_class = ""
local row_class = ""
if i == 1 then
if i == 1 then
row_class = ' class="infobox-section-toprow"'
row_class = ' class="infobox-section-toprow"'
elseif i == num_items then
elseif i == num_items then
row_class = ' class="infobox-section-bottomrow"'
row_class = ' class="infobox-section-bottomrow"'
end
end
if (not item.label) or (not item.value) then
if (not item.label) or (not item.value) then
error("each item must have a label and value")
error("each item must have a label and value")
end
ret = ret .. '\n|-' .. row_class
ret = ret .. '\n! ' .. item.label .. ' | ' .. item.value
end
end
ret = ret .. '\n|-' .. row_class
ret = ret .. '\n! ' .. item.label
ret = ret .. '\n| ' .. item.value
end
end
end
end
Line 36: Line 39:
data.sections = {}
data.sections = {}
if args["title"] then data.title = args["title"] end
if args["title"] then data.title = args["title"] end
if args["image"] then data.image = args["image"] end
local i = 1
local i = 1
while args[i] do
while args[i] do

Latest revision as of 21:50, 7 August 2023

Documentation for this module may be created at Module:infobox/documentation

local export = {}

function export.make_infobox(data)
	local ret = '{| class="infobox"'
	if data.title then
		ret = ret .. '\n|-'
		ret = ret .. '\n! class="infobox-title" colspan=2 | ' .. data.title
	end
	if data.image then
		ret = ret .. '\n|-'
		ret = ret .. '\n| class="infobox-image" colspan=2 | ' .. data.image
	end
	if data.sections then
		for _, section in ipairs(data.sections) do
			local num_items = #section
			for i, item in ipairs(section) do
				local row_class = ""
				if i == 1 then
					row_class = ' class="infobox-section-toprow"'
				elseif i == num_items then
					row_class = ' class="infobox-section-bottomrow"'
				end
				if (not item.label) or (not item.value) then
					error("each item must have a label and value")
				end
				ret = ret .. '\n|-' .. row_class
				ret = ret .. '\n! ' .. item.label
				ret = ret .. '\n| ' .. item.value
			end
		end
	end
	ret = ret .. '\n|}'
	return ret
end

function export.show(frame)
	local args = frame:getParent().args
	local data = {}
	data.sections = {}
	if args["title"] then data.title = args["title"] end
	if args["image"] then data.image = args["image"] end
	local i = 1
	while args[i] do
		local section = {}
		while args[i] and (args[i] ~= "-") do
			if not args[i+1] then error("unexpected end of args") end
			if args[i+1] == "-" then error("unexpected end of section") end
			table.insert(section, {label = args[i], value = args[i+1]})
			i = i + 2
		end
		if #section > 0 then table.insert(data.sections, section) end
		i = i + 1
	end
	return export.make_infobox(data)
end

return export