跳转到内容

模块:TowerTable:修订间差异

来自Minimalist Tower Defense Wiki
无编辑摘要
test
第9行: 第9行:
     local footerStr = args.footer or ""
     local footerStr = args.footer or ""


     -- 解析列标题
     -- 解析表头
     local headers = {}
     local headers = {}
     if headerStr ~= "" then
     for h in mw.text.gsplit(headerStr, ",") do
        for h in mw.text.gsplit(headerStr, ",") do
        table.insert(headers, mw.text.trim(h))
            table.insert(headers, mw.text.trim(h))
        end
     end
     end


     -- 解析数据行
     -- 解析数据行
     local rows = {}
     local rows = {}
     if rowsStr ~= "" then
     for row in mw.text.gsplit(rowsStr, ";") do
        for row in mw.text.gsplit(rowsStr, ";") do
        local cells = {}
            local cells = {}
        for cell in mw.text.gsplit(row, ",") do
            for cell in mw.text.gsplit(row, ",") do
            table.insert(cells, mw.text.trim(cell))
                table.insert(cells, mw.text.trim(cell))
        end
            end
        if #cells > 0 then
            if #cells > 0 then
            table.insert(rows, cells)
                table.insert(rows, cells)
            end
         end
         end
     end
     end


     local result = {}
    -- 使用 frame:extensionTag 构建表格
    table.insert(result, '{| class="' .. class .. '"\n')
     local builder = frame:extensionTag("templatestyles", "", {src = "..."}) ..
 
                    mw.html.create("table"):addClass(class):done()
     -- 表头行
     -- 构建表头
     table.insert(result, '| \'\'\'防御塔等级与属性项(总和)\'\'\'')
    local caption = builder:tag("tr")
     caption:tag("td"):wikitext("'''防御塔等级与属性项(总和)'''")
     for _, h in ipairs(headers) do
     for _, h in ipairs(headers) do
         local colored = frame:expandTemplate{ title = 'c', args = { h, h } }
         caption:tag("td"):wikitext(frame:expandTemplate{ title = 'c', args = { h, h } })
        table.insert(result, ' || ' .. colored)
     end
     end
    table.insert(result, '\n')


     -- 数据行
     -- 构建数据行
     for _, rowCells in ipairs(rows) do
     for _, rowCells in ipairs(rows) do
         table.insert(result, '|-\n')
         local tr = builder:tag("tr")
       
        -- 第一列
         local firstColRaw = rowCells[1] or ""
         local firstColRaw = rowCells[1] or ""
         -- 使用 preprocess 让 MediaWiki 解析第一列中的所有模板
         -- 核心修复点:延迟解析,先保留 wikitext,最后一起交给 frame:preprocess
         local firstColParsed = frame:preprocess(firstColRaw)
         tr:tag("td"):wikitext(firstColRaw)
        table.insert(result, '| ' .. firstColParsed .. '\n')
       
        -- 其余列:使用 c 模板上色
         for i = 2, #rowCells do
         for i = 2, #rowCells do
             local header = headers[i-1] or ""
             local header = headers[i-1] or ""
             local cellValue = rowCells[i] or ""
             local cellValue = rowCells[i] or ""
             local colored = frame:expandTemplate{ title = 'c', args = { header, cellValue } }
             tr:tag("td"):wikitext("{{c|" .. header .. "|" .. cellValue .. "}}")
            if i == 2 then
                table.insert(result, '| ' .. colored)
            else
                table.insert(result, ' || ' .. colored)
            end
         end
         end
        table.insert(result, '\n')
     end
     end


     -- 脚注行
     -- 构建脚注
     if footerStr ~= "" then
     if footerStr ~= "" then
         table.insert(result, '|-\n')
         local tr = builder:tag("tr")
         table.insert(result, '| ' .. footerStr .. '\n')
         tr:tag("td"):attr("colspan", #headers + 1):wikitext(footerStr)
     end
     end


     table.insert(result, '|}')
     -- 执行最后解析
     return table.concat(result)
     return frame:preprocess(tostring(builder))
end
end


return p
return p

2026年6月12日 (五) 05:22的版本

见Template:TowerTable。



local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local class = args.class or "wikitable towertable"
    local headerStr = args.header or ""
    local rowsStr = args.rows or ""
    local footerStr = args.footer or ""

    -- 解析表头
    local headers = {}
    for h in mw.text.gsplit(headerStr, ",") do
        table.insert(headers, mw.text.trim(h))
    end

    -- 解析数据行
    local rows = {}
    for row in mw.text.gsplit(rowsStr, ";") do
        local cells = {}
        for cell in mw.text.gsplit(row, ",") do
            table.insert(cells, mw.text.trim(cell))
        end
        if #cells > 0 then
            table.insert(rows, cells)
        end
    end

    -- 使用 frame:extensionTag 构建表格
    local builder = frame:extensionTag("templatestyles", "", {src = "..."}) ..
                    mw.html.create("table"):addClass(class):done()
    -- 构建表头
    local caption = builder:tag("tr")
    caption:tag("td"):wikitext("'''防御塔等级与属性项(总和)'''")
    for _, h in ipairs(headers) do
        caption:tag("td"):wikitext(frame:expandTemplate{ title = 'c', args = { h, h } })
    end

    -- 构建数据行
    for _, rowCells in ipairs(rows) do
        local tr = builder:tag("tr")
        local firstColRaw = rowCells[1] or ""
        -- 核心修复点:延迟解析,先保留 wikitext,最后一起交给 frame:preprocess
        tr:tag("td"):wikitext(firstColRaw)
        for i = 2, #rowCells do
            local header = headers[i-1] or ""
            local cellValue = rowCells[i] or ""
            tr:tag("td"):wikitext("{{c|" .. header .. "|" .. cellValue .. "}}")
        end
    end

    -- 构建脚注
    if footerStr ~= "" then
        local tr = builder:tag("tr")
        tr:tag("td"):attr("colspan", #headers + 1):wikitext(footerStr)
    end

    -- 执行最后解析
    return frame:preprocess(tostring(builder))
end

return p