模块:Html:修订间差异
来自乐园数据管理室
无编辑摘要 |
无编辑摘要 |
||
| (未显示同一用户的1个中间版本) | |||
| 第1行: | 第1行: | ||
local p = {} | local p = {} | ||
function p.wikitable(schema, data) | 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 = "" | local out = "" | ||
if schema[1]["scope"] then | if schema[1]["scope"] then | ||
-- 数据为dict形式 | -- 数据为dict形式 | ||
for | if transpose then | ||
out = out .. "<th>" .. field["label"] .. "</th>" | -- 转置 | ||
end | for _, field in ipairs(schema) do | ||
local rowHtml = "" | |||
rowHtml = rowHtml .. "<th>" .. field.label .. "</th>" | |||
for rowNumber, row in ipairs(data) do | |||
if row[field.scope] == nil then | |||
error("wikitable数据第" .. rowNumber .. "列 " .. field.scope .. " 的值为nil") | |||
end | |||
rowHtml = rowHtml .. "<td>" .. row[field.scope] .. "</td>" | |||
end | |||
rowHtml = "<tr>" .. rowHtml .. "</tr>" | |||
out = out .. rowHtml | |||
end | |||
elseif transpose == nil and expandRow == nil then | |||
for _, field in ipairs(schema) do | |||
out = out .. "<th>" .. field["label"] .. "</th>" | |||
end | |||
out = "<tr>" .. out .. "</tr>" | |||
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 .. "<td>" .. row[field.scope] .. "</td>" | |||
end | |||
rowHtml = "<tr>" .. rowHtml .. "</tr>" | |||
out = out .. rowHtml | |||
end | |||
elseif expandRow then | |||
for _, field in ipairs(schema) do | |||
out = out .. "<th>" .. field["label"] .. "</th>" | |||
end | |||
out = "<tr>" .. out .. "</tr>" | |||
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 .. "<td>" .. row[field.scope] .. "</td>" | |||
end | |||
rowHtml = '<tr class="mw-customtoggle-' .. row[expandRowKey] .. '">' .. rowHtml .. "</tr>" | |||
out = out .. rowHtml | |||
rowHtml = "" | |||
rowHtml = rowHtml .. "<td>" .. row[ | local expandContent = "" | ||
for _, field in ipairs(expandRow) do | |||
if row[field.scope] and #tostring(row[field.scope]) > 0 then | |||
expandContent = | |||
expandContent .. | |||
mw.ustring.format( | |||
[[<div class="expand-item"><div class="label">%s</div><div style="display: inline-block;">%s</div></div>]], | |||
field.label, | |||
row[field.scope] | |||
) | |||
end | |||
end | |||
rowHtml = | |||
rowHtml .. '<td colspan="' .. #schema .. '" ><div class="flex-wrap" >' .. expandContent .. "</div></td>" | |||
rowHtml = | |||
'<tr class="mw-collapsible mw-collapsed" id="mw-customcollapsible-' .. | |||
row[expandRowKey] .. ">" .. rowHtml .. "</tr>" | |||
out = out .. rowHtml | |||
end | end | ||
end | end | ||
else | else | ||
-- 数据为array形式 | -- 数据为array形式 | ||
for i, label in ipairs(schema) do | |||
out = out .. "<th>" .. label .. "</th>" | if transpose then | ||
-- 转置 | |||
for i, label in ipairs(schema) do | |||
local rowHtml = "" | |||
rowHtml = rowHtml .. "<th>" .. label .. "</th>" | |||
for _, row in ipairs(data) do | |||
rowHtml = rowHtml .. "<td>" .. row[i] .. "</td>" | |||
end | |||
rowHtml = "<tr>" .. rowHtml .. "</tr>" | |||
out = out .. rowHtml | |||
end | |||
else | |||
for _, label in ipairs(schema) do | |||
out = out .. "<th>" .. label .. "</th>" | |||
end | |||
out = "<tr>" .. out .. "</tr>" | |||
for _, row in ipairs(data) do | |||
local rowHtml = "" | |||
for i, _ in ipairs(schema) do | |||
rowHtml = rowHtml .. "<td>" .. row[i] .. "</td>" | |||
end | |||
rowHtml = "<tr>" .. rowHtml .. "</tr>" | |||
out = out .. rowHtml | |||
end | |||
end | end | ||
end | |||
out = | |||
mw.ustring.format( | |||
'<table class="%s" %s >%s</table>\n', | |||
class or "wikitable", | |||
width and 'style="width:' .. width .. ';"' or "", | |||
out | |||
) | |||
return out | |||
end | |||
function p.h2(text) | |||
return "<h2>" .. text .. "</h2>" | |||
end | |||
function p.h3(text) | |||
return "<h3>" .. text .. "</h3>" | |||
end | |||
function p.h4(text) | |||
return "<h4>" .. text .. "</h4>" | |||
end | |||
function p.p(text) | |||
return "<p>" .. text .. "</p>" | |||
end | |||
function p.ul(array) | |||
if #array > 0 then | |||
local text = "" | |||
for _, v in ipairs(array) do | |||
if #v > 0 then | |||
text = text .. "<li>" .. v .. "</li>" | |||
end | end | ||
end | end | ||
return "<ul>" .. text .. "</ul>" | |||
end | end | ||
return "" | |||
end | |||
function p.ol(array) | |||
return | if #array > 0 then | ||
local text = "" | |||
for _, v in ipairs(array) do | |||
if #v > 0 then | |||
text = text .. "<li>" .. v .. "</li>" | |||
end | |||
end | |||
return "<ol>" .. text .. "</ol>" | |||
end | |||
return "" | |||
end | end | ||
return p | return p | ||
2019年5月17日 (五) 18:01的最新版本
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 .. "<th>" .. field.label .. "</th>"
for rowNumber, row in ipairs(data) do
if row[field.scope] == nil then
error("wikitable数据第" .. rowNumber .. "列 " .. field.scope .. " 的值为nil")
end
rowHtml = rowHtml .. "<td>" .. row[field.scope] .. "</td>"
end
rowHtml = "<tr>" .. rowHtml .. "</tr>"
out = out .. rowHtml
end
elseif transpose == nil and expandRow == nil then
for _, field in ipairs(schema) do
out = out .. "<th>" .. field["label"] .. "</th>"
end
out = "<tr>" .. out .. "</tr>"
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 .. "<td>" .. row[field.scope] .. "</td>"
end
rowHtml = "<tr>" .. rowHtml .. "</tr>"
out = out .. rowHtml
end
elseif expandRow then
for _, field in ipairs(schema) do
out = out .. "<th>" .. field["label"] .. "</th>"
end
out = "<tr>" .. out .. "</tr>"
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 .. "<td>" .. row[field.scope] .. "</td>"
end
rowHtml = '<tr class="mw-customtoggle-' .. row[expandRowKey] .. '">' .. rowHtml .. "</tr>"
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(
[[<div class="expand-item"><div class="label">%s</div><div style="display: inline-block;">%s</div></div>]],
field.label,
row[field.scope]
)
end
end
rowHtml =
rowHtml .. '<td colspan="' .. #schema .. '" ><div class="flex-wrap" >' .. expandContent .. "</div></td>"
rowHtml =
'<tr class="mw-collapsible mw-collapsed" id="mw-customcollapsible-' ..
row[expandRowKey] .. ">" .. rowHtml .. "</tr>"
out = out .. rowHtml
end
end
else
-- 数据为array形式
if transpose then
-- 转置
for i, label in ipairs(schema) do
local rowHtml = ""
rowHtml = rowHtml .. "<th>" .. label .. "</th>"
for _, row in ipairs(data) do
rowHtml = rowHtml .. "<td>" .. row[i] .. "</td>"
end
rowHtml = "<tr>" .. rowHtml .. "</tr>"
out = out .. rowHtml
end
else
for _, label in ipairs(schema) do
out = out .. "<th>" .. label .. "</th>"
end
out = "<tr>" .. out .. "</tr>"
for _, row in ipairs(data) do
local rowHtml = ""
for i, _ in ipairs(schema) do
rowHtml = rowHtml .. "<td>" .. row[i] .. "</td>"
end
rowHtml = "<tr>" .. rowHtml .. "</tr>"
out = out .. rowHtml
end
end
end
out =
mw.ustring.format(
'<table class="%s" %s >%s</table>\n',
class or "wikitable",
width and 'style="width:' .. width .. ';"' or "",
out
)
return out
end
function p.h2(text)
return "<h2>" .. text .. "</h2>"
end
function p.h3(text)
return "<h3>" .. text .. "</h3>"
end
function p.h4(text)
return "<h4>" .. text .. "</h4>"
end
function p.p(text)
return "<p>" .. text .. "</p>"
end
function p.ul(array)
if #array > 0 then
local text = ""
for _, v in ipairs(array) do
if #v > 0 then
text = text .. "<li>" .. v .. "</li>"
end
end
return "<ul>" .. text .. "</ul>"
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 .. "<li>" .. v .. "</li>"
end
end
return "<ol>" .. text .. "</ol>"
end
return ""
end
return p