跳转到内容

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

来自Minimalist Tower Defense Wiki
Jathonbot​(留言 | 贡献)
创建 TowerTable 沙盒版本用于测试
 
Jathonbot​(留言 | 贡献)
小 测试页同步为当前正式实现
 
(未显示同一用户的23个中间版本)
第1行: 第1行:
-- 与 Module:TowerTable 同步的测试副本,修改正式模块前可先在此验证。
-- 页面用法:{{TowerTable/sandbox|header=…|rows=…|footer=…}}
local p = {}
local p = {}


local cfg = {
-- 按分隔符拆分字符串,但忽略 {{…}}、[[…]] 内部以及 HTML 标签(<…>)内部的分隔符。
     wrapStyle = 'overflow-x:auto;width:100%;',
local function splitTop(text, sep)
    tableStyle = 'background-color:#212121;color: #EAECF0;white-space: nowrap;',
    local parts, cur = {}, {}
     defaultClass = 'wikitable',
    local depth, inTag = 0, false
     firstHeader = "'''防御塔等级与属性项(总和)'''",
    local i, n = 1, #text
     errCategory = '[[Category:防御塔表格需要修复]]',
     while i <= n do
}
        local ch = text:sub(i, i)
        local two = text:sub(i, i + 1)
        if inTag then
            if ch == ">" then inTag = false end
            cur[#cur + 1] = ch
            i = i + 1
        elseif two == "{{" or two == "[[" then
            depth = depth + 1
            cur[#cur + 1] = two
            i = i + 2
        elseif two == "}}" or two == "]]" then
            if depth > 0 then depth = depth - 1 end
            cur[#cur + 1] = two
            i = i + 2
        elseif ch == "<" and depth == 0 then
            inTag = true
            cur[#cur + 1] = ch
            i = i + 1
        elseif ch == sep and depth == 0 then
            parts[#parts + 1] = table.concat(cur)
            cur = {}
            i = i + 1
        else
            cur[#cur + 1] = ch
            i = i + 1
        end
    end
    parts[#parts + 1] = table.concat(cur)
    return parts
end
 
local function cleanFirstCell(raw)
    local s = mw.text.trim(raw or "")
    s = s:gsub("^|%-[^\n]*\n?", "")
     s = mw.text.trim(s)
 
    local colspan
    local body, attr = s:match("^(.-)%s*||%s*([^|]-)%s*|%s*$")
     if body and attr:find("colspan", 1, true) then
        s = body
        colspan = tonumber(attr:match("colspan%s*=%s*\"?(%d+)"))
    end
    s = mw.text.trim(s)
 
    if s:sub(1, 2) == "||" then
        s = s:sub(3)
     elseif s:sub(1, 1) == "|" then
        local rest = s:sub(2)
        local a, c = rest:match("^([^|\n]-)|(.*)$")
        if a and (a:find("colspan", 1, true) or a:find("rowspan", 1, true)
            or a:find("style", 1, true) or a:find("class", 1, true)) then
            if not colspan then
                colspan = tonumber(a:match("colspan%s*=%s*\"?(%d+)"))
            end
            s = c
        else
            s = rest
        end
    end


local function trim(s)
    s = s:gsub("%s*\n%s*", " ")
     return mw.text.trim(s or '')
     return mw.text.trim(s), colspan
end
end


local function cell(header, value)
local function td(content, colspan)
     return '{{c|' .. header .. '|' .. value .. '}}'
     if colspan then
        return '<td colspan="' .. colspan .. '">' .. content .. "</td>"
    end
    return "<td>" .. content .. "</td>"
end
 
local function tr(cells)
    return "<tr>" .. table.concat(cells) .. "</tr>"
end
end


function p.main(frame)
function p.main(frame)
     local args = frame.args
     local args = frame:getParent().args
    local parent = frame:getParent()
     if not (args.header or args.rows or args.footer) then
     if parent and parent.args and (parent.args.header or parent.args.rows) then
         args = frame.args
         args = parent.args
     end
     end


     local class = args.class or cfg.defaultClass
     local class = args.class or "wikitable"
     local headerStr = trim(args.header)
     local headerStr = args.header or ""
     local rowsStr = trim(args.rows)
     local rowsStr = args.rows or ""
     local footerStr = trim(args.footer)
     local footerStr = args.footer or ""
    local issues = {}
 
    if headerStr == '' then
        return cfg.errCategory
    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
    local ncol = #headers
    if ncol == 0 then
        return cfg.errCategory
     end
     end


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


    local headCells = { td("<b>防御塔等级与属性项(总和)</b>") }
     for _, h in ipairs(headers) do
     for _, h in ipairs(headers) do
         tableText = tableText .. ' || ' .. cell(h, h)
         table.insert(headCells, td("{{c|" .. h .. "|" .. h .. "}}"))
     end
     end
     tableText = tableText .. '\n'
     table.insert(out, tr(headCells))


     for row in mw.text.gsplit(rowsStr, ';') do
     for _, row in ipairs(splitTop(rowsStr, ";")) do
         local rowTrim = trim(row)
         if mw.text.trim(row) ~= "" then
        if rowTrim ~= '' then
             local cells = splitTop(row, ",")
             local cells = {}
            local label, labelSpan = cleanFirstCell(cells[1])
             for c in mw.text.gsplit(rowTrim, ',') do
            local rowCells = { td(label, labelSpan) }
                 cells[#cells + 1] = trim(c)
             for i = 2, #headers + 1 do
                local value = mw.text.trim(cells[i] or "")
                 table.insert(rowCells, td("{{c|" .. headers[i - 1] .. "|" .. value .. "}}"))
             end
             end
 
             table.insert(out, tr(rowCells))
             local firstCol = cells[1] or ''
            firstCol = firstCol:gsub('^|%-%s*', '')
            if firstCol == '' then
                firstCol = '| '
                issues[#issues + 1] = 'empty-first-cell'
            elseif not firstCol:match('^[|!]') then
                firstCol = '| ' .. firstCol
            end
 
            tableText = tableText .. '|-\n' .. firstCol .. '\n'
 
            for i = 2, #cells do
                local header = 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
 
            if #cells - 1 < ncol then
                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
     end


     if footerStr ~= '' then
    local footer, footerSpan = cleanFirstCell(footerStr)
         tableText = tableText .. '|-\n| ' .. footerStr .. '\n'
     if footer ~= "" then
         local span = footerSpan or #headers
        local footerCells = { td(footer) }
        if span > 0 then
            table.insert(footerCells, td("", span))
        end
        table.insert(out, tr(footerCells))
     end
     end


     tableText = tableText .. '|}\n</div>'
     table.insert(out, "</table></div>")


     local out = frame:preprocess(tableText)
     return frame:preprocess(table.concat(out, "\n"))
    if #issues > 0 then
        out = out .. cfg.errCategory
    end
    return out
end
end


return p
return p

2026年11月25日 (三) 11:12的最新版本

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

-- 与 Module:TowerTable 同步的测试副本,修改正式模块前可先在此验证。
-- 页面用法:{{TowerTable/sandbox|header=…|rows=…|footer=…}}

local p = {}

-- 按分隔符拆分字符串,但忽略 {{…}}、[[…]] 内部以及 HTML 标签(<…>)内部的分隔符。
local function splitTop(text, sep)
    local parts, cur = {}, {}
    local depth, inTag = 0, false
    local i, n = 1, #text
    while i <= n do
        local ch = text:sub(i, i)
        local two = text:sub(i, i + 1)
        if inTag then
            if ch == ">" then inTag = false end
            cur[#cur + 1] = ch
            i = i + 1
        elseif two == "{{" or two == "[[" then
            depth = depth + 1
            cur[#cur + 1] = two
            i = i + 2
        elseif two == "}}" or two == "]]" then
            if depth > 0 then depth = depth - 1 end
            cur[#cur + 1] = two
            i = i + 2
        elseif ch == "<" and depth == 0 then
            inTag = true
            cur[#cur + 1] = ch
            i = i + 1
        elseif ch == sep and depth == 0 then
            parts[#parts + 1] = table.concat(cur)
            cur = {}
            i = i + 1
        else
            cur[#cur + 1] = ch
            i = i + 1
        end
    end
    parts[#parts + 1] = table.concat(cur)
    return parts
end

local function cleanFirstCell(raw)
    local s = mw.text.trim(raw or "")
    s = s:gsub("^|%-[^\n]*\n?", "")
    s = mw.text.trim(s)

    local colspan
    local body, attr = s:match("^(.-)%s*||%s*([^|]-)%s*|%s*$")
    if body and attr:find("colspan", 1, true) then
        s = body
        colspan = tonumber(attr:match("colspan%s*=%s*\"?(%d+)"))
    end
    s = mw.text.trim(s)

    if s:sub(1, 2) == "||" then
        s = s:sub(3)
    elseif s:sub(1, 1) == "|" then
        local rest = s:sub(2)
        local a, c = rest:match("^([^|\n]-)|(.*)$")
        if a and (a:find("colspan", 1, true) or a:find("rowspan", 1, true)
            or a:find("style", 1, true) or a:find("class", 1, true)) then
            if not colspan then
                colspan = tonumber(a:match("colspan%s*=%s*\"?(%d+)"))
            end
            s = c
        else
            s = rest
        end
    end

    s = s:gsub("%s*\n%s*", " ")
    return mw.text.trim(s), colspan
end

local function td(content, colspan)
    if colspan then
        return '<td colspan="' .. colspan .. '">' .. content .. "</td>"
    end
    return "<td>" .. content .. "</td>"
end

local function tr(cells)
    return "<tr>" .. table.concat(cells) .. "</tr>"
end

function p.main(frame)
    local args = frame:getParent().args
    if not (args.header or args.rows or args.footer) then
        args = frame.args
    end

    local class = args.class or "wikitable"
    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 out = {}
    table.insert(out, '<div style="overflow-x:auto;width:100%;"><table class="' .. class
        .. '" style="background-color:#212121;color: #EAECF0;white-space: nowrap;">')

    local headCells = { td("<b>防御塔等级与属性项(总和)</b>") }
    for _, h in ipairs(headers) do
        table.insert(headCells, td("{{c|" .. h .. "|" .. h .. "}}"))
    end
    table.insert(out, tr(headCells))

    for _, row in ipairs(splitTop(rowsStr, ";")) do
        if mw.text.trim(row) ~= "" then
            local cells = splitTop(row, ",")
            local label, labelSpan = cleanFirstCell(cells[1])
            local rowCells = { td(label, labelSpan) }
            for i = 2, #headers + 1 do
                local value = mw.text.trim(cells[i] or "")
                table.insert(rowCells, td("{{c|" .. headers[i - 1] .. "|" .. value .. "}}"))
            end
            table.insert(out, tr(rowCells))
        end
    end

    local footer, footerSpan = cleanFirstCell(footerStr)
    if footer ~= "" then
        local span = footerSpan or #headers
        local footerCells = { td(footer) }
        if span > 0 then
            table.insert(footerCells, td("", span))
        end
        table.insert(out, tr(footerCells))
    end

    table.insert(out, "</table></div>")

    return frame:preprocess(table.concat(out, "\n"))
end

return p