wikitable
wikitable(schema, data)
schema: 表头
data: 表格数据
Example
数组方式
wikitable(
{"col1", "col2", "col3"},
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}
)
字典方式
wikitable(
{
{
["scope"] = "a",
["label"] = "col1"
},
{
["scope"] = "b",
["label"] = "col2"
},
{
["scope"] = "c",
["label"] = "col3"
}
},
{
{["a"] = 1, ["b"] = 2, ["c"] = 3},
{["a"] = 4, ["b"] = 5, ["c"] = 6},
{["a"] = 7, ["b"] = 8, ["c"] = 9}
}
)
local p = {}
function p.wikitable(schema, data, options)
local transpose, expandRow, expandRowKey, width, class
if options then
transpose = options.transpose or nil
expandRow = options.expandRow or nil
expandRowKey = options.expandRowKey or nil
width = options.width or nil
class = options.class or nil
end
local out = ""
if schema[1]["scope"] then
-- 数据为dict形式
if transpose then
-- 转置
for _, field in ipairs(schema) do
local rowHtml = ""
rowHtml = rowHtml .. "
" .. field.label .. ""for rowNumber, row in ipairs(data) do
if row[field.scope] == nil then
error("wikitable数据第" .. rowNumber .. "列 " .. field.scope .. " 的值为nil")
end
rowHtml = rowHtml .. "
" .. row[field.scope] .. ""end
rowHtml = "
" .. rowHtml .. ""out = out .. rowHtml
end
elseif transpose == nil and expandRow == nil then
for _, field in ipairs(schema) do
out = out .. "
" .. field["label"] .. ""end
out = "
" .. out .. ""for rowNumber, row in ipairs(data) do
local rowHtml = ""
for _, field in ipairs(schema) do
if row[field.scope] == nil then
error("wikitable数据第" .. rowNumber .. "行 " .. field.scope .. " 的值为nil")
end
rowHtml = rowHtml .. "
" .. row[field.scope] .. ""end
rowHtml = "
" .. rowHtml .. ""out = out .. rowHtml
end
elseif expandRow then
for _, field in ipairs(schema) do
out = out .. "
" .. field["label"] .. ""end
out = "
" .. out .. ""for rowNumber, row in ipairs(data) do
local rowHtml = ""
for _, field in ipairs(schema) do
if row[field.scope] == nil then
error("wikitable数据第" .. rowNumber .. "行 " .. field.scope .. " 的值为nil")
end
rowHtml = rowHtml .. "
" .. row[field.scope] .. ""end
rowHtml = '
' .. rowHtml .. ""out = out .. rowHtml
rowHtml = ""
local expandContent = ""
for _, field in ipairs(expandRow) do
if row[field.scope] and #tostring(row[field.scope]) > 0 then
expandContent =
expandContent ..
mw.ustring.format(
[[
]],field.label,
row[field.scope]
)
end
end
rowHtml =
rowHtml .. '
rowHtml =
'
" .. rowHtml .. ""out = out .. rowHtml
end
end
else
-- 数据为array形式
if transpose then
-- 转置
for i, label in ipairs(schema) do
local rowHtml = ""
rowHtml = rowHtml .. "
" .. label .. ""for _, row in ipairs(data) do
rowHtml = rowHtml .. "
" .. row[i] .. ""end
rowHtml = "
" .. rowHtml .. ""out = out .. rowHtml
end
else
for _, label in ipairs(schema) do
out = out .. "
" .. label .. ""end
out = "
" .. out .. ""for _, row in ipairs(data) do
local rowHtml = ""
for i, _ in ipairs(schema) do
rowHtml = rowHtml .. "
" .. row[i] .. ""end
rowHtml = "
" .. rowHtml .. ""out = out .. rowHtml
end
end
end
out =
mw.ustring.format(
'
\n',class or "wikitable",
width and 'style="width:' .. width .. ';"' or "",
out
)
return out
end
function p.h2(text)
return "
" .. text .. "
"end
function p.h3(text)
return "
" .. text .. "
"end
function p.h4(text)
return "
" .. text .. "
"end
function p.p(text)
return "
" .. text .. "
"end
function p.ul(array)
if #array > 0 then
local text = ""
for _, v in ipairs(array) do
if #v > 0 then
text = text .. "
" .. v .. ""end
end
return "
- " .. text .. "
end
return ""
end
function p.ol(array)
if #array > 0 then
local text = ""
for _, v in ipairs(array) do
if #v > 0 then
text = text .. "
" .. v .. ""end
end
return "
- " .. text .. "
end
return ""
end
return p