模块:TowerTable
外观
local p = {}
function p.main(frame)
local args = frame:getParent().args
local class = args.class or "wikitable towertable"
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 rows = {}
for row in mw.text.gsplit(rowsStr, ";") do
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
-- 使用 frame:extensionTag 构建表格
local builder = frame:extensionTag("templatestyles", "", {src = "..."}) ..
mw.html.create("table"):addClass(class):done()
-- 构建表头
local caption = builder:tag("tr")
caption:tag("td"):wikitext("'''防御塔等级与属性项(总和)'''")
for _, h in ipairs(headers) do
caption:tag("td"):wikitext(frame:expandTemplate{ title = 'c', args = { h, h } })
end
-- 构建数据行
for _, rowCells in ipairs(rows) do
local tr = builder:tag("tr")
local firstColRaw = rowCells[1] or ""
-- 核心修复点:延迟解析,先保留 wikitext,最后一起交给 frame:preprocess
tr:tag("td"):wikitext(firstColRaw)
for i = 2, #rowCells do
local header = headers[i-1] or ""
local cellValue = rowCells[i] or ""
tr:tag("td"):wikitext("{{c|" .. header .. "|" .. cellValue .. "}}")
end
end
-- 构建脚注
if footerStr ~= "" then
local tr = builder:tag("tr")
tr:tag("td"):attr("colspan", #headers + 1):wikitext(footerStr)
end
-- 执行最后解析
return frame:preprocess(tostring(builder))
end
return p