七日内新公告:全站维护公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:Infomation

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
local p = {}

-- 辅助函数:获取参数值,支持多个可能的参数名
local function getParam(args, possibleNames)
    for _, name in ipairs(possibleNames) do
        if args[name] and args[name] ~= '' then
            return args[name]
        end
    end
    return nil
end

-- 生成表格HTML
local function generateTable(args)
    local output = {}
    
    -- 开始表格
    table.insert(output, '<table cellpadding="3" class="toccolours" width="100%">')
    
    -- 说明字段
    local description = getParam(args, {'A', 'Description', 'description', '说明', '説明'})
    if description then
        table.insert(output, '<tr style="vertical-align: top">')
        table.insert(output, '<th id="fileinfotpl_desc" class="fileinfo-paramfield" width="15%">说明</th>')
        table.insert(output, '<td>' .. description .. '</td>')
        table.insert(output, '</tr>')
    else
        table.insert(output, '<tr style="vertical-align: top">')
        table.insert(output, '<th id="fileinfotpl_desc" class="fileinfo-paramfield" width="15%">说明</th>')
        table.insert(output, '<td>{{Description missing}}</td>')
        table.insert(output, '</tr>')
    end
    
    -- 来源字段
    local source = getParam(args, {'B', 'Source', 'source', '来源', '來源'})
    if source then
        table.insert(output, '<tr style="vertical-align: top">')
        table.insert(output, '<th id="fileinfotpl_src" class="fileinfo-paramfield">来源</th>')
        table.insert(output, '<td>' .. source .. '</td>')
        table.insert(output, '</tr>')
    else
        table.insert(output, '<tr style="vertical-align: top">')
        table.insert(output, '<th id="fileinfotpl_src" class="fileinfo-paramfield">来源</th>')
        table.insert(output, '<td>{{Source missing}}</td>')
        table.insert(output, '</tr>')
    end
    
    -- 日期字段
    local date = getParam(args, {'C', 'Date', 'date', '日期'})
    table.insert(output, '<tr style="vertical-align: top">')
    table.insert(output, '<th id="fileinfotpl_date" class="fileinfo-paramfield">日期</th>')
    table.insert(output, '<td>{{ISOdate|' .. (date or '') .. '}}</td>')
    table.insert(output, '</tr>')
    
    -- 作者字段
    local author = getParam(args, {'D', 'Author', 'author', '作者'})
    if author then
        table.insert(output, '<tr style="vertical-align: top">')
        table.insert(output, '<th id="fileinfotpl_aut" class="fileinfo-paramfield">作者</th>')
        table.insert(output, '<td>' .. author .. '</td>')
        table.insert(output, '</tr>')
    else
        table.insert(output, '<tr style="vertical-align: top">')
        table.insert(output, '<th id="fileinfotpl_aut" class="fileinfo-paramfield">作者</th>')
        table.insert(output, '<td>{{Author missing}}</td>')
        table.insert(output, '</tr>')
    end
    
    -- 许可字段(条件显示)
    local permission = getParam(args, {'E', 'Permission', 'permission', '许可', '許可'})
    if permission and permission ~= '' and permission ~= '-' and permission ~= 'none' then
        table.insert(output, '<tr style="vertical-align: top">')
        table.insert(output, '<th id="fileinfotpl_perm" class="fileinfo-paramfield">许可</th>')
        table.insert(output, '<td>' .. permission .. '</td>')
        table.insert(output, '</tr>')
    end
    
    -- 其他版本字段(条件显示)
    local otherVersions = getParam(args, {'F', 'other_versions', 'Other_Versions', 'Other Versions', 'other versions', '其他版本'})
    if otherVersions and otherVersions ~= '' and otherVersions ~= '-' and otherVersions ~= 'none' then
        table.insert(output, '<tr style="vertical-align: top">')
        table.insert(output, '<th id="fileinfotpl_ver" class="fileinfo-paramfield">其他版本</th>')
        table.insert(output, '<td>' .. otherVersions .. '</td>')
        table.insert(output, '</tr>')
    end
    
    -- 其他字段
    local otherFields = getParam(args, {'other_fields', 'Other_fields', 'Other fields', 'other fields', '其他领域', '其他領域'})
    if otherFields then
        table.insert(output, otherFields)
    end
    
    -- 处理动态的其他字段(other_fields1, other_fields2, ...)
    local i = 1
    while true do
        local fieldName = 'other_fields' .. i
        local fieldValue = getParam(args, {fieldName, 'Other_fields' .. i, 'Other fields' .. i, 'other fields' .. i, '其他领域' .. i, '其他領域' .. i})
        
        if not fieldValue then
            break
        end
        
        table.insert(output, fieldValue)
        i = i + 1
    end
    
    -- 结束表格
    table.insert(output, '</table>')
    
    return table.concat(output, '\n')
end

-- 主函数
function p.main(frame)
    local args
    if frame == mw.getCurrentFrame() then
        args = frame:getParent().args
    else
        args = frame
    end
    
    return generateTable(args)
end

return p