模块:TowerTable/sandbox:修订间差异
外观
小 沙盒:完整改进版(纯文本标签 + 列数校验 + 兜底) |
小 沙盒:最小改动(仅纯文本标签补行分隔符) |
||
| 第1行: | 第1行: | ||
local p = {} | local p = {} | ||
function p.main(frame) | function p.main(frame) | ||
| 第16行: | 第7行: | ||
local rowsStr = args.rows or "" | local rowsStr = args.rows or "" | ||
local footerStr = args.footer or "" | local footerStr = args.footer or "" | ||
local headers = {} | local headers = {} | ||
for h in mw.text.gsplit(headerStr, ",") do | for h in mw.text.gsplit(headerStr, ",") do | ||
table.insert(headers, mw.text.trim(h)) | table.insert(headers, mw.text.trim(h)) | ||
end | end | ||
local rows = {} | local rows = {} | ||
for row in mw.text.gsplit(rowsStr, ";") do | 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 | ||
| 第45行: | 第26行: | ||
local tableText = '<div style="overflow-x:auto;width:100%;">\n{| class="' .. class .. '" style="background-color:#212121;color: #EAECF0;white-space: nowrap;"\n' | local tableText = '<div style="overflow-x:auto;width:100%;">\n{| class="' .. class .. '" style="background-color:#212121;color: #EAECF0;white-space: nowrap;"\n' | ||
tableText = tableText .. '| ' | tableText = tableText .. '| \'\'\'防御塔等级与属性项(总和)\'\'\'' | ||
for _, h in ipairs(headers) do | for _, h in ipairs(headers) do | ||
tableText = tableText .. ' || {{c|' .. h .. '|' .. h .. '}}' | tableText = tableText .. ' || {{c|' .. h .. '|' .. h .. '}}' | ||
| 第53行: | 第34行: | ||
for _, rowCells in ipairs(rows) do | for _, rowCells in ipairs(rows) do | ||
local firstColRaw = rowCells[1] or "" | local firstColRaw = rowCells[1] or "" | ||
if firstColRaw:match('^{{') or firstColRaw:match('^|') then | |||
tableText = tableText .. firstColRaw .. '\n' | tableText = tableText .. firstColRaw .. '\n' | ||
else | else | ||
tableText = tableText .. '|-\n| ' .. firstColRaw | tableText = tableText .. '|-\n| ' .. firstColRaw .. '\n' | ||
end | end | ||
for i = 2, #rowCells do | for i = 2, #rowCells do | ||
local header = headers[i - 1] | local header = headers[i-1] or "" | ||
if | local cellValue = rowCells[i] or "" | ||
if i == 2 then | |||
tableText = tableText .. '| {{c|' .. header .. '|' .. cellValue .. '}}' | |||
else | |||
tableText = tableText .. ' || {{c|' .. header .. '|' .. cellValue .. '}}' | |||
end | end | ||
end | end | ||
tableText = tableText .. '\n' | tableText = tableText .. '\n' | ||
end | end | ||
if footerStr ~= "" then | if footerStr ~= "" then | ||
tableText = tableText .. '|-\n| ' .. footerStr .. '\n' | tableText = tableText .. '|-\n' | ||
tableText = tableText .. '| ' .. footerStr .. '\n' | |||
end | end | ||
tableText = tableText .. '|}\n</div>' | tableText = tableText .. '|}\n</div>' | ||
return frame:preprocess(tableText) | |||
end | end | ||
return p | return p | ||
2026年11月25日 (三) 10:45的版本
此模块的文档可以在模块:TowerTable/sandbox/doc创建
local p = {}
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 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
local tableText = '<div style="overflow-x:auto;width:100%;">\n{| class="' .. class .. '" style="background-color:#212121;color: #EAECF0;white-space: nowrap;"\n'
tableText = tableText .. '| \'\'\'防御塔等级与属性项(总和)\'\'\''
for _, h in ipairs(headers) do
tableText = tableText .. ' || {{c|' .. h .. '|' .. h .. '}}'
end
tableText = tableText .. '\n'
for _, rowCells in ipairs(rows) do
local firstColRaw = rowCells[1] or ""
if firstColRaw:match('^{{') or firstColRaw:match('^|') then
tableText = tableText .. firstColRaw .. '\n'
else
tableText = tableText .. '|-\n| ' .. firstColRaw .. '\n'
end
for i = 2, #rowCells do
local header = headers[i-1] or ""
local cellValue = rowCells[i] or ""
if i == 2 then
tableText = tableText .. '| {{c|' .. header .. '|' .. cellValue .. '}}'
else
tableText = tableText .. ' || {{c|' .. header .. '|' .. cellValue .. '}}'
end
end
tableText = tableText .. '\n'
end
if footerStr ~= "" then
tableText = tableText .. '|-\n'
tableText = tableText .. '| ' .. footerStr .. '\n'
end
tableText = tableText .. '|}\n</div>'
return frame:preprocess(tableText)
end
return p