模块:Infobox
外观
此模块的文档可以在模块:Infobox/doc创建
local p = {}
function p.main(frame)
local args = frame:getParent().args
return p._renderInfobox(args)
end
function p._renderInfobox(args)
local infobox = {}
-- 开始 Infobox 表格
table.insert(infobox, '{| class="infobox"')
-- 标题行
if args.title then
table.insert(infobox, '|+ | ' .. args.title)
end
-- 图片行
if args.image then
table.insert(infobox, '|-')
table.insert(infobox, '| colspan="2" | [[' .. args.image .. '|250px]]')
end
-- 处理字段和分组
local groups = {}
local currentGroup = {title = nil, fields = {}}
local hasExplicitGroups = false
-- 按参数顺序处理
for key, value in pairs(args) do
if key ~= "title" and key ~= "image" then
-- 检查是否是分组标题
local groupNum = key:match("^group(%d+)_title$")
if groupNum then
hasExplicitGroups = true
-- 保存前一个分组(如果有字段)
if #currentGroup.fields > 0 or currentGroup.title then
table.insert(groups, currentGroup)
end
-- 开始新分组
currentGroup = {title = value, fields = {}}
else
-- 普通字段,添加到当前分组
table.insert(currentGroup.fields, {name = key, value = value})
end
end
end
-- 添加最后一个分组
if #currentGroup.fields > 0 or currentGroup.title then
table.insert(groups, currentGroup)
end
-- 渲染分组和字段
for _, group in ipairs(groups) do
if group.title then
table.insert(infobox, '|-')
table.insert(infobox, '! colspan="2" | ' .. group.title)
end
for _, field in ipairs(group.fields) do
if field.value and field.value ~= "" then
table.insert(infobox, '|-')
table.insert(infobox, '! ' .. field.name)
table.insert(infobox, '| ' .. field.value)
end
end
end
-- 结束表格
table.insert(infobox, '|}')
return table.concat(infobox, '\n')
end
return p