模块:TowerTable/sandbox:修订间差异
外观
小 沙盒:纯文本行用 {{!}} 输出表格标记 |
小 测试版:表格结构改由模块直接输出 HTML |
||
| 第1行: | 第1行: | ||
local p = {} | 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) | function p.main(frame) | ||
| 第8行: | 第60行: | ||
local footerStr = args.footer or "" | local footerStr = args.footer or "" | ||
-- 1. 列标题 | |||
local headers = {} | local headers = {} | ||
for h in mw.text.gsplit(headerStr, ",") do | for h in mw.text.gsplit(headerStr, ",") do | ||
| 第13行: | 第66行: | ||
end | end | ||
local | 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 | for _, h in ipairs(headers) do | ||
table.insert(headCells, td("{{c|" .. h .. "|" .. h .. "}}")) | |||
end | 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 | end | ||
table.insert(out, tr(rowCells)) | |||
end | end | ||
end | end | ||
if | -- 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 | end | ||
table.insert(out, "</table></div>") | |||
return | return table.concat(out, "\n") | ||
end | end | ||
return p | return p | ||
2026年11月25日 (三) 10:58的版本
此模块的文档可以在模块: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 ""
-- 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
return p