模块:TowerTable:修订间差异
外观
Jathon15201(留言 | 贡献) 无编辑摘要 |
Jathon15201(留言 | 贡献) test |
||
| 第9行: | 第9行: | ||
local footerStr = args.footer or "" | local footerStr = args.footer or "" | ||
-- | -- 解析表头 | ||
local headers = {} | local headers = {} | ||
for h in mw.text.gsplit(headerStr, ",") do | |||
table.insert(headers, mw.text.trim(h)) | |||
end | end | ||
-- 解析数据行 | -- 解析数据行 | ||
local rows = {} | 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 | ||
end | end | ||
local | -- 使用 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 | for _, h in ipairs(headers) do | ||
caption:tag("td"):wikitext(frame:expandTemplate{ title = 'c', args = { h, h } }) | |||
end | end | ||
-- | -- 构建数据行 | ||
for _, rowCells in ipairs(rows) do | for _, rowCells in ipairs(rows) do | ||
local tr = builder:tag("tr") | |||
local firstColRaw = rowCells[1] or "" | local firstColRaw = rowCells[1] or "" | ||
-- | -- 核心修复点:延迟解析,先保留 wikitext,最后一起交给 frame:preprocess | ||
tr:tag("td"):wikitext(firstColRaw) | |||
for i = 2, #rowCells do | for i = 2, #rowCells do | ||
local header = headers[i-1] or "" | local header = headers[i-1] or "" | ||
local cellValue = rowCells[i] or "" | local cellValue = rowCells[i] or "" | ||
tr:tag("td"):wikitext("{{c|" .. header .. "|" .. cellValue .. "}}") | |||
end | end | ||
end | end | ||
-- | -- 构建脚注 | ||
if footerStr ~= "" then | if footerStr ~= "" then | ||
local tr = builder:tag("tr") | |||
tr:tag("td"):attr("colspan", #headers + 1):wikitext(footerStr) | |||
end | end | ||
-- 执行最后解析 | |||
return | return frame:preprocess(tostring(builder)) | ||
end | end | ||
return p | return p | ||
2026年6月12日 (五) 05:22的版本
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