跳转到内容

模块:Infobox:修订间差异

来自還阔以
Admin留言 | 贡献
无编辑摘要
Admin留言 | 贡献
无编辑摘要
 
(未显示同一用户的5个中间版本)
第3行: 第3行:
function p.main(frame)
function p.main(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
     local html = mw.html.create('table'):addClass('infobox')
     return p._renderInfobox(args)
end


     -- 顶部标题(可选:title + image)
function p._renderInfobox(args)
    local infobox = {}
   
    -- 开始 Infobox 表格
    table.insert(infobox, '{| class="infobox"')
   
     -- 标题行
     if args.title then
     if args.title then
         local topRow = html:tag('tr'):tag('th'):attr('colspan', '2'):addClass('infobox-title')
         table.insert(infobox, '|+ | ' .. args.title)
         if args.image then
    end
             topRow:tag('img')
   
                 :attr('src', args.image)
    -- 图片行
                 :attr('alt', args.title)
    if args.image then
                 :css('max-width:60px; vertical-align:middle; margin-right:10px')
        table.insert(infobox, '|-')
                 :done()
        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
        topRow:wikitext(args.title):done():done()
     end
     end
 
   
     -- 遍历所有参数,动态识别分组
     -- 添加最后一个分组
     local currentGroupTitle = nil
     if #currentGroup.fields > 0 or currentGroup.title then
    for key, value in pairs(args) do
        table.insert(groups, currentGroup)
        -- 匹配分组标题(格式:groupN_title)
    end
        local groupMatch = key:match('^group(%d+)_title$')
   
         if groupMatch then
    -- 渲染分组和字段
             currentGroupTitle = value
    for _, group in ipairs(groups) do
            -- 渲染分组标题行(合并两列)
         if group.title then
            html:tag('tr')
             table.insert(infobox, '|-')
                :tag('th')
            table.insert(infobox, '! colspan="2" | ' .. group.title)
                :attr('colspan', '2')
        end
                :addClass('infobox-group-title')
       
                :wikitext(currentGroupTitle)
        for _, field in ipairs(group.fields) do
                :done()
            if field.value and field.value ~= "" then
                :done()
                table.insert(infobox, '|-')
        -- 非分组标题且当前有分组 → 视为组内字段
                 table.insert(infobox, '! ' .. field.name)
        elseif currentGroupTitle and value ~= '' and key ~= 'title' and key ~= 'image' then
                 table.insert(infobox, '| ' .. field.value)
            -- 字段名美化(首字母大写)
            end
            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
     end
     end
 
   
     return html
    -- 结束表格
    table.insert(infobox, '|}')
   
     return table.concat(infobox, '\n')
end
end


return p
return p

2025年9月16日 (二) 10:11的最新版本

此模块的文档可以在模块: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