模块:TowerTable/sandbox
外观
此模块的文档可以在模块:TowerTable/sandbox/doc创建
local p = {}
-- 第一列(行标题)在 {{twtl}} 展开后会带有表格语法:
-- 旧格式:|-\n| <strong>标签</strong>
-- 页脚: '''<strong>标签</strong>''' || colspan="5" |
-- 纯文本:<strong>标签</strong> 或 基础面板(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
-- 去掉尾部的 colspan 属性占位(twtl 的 wno/noup 输出)
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
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 ""
if args.debug then
return "<pre>" .. mw.text.nowiki("header=" .. headerStr .. "\nrows=" .. rowsStr) .. "</pre>"
end
-- 1. 列标题
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;">')
-- 2. 表头行
local headCells = { td("<b>防御塔等级与属性项(总和)</b>") }
for _, h in ipairs(headers) do
table.insert(headCells, td("{{c|" .. h .. "|" .. h .. "}}"))
end
table.insert(out, tr(headCells))
-- 3. 数据行
for row in mw.text.gsplit(rowsStr, ";") do
if mw.text.trim(row) ~= "" then
local cells = {}
for cell in mw.text.gsplit(row, ",") do
table.insert(cells, mw.text.trim(cell))
end
local label, labelSpan = cleanFirstCell(cells[1])
local rowCells = { td(label, labelSpan) }
for i = 2, #cells do
local header = headers[i - 1] or ""
table.insert(rowCells, td("{{c|" .. header .. "|" .. cells[i] .. "}}"))
end
table.insert(out, tr(rowCells))
end
end
-- 4. 脚注行:标签单元格 + 自动补齐的空白单元格
local footer, footerSpan = cleanFirstCell(footerStr)
if footer ~= "" then
local span = footerSpan
if not span or span < 1 then
span = #headers
end
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 table.concat(out, "\n")
end
-- ---- 临时探针 ----
function p.probe_c()
return '<span style="color:#88FD57">X</span>'
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
return p