跳转到内容

模块:TowerTable/sandbox:修订间差异

来自Minimalist Tower Defense Wiki
Jathonbot​(留言 | 贡献)
小 沙盒重写:保留原 preprocess 拼接路径,新增纯文本行与 twtl 缺失回退
Jathonbot​(留言 | 贡献)
小 沙盒:完整改进版(纯文本标签 + 列数校验 + 兜底)
第2行: 第2行:


local cfg = {
local cfg = {
    wrapStyle = 'overflow-x:auto;width:100%;',
    tableStyle = 'background-color:#212121;color: #EAECF0;white-space: nowrap;',
    defaultClass = 'wikitable',
     firstHeader = "'''防御塔等级与属性项(总和)'''",
     firstHeader = "'''防御塔等级与属性项(总和)'''",
     errCategory = '[[Category:防御塔表格需要修复]]',
     errCategory = '[[Category:防御塔表格需要修复]]',
第11行: 第8行:
local function trim(s)
local function trim(s)
     return mw.text.trim(s or '')
     return mw.text.trim(s or '')
end
local function cell(header, value)
    return '{{c|' .. header .. '|' .. value .. '}}'
end
local function fallbackLabel(call)
    local body = call:gsub('^{{%s*[Tt]wtl%s*|', ''):gsub('%s*}}$', '')
    local parts = {}
    for a in mw.text.gsplit(body, '|') do
        parts[#parts + 1] = trim(a)
    end
    local key = parts[1] or ''
    local arg = parts[2] or ''
    local map = {
        ['基础面板'] = '<strong>基础面板</strong>',
        ['基础升级'] = '<strong>{{c|2|基础升级}}</strong>',
        ['进阶升级'] = '<strong>{{c|3|进阶升级}}</strong>',
        ['q'] = '<strong>{{c|4|分支Q:' .. arg .. '}}</strong>',
        ['w'] = '<strong>{{c|4|分支W:' .. arg .. '}}</strong>',
        ['5q'] = '<strong>{{c|5|分支Q:' .. arg .. '}}</strong>',
        ['5w'] = '<strong>{{c|5|分支W:' .. arg .. '}}</strong>',
        ['4q5q'] = '<strong>{{c|4|(分支Q:)}}{{c|5|分支Q:' .. arg .. '}}</strong>',
        ['4q5w'] = '<strong>{{c|4|(分支Q:)}}{{c|5|分支W:' .. arg .. '}}</strong>',
        ['4w5q'] = '<strong>{{c|4|(分支W:)}}{{c|5|分支Q:' .. arg .. '}}</strong>',
        ['4w5w'] = '<strong>{{c|4|(分支W:)}}{{c|5|分支W:' .. arg .. '}}</strong>',
        ['1'] = '<strong>' .. arg .. '</strong>',
        ['2'] = '<strong>{{c|2|' .. arg .. '}}</strong>',
        ['3'] = '<strong>{{c|3|' .. arg .. '}}</strong>',
        ['4'] = '<strong>{{c|4|' .. arg .. '}}</strong>',
        ['5'] = '<strong>{{c|5|' .. arg .. '}}</strong>',
        ['wno'] = '<strong>{{c|4|分支W:无W分支}}</strong> || colspan="' .. arg .. '" |',
        ['noup'] = '<strong>该防御塔无升级</strong> || colspan="' .. arg .. '" |',
    }
    return '| ' .. (map[key] or '未知的章节')
end
end


function p.main(frame)
function p.main(frame)
     local args = frame.args
     local args = frame:getParent().args
    local parent = frame:getParent()
     local class = args.class or "wikitable"
    if parent and parent.args and (parent.args.header or parent.args.rows) then
     local headerStr = args.header or ""
        args = parent.args
     local rowsStr = args.rows or ""
    end
     local footerStr = args.footer or ""
 
     local class = args.class or cfg.defaultClass
     local headerStr = trim(args.header)
     local rowsStr = trim(args.rows)
     local footerStr = trim(args.footer)
     local issues = {}
     local issues = {}


     if headerStr == '' then
     if trim(headerStr) == "" then
         return cfg.errCategory
         return cfg.errCategory
     end
     end


     local headers = {}
     local headers = {}
     for h in mw.text.gsplit(headerStr, ',') do
     for h in mw.text.gsplit(headerStr, ",") do
         headers[#headers + 1] = trim(h)
         table.insert(headers, mw.text.trim(h))
     end
     end
     local ncol = #headers
     if #headers == 0 then
    if ncol == 0 then
         return cfg.errCategory
         return cfg.errCategory
     end
     end


     local useFallback = args.forcefallback == '1' or not mw.title.new('Template:Twtl').exists
     local rows = {}
    for row in mw.text.gsplit(rowsStr, ";") do
        if trim(row) ~= "" then
            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
    end


     local tableText = '<div style="' .. cfg.wrapStyle .. '">\n'
     local tableText = '<div style="overflow-x:auto;width:100%;">\n{| class="' .. class .. '" style="background-color:#212121;color: #EAECF0;white-space: nowrap;"\n'
        .. '{| class="' .. class .. '" style="' .. cfg.tableStyle .. '"\n'
        .. '| ' .. cfg.firstHeader


    tableText = tableText .. '| ' .. cfg.firstHeader
     for _, h in ipairs(headers) do
     for _, h in ipairs(headers) do
         tableText = tableText .. ' || ' .. cell(h, h)
         tableText = tableText .. ' || {{c|' .. h .. '|' .. h .. '}}'
     end
     end
     tableText = tableText .. '\n'
     tableText = tableText .. '\n'


     for row in mw.text.gsplit(rowsStr, ';') do
     for _, rowCells in ipairs(rows) do
         local rowTrim = trim(row)
         local firstColRaw = rowCells[1] or ""
         if rowTrim ~= '' then
        local markup = firstColRaw:match('^%s*|') or firstColRaw:match('^%s*!') or firstColRaw:match('^{{')
             local cells = {}
         if markup then
            for c in mw.text.gsplit(rowTrim, ',') do
            tableText = tableText .. firstColRaw .. '\n'
                cells[#cells + 1] = trim(c)
        else
            end
             tableText = tableText .. '|-\n| ' .. firstColRaw:gsub('^|%-%s*', '') .. '\n'
        end


             local firstCol = cells[1] or ''
        for i = 2, #rowCells do
            local rowText
             local header = headers[i - 1]
 
             if header == nil then
             if firstCol:match('^{{') then
                 issues[#issues + 1] = 'extra-cell'
                if useFallback and firstCol:match('^{{%s*[Tt]wtl%s*|') then
                 break
                    rowText = '|-\n' .. fallbackLabel(firstCol)
                else
                    rowText = firstCol
                 end
            else
                firstCol = firstCol:gsub('^|%-%s*', '')
                if firstCol == '' then
                    issues[#issues + 1] = 'empty-first-cell'
                    firstCol = ' '
                end
                 rowText = '|-\n| ' .. firstCol
             end
             end
            tableText = tableText .. (i == 2 and '| ' or ' || ') .. '{{c|' .. header .. '|' .. (rowCells[i] or '') .. '}}'
        end


            tableText = tableText .. rowText .. '\n'
        if #rowCells - 1 < #headers then
 
            issues[#issues + 1] = 'missing-cell'
            for i = 2, #cells do
            for i = #rowCells + 1, #headers + 1 do
                local header = headers[i - 1]
                 tableText = tableText .. (i == 2 and '| ' or ' || ') .. '{{c|' .. headers[i - 1] .. '|}}'
                if header == nil then
                    issues[#issues + 1] = 'extra-cell'
                    break
                end
                local value = cells[i]:gsub('|', '{{!}}')
                 tableText = tableText .. (i == 2 and '| ' or ' || ') .. cell(header, value)
             end
             end
        end


            if #cells - 1 < ncol then
        tableText = tableText .. '\n'
                issues[#issues + 1] = 'missing-cell'
                for i = #cells + 1, ncol + 1 do
                    tableText = tableText .. (i == 2 and '| ' or ' || ') .. cell(headers[i - 1], '')
                end
            end
 
            tableText = tableText .. '\n'
        end
     end
     end


     if footerStr ~= '' then
     if footerStr ~= "" then
         tableText = tableText .. '|-\n| ' .. footerStr .. '\n'
         tableText = tableText .. '|-\n| ' .. footerStr .. '\n'
     end
     end

2026年11月25日 (三) 10:44的版本

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

local p = {}

local cfg = {
    firstHeader = "'''防御塔等级与属性项(总和)'''",
    errCategory = '[[Category:防御塔表格需要修复]]',
}

local function trim(s)
    return mw.text.trim(s or '')
end

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

    if trim(headerStr) == "" then
        return cfg.errCategory
    end

    local headers = {}
    for h in mw.text.gsplit(headerStr, ",") do
        table.insert(headers, mw.text.trim(h))
    end
    if #headers == 0 then
        return cfg.errCategory
    end

    local rows = {}
    for row in mw.text.gsplit(rowsStr, ";") do
        if trim(row) ~= "" then
            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
    end

    local tableText = '<div style="overflow-x:auto;width:100%;">\n{| class="' .. class .. '" style="background-color:#212121;color: #EAECF0;white-space: nowrap;"\n'

    tableText = tableText .. '| ' .. cfg.firstHeader
    for _, h in ipairs(headers) do
        tableText = tableText .. ' || {{c|' .. h .. '|' .. h .. '}}'
    end
    tableText = tableText .. '\n'

    for _, rowCells in ipairs(rows) do
        local firstColRaw = rowCells[1] or ""
        local markup = firstColRaw:match('^%s*|') or firstColRaw:match('^%s*!') or firstColRaw:match('^{{')
        if markup then
            tableText = tableText .. firstColRaw .. '\n'
        else
            tableText = tableText .. '|-\n| ' .. firstColRaw:gsub('^|%-%s*', '') .. '\n'
        end

        for i = 2, #rowCells do
            local header = headers[i - 1]
            if header == nil then
                issues[#issues + 1] = 'extra-cell'
                break
            end
            tableText = tableText .. (i == 2 and '| ' or ' || ') .. '{{c|' .. header .. '|' .. (rowCells[i] or '') .. '}}'
        end

        if #rowCells - 1 < #headers then
            issues[#issues + 1] = 'missing-cell'
            for i = #rowCells + 1, #headers + 1 do
                tableText = tableText .. (i == 2 and '| ' or ' || ') .. '{{c|' .. headers[i - 1] .. '|}}'
            end
        end

        tableText = tableText .. '\n'
    end

    if footerStr ~= "" then
        tableText = tableText .. '|-\n| ' .. footerStr .. '\n'
    end

    tableText = tableText .. '|}\n</div>'

    local out = frame:preprocess(tableText)
    if #issues > 0 then
        out = out .. cfg.errCategory
    end
    return out
end

return p