模块

模块:词典表格

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献2025年10月9日 (四) 14:05的版本

此模块的文档可以在模块:词典表格/doc创建

local p = {}
local data = mw.loadData("模块:词典/data")

function p.makeTable(frame)
    local result = {}
    
    -- 表格开始
    table.insert(result, '<table class="wikitable sortable" style="width:100%" id="CardSelectTr">')
    
    -- 表头
    table.insert(result, '<thead>')
    table.insert(result, '<tr id="CardSelectTabHeader">')
    table.insert(result, '<th style="width:80px" class="dataHeader">图标</th>')
    table.insert(result, '<th style="width:150px" class="dataHeader headerSort">名称</th>')
    table.insert(result, '<th style="width:150px" class="dataHeader headerSort">类型</th>')
    table.insert(result, '<th class="dataHeader">描述</th>')
    table.insert(result, '</tr>')
    table.insert(result, '</thead>')
    
    -- 表格主体
    table.insert(result, '<tbody>')
    
    -- 先收集所有数据并排序
    local allEntries = {}
    for name, entries in pairs(data.dictionary) do
        for _, entry in ipairs(entries) do
            table.insert(allEntries, {
                name = name,
                entry = entry
            })
        end
    end
    
    -- 按类型排序
    table.sort(allEntries, function(a, b)
        local typeA = a.entry['类型'] or ''
        local typeB = b.entry['类型'] or ''
        if typeA == typeB then
            return a.name < b.name
        end
        return typeA < typeB
    end)
    
    -- 遍历排序后的数据
    for _, item in ipairs(allEntries) do
        local name = item.name
        local entry = item.entry
        local dataType = entry['类型'] or ''
        local dataColor = entry['颜色'] or ''
        
        table.insert(result, string.format('<tr data-param类型="%s" data-param颜色="%s">', 
            mw.text.encode(dataType), mw.text.encode(dataColor)))
        
        -- 图标列
        if entry['icon'] and entry['icon'] ~= '' then
            table.insert(result, '<td>[[File:' .. entry['icon'] .. '|50px]]</td>')
        else
            table.insert(result, '<td>暂无图标</td>')
        end
        
        -- 名称列
        table.insert(result, '<td>' .. mw.text.encode(name) .. '</td>')
        
        -- 类型列(添加颜色)
        local typeText = entry['类型'] or ''
        local color = entry['颜色'] or ''
        local colorStyle = ''
        
        if color == '橙' then
            colorStyle = ' style="color:#ff8c00"'
        elseif color == '蓝' then
            colorStyle = ' style="color:#4169E1"'
        elseif color == '红' then
            colorStyle = ' style="color:#DC143C"'
        end
        
        table.insert(result, '<td' .. colorStyle .. '>' .. mw.text.encode(typeText) .. '</td>')
        
        -- 描述列
        table.insert(result, '<td>' .. (entry['描述'] or '') .. '</td>')
        
        table.insert(result, '</tr>')
    end
    
    table.insert(result, '</tbody>')
    table.insert(result, '</table>')
    
    return frame:preprocess(table.concat(result, '\n'))
end

return p