Module:Infobox
Documentation for this module may be created at Module:Infobox/doc
-- 中文的此模块对比英文维基多解析了 overimage、overcaption,
-- overimagerowclass 以及 header/labal/data*style 参数。
local p = {}
local args = {}
local origArgs = {}
local root
local has_rows = false
-- 解析标题函数
local function renderTitle()
if not args.title then return end
has_rows = true
-- 日志调试,确保 titlestyle 被正确传递
mw.log('Title: ', args.title)
mw.log('Title Style: ', args.titlestyle or '(No Style)')
root
:tag('caption') -- 生成 <caption> 标签
:addClass('infobox-title') -- 添加默认类
:addClass(args.titleclass) -- 添加用户定义的类
:cssText(args.titlestyle) -- 添加用户定义的样式
:wikitext(args.title) -- 添加标题文本
end
-- 初始化 infobox 的主结构
local function _infobox()
-- 创建表格
root = mw.html.create('table')
root:addClass('infobox') -- 默认 infobox 样式
-- 渲染标题
renderTitle()
-- 返回最终的 HTML
return tostring(root)
end
-- 解析参数
local function parseDataParameters()
-- 处理单个参数的解析
local function preprocessSingleArg(argName)
if origArgs[argName] and origArgs[argName] ~= '' then
args[argName] = origArgs[argName]
end
end
-- 解析特定参数
preprocessSingleArg('title')
preprocessSingleArg('titlestyle')
preprocessSingleArg('titleclass')
end
-- 主调用函数
function p.infobox(frame)
-- 获取模板传入的参数
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
else
origArgs = frame
end
-- 解析参数
parseDataParameters()
-- 渲染 infobox
return _infobox()
end
return p