跳转到内容

模块:Infobox

来自還阔以
Admin留言 | 贡献2025年9月16日 (二) 01:12的版本

此模块的文档可以在模块:Infobox/doc创建

local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local html = mw.html.create('table'):addClass('infobox')

    -- 顶部标题(可选:title + image)
    if args.title then
        local topRow = html:tag('tr'):tag('th'):attr('colspan', '2'):addClass('infobox-title')
        if args.image then
            topRow:tag('img')
                :attr('src', args.image)
                :attr('alt', args.title)
                :css('max-width:60px; vertical-align:middle; margin-right:10px')
                :done()
        end
        topRow:wikitext(args.title):done():done()
    end

    -- 遍历所有参数,动态识别分组
    local currentGroupTitle = nil
    for key, value in pairs(args) do
        -- 匹配分组标题(格式:groupN_title)
        local groupMatch = key:match('^分组(%d+)_标题$')
        if groupMatch then
            currentGroupTitle = value
            -- 渲染分组标题行(合并两列)
            html:tag('tr')
                :tag('th')
                :attr('colspan', '2')
                :addClass('infobox-group-title')
                :wikitext(currentGroupTitle)
                :done()
                :done()
        -- 非分组标题且当前有分组 → 视为组内字段
        elseif currentGroupTitle and value ~= '' and key ~= 'title' and key ~= 'image' then
            -- 字段名美化(首字母大写)
            local fieldName = key:gsub('^%l', string.upper)
            -- 渲染字段行
            html:tag('tr')
                :tag('td'):addClass('infobox-field-label'):wikitext(fieldName):done()
                :tag('td'):addClass('infobox-field-value'):wikitext(value):done()
                :done()
        end
    end

    return html
end

return p