跳转到内容

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

来自Minimalist Tower Defense Wiki
Jathonbot​(留言 | 贡献)
小 临时探针
Jathonbot​(留言 | 贡献)
小 测试版:表格结构改由模块输出 HTML,行/单元格分隔符忽略标签内字符
第1行: 第1行:
local p = {}
local p = {}


-- 第一列(行标题)在 {{twtl}} 展开后会带有表格语法:
-- 按分隔符拆分字符串,但忽略 {{…}}、[[…]] 内部以及 HTML 标签(<…>)内部的分隔符。
--  旧格式:|-\n| <strong>标签</strong>
-- 例如 <span style="color:#88FD57;"> 里的分号不会被当作行分隔符。
--  页脚:  '''<strong>标签</strong>''' || colspan="5" |
local function splitTop(text, sep)
--  纯文本:<strong>标签</strong> 或 基础面板(twtl 缺失/未知键时)
    local parts, cur = {}, {}
-- 这里统一取出「单元格内容」,并返回可选的 colspan,
    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
 
-- 第一列(行标题)可能是:
--  旧版 {{twtl}} 展开后的 "|-\n| <strong>标签</strong>"
--  页脚里的 "'''<strong>标签</strong>''' || colspan="5" |"
--  纯文本(twtl 缺失或未知键时)
-- 这里只取出单元格内容与可选 colspan,表格结构全部由模块负责。
local function cleanFirstCell(raw)
local function cleanFirstCell(raw)
     local s = mw.text.trim(raw or "")
     local s = mw.text.trim(raw or "")
    -- 去掉行分隔符 |- 及紧随的换行
     s = s:gsub("^|%-[^\n]*\n?", "")
     s = s:gsub("^|%-[^\n]*\n?", "")
     s = mw.text.trim(s)
     s = mw.text.trim(s)


     local colspan
     local colspan
    -- 去掉尾部的 colspan 属性占位(twtl 的 wno/noup 输出)
     local body, attr = s:match("^(.-)%s*||%s*([^|]-)%s*|%s*$")
     local body, attr = s:match("^(.-)%s*||%s*([^|]-)%s*|%s*$")
     if body and attr:find("colspan", 1, true) then
     if body and attr:find("colspan", 1, true) then
第22行: 第57行:
     s = mw.text.trim(s)
     s = mw.text.trim(s)


    -- 去掉行内单元格标记 | 及其属性
     if s:sub(1, 2) == "||" then
     if s:sub(1, 2) == "||" then
         s = s:sub(3)
         s = s:sub(3)
第39行: 第73行:
     end
     end


    -- 标签里的换行只是模板排版残留,压成空格以与渲染结果保持一致
    s = s:gsub("%s*\n%s*", " ")
     return mw.text.trim(s), colspan
     return mw.text.trim(s), colspan
end
end
第60行: 第96行:
     local footerStr = args.footer or ""
     local footerStr = args.footer or ""


    if args.dump then
        local hex = args.dump:gsub(".", function(c)
            return string.format("%02X ", string.byte(c))
        end)
        return "<div>" .. hex .. "</div>"
    end
    if args.echo then
        return "<em>" .. args.echo .. "</em>"
    end
    if args.echo_td then
        return '<table class="wikitable"><tr><td>' .. args.echo_td .. "</td></tr></table>"
    end
    if args.debug then
        local function tag(s)
            return (s:gsub("<", "[LT]"):gsub(">", "[GT]"):gsub("\n", "[NL]"):gsub("&", "[AMP]"))
        end
        return "<div>header=" .. tag(headerStr) .. "<br>rows=" .. tag(rowsStr) .. "</div>"
    end
    -- 1. 列标题
     local headers = {}
     local headers = {}
     for h in mw.text.gsplit(headerStr, ",") do
     for h in mw.text.gsplit(headerStr, ",") do
第92行: 第105行:
         .. '" style="background-color:#212121;color: #EAECF0;white-space: nowrap;">')
         .. '" style="background-color:#212121;color: #EAECF0;white-space: nowrap;">')


     -- 2. 表头行
     -- 表头行
     local headCells = { td("<b>防御塔等级与属性项(总和)</b>") }
     local headCells = { td("<b>防御塔等级与属性项(总和)</b>") }
     for _, h in ipairs(headers) do
     for _, h in ipairs(headers) do
第99行: 第112行:
     table.insert(out, tr(headCells))
     table.insert(out, tr(headCells))


     -- 3. 数据行
     -- 数据行
     for row in mw.text.gsplit(rowsStr, ";") do
     for _, row in ipairs(splitTop(rowsStr, ";")) do
         if mw.text.trim(row) ~= "" then
         if mw.text.trim(row) ~= "" then
             local cells = {}
             local cells = splitTop(row, ",")
            for cell in mw.text.gsplit(row, ",") do
                table.insert(cells, mw.text.trim(cell))
            end
 
             local label, labelSpan = cleanFirstCell(cells[1])
             local label, labelSpan = cleanFirstCell(cells[1])
            if args.lab then
                local hx = label:gsub(".", function(ch)
                    return string.format("%02X ", string.byte(ch))
                end)
                return "<div>" .. hx .. "</div>"
            end
             local rowCells = { td(label, labelSpan) }
             local rowCells = { td(label, labelSpan) }
             for i = 2, #cells do
             for i = 2, #headers + 1 do
                 local header = headers[i - 1] or ""
                 local value = mw.text.trim(cells[i] or "")
                 table.insert(rowCells, td("{{c|" .. header .. "|" .. cells[i] .. "}}"))
                 table.insert(rowCells, td("{{c|" .. headers[i - 1] .. "|" .. value .. "}}"))
             end
             end
             table.insert(out, tr(rowCells))
             table.insert(out, tr(rowCells))
第123行: 第126行:
     end
     end


     -- 4. 脚注行:标签单元格 + 自动补齐的空白单元格
     -- 脚注行:标签单元格 + 自动补齐的空白单元格(不再需要手写列数)
     local footer, footerSpan = cleanFirstCell(footerStr)
     local footer, footerSpan = cleanFirstCell(footerStr)
     if footer ~= "" then
     if footer ~= "" then
         local span = footerSpan
         local span = footerSpan or #headers
        if not span or span < 1 then
            span = #headers
        end
         local footerCells = { td(footer) }
         local footerCells = { td(footer) }
         if span > 0 then
         if span > 0 then
第139行: 第139行:
     table.insert(out, "</table></div>")
     table.insert(out, "</table></div>")


     local raw = table.concat(out, "\n")
     -- 列标题与单元格颜色仍由 Template:Color 决定,这里只对表格片段做一次模板展开
    if args.nlfix then
     return frame:preprocess(table.concat(out, "\n"))
        raw = raw:gsub("style=\"color:\n", "style=\"color:")
    end
     if args.pp then
        return frame:preprocess(raw)
    end
    return raw
end
 
-- ---- 临时探针 ----
function p.probe_c()
    return '<span style="color:#88FD57">X</span>'
end
function p.probe_nl1()
    return '<span style="color:\n#88FD57">X</span>'
end
function p.probe_nl2()
    return '<span\nstyle="color:#88FD57">X</span>'
end
function p.probe_entity()
    return '&lt;span style="color:#88FD57"&gt;X&lt;/span&gt;'
end
function p.probe_td(frame)
    return '<table class="wikitable"><tr><td><span style="color:\n#88FD57">X</span></td></tr></table>'
end
function p.probe_td2(frame)
    return '<table class="wikitable"><tr><td><span style="color:#88FD57">X</span></td></tr></table>'
end
function p.echo(frame)
    return '<em>' .. tostring(frame.args[1]) .. '</em>'
end
function p.echo_parent(frame)
    return '<em>' .. tostring(frame:getParent().args[1]) .. '</em>'
end
function p.probe_pp(frame)
    return '<em>' .. frame:preprocess(tostring(frame.args[1] or '')) .. '</em>'
end
end


return p
return p

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

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

local p = {}

-- 按分隔符拆分字符串,但忽略 {{…}}、[[…]] 内部以及 HTML 标签(<…>)内部的分隔符。
-- 例如 <span style="color:#88FD57;"> 里的分号不会被当作行分隔符。
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

-- 第一列(行标题)可能是:
--   旧版 {{twtl}} 展开后的 "|-\n| <strong>标签</strong>"
--   页脚里的 "'''<strong>标签</strong>''' || colspan="5" |"
--   纯文本(twtl 缺失或未知键时)
-- 这里只取出单元格内容与可选 colspan,表格结构全部由模块负责。
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
    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>")

    -- 列标题与单元格颜色仍由 Template:Color 决定,这里只对表格片段做一次模板展开
    return frame:preprocess(table.concat(out, "\n"))
end

return p