Html
来自蒙纳多研究本部
此模块的文档可以在模块:Html/doc创建
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 function p.compress(wikitext) wikitext = mw.ustring.gsub(wikitext, "\n", "") wikitext = mw.ustring.gsub(wikitext, ">%s+<", "><") wikitext = mw.ustring.gsub(wikitext, ">%s+{{", ">{{") wikitext = mw.ustring.gsub(wikitext, "}}%s+<", "}}<") return wikitext end function p.addlink(title, namespace, pagename) if pagename == nil then pagename = title end return mw.ustring.format("[[%s:%s|%s]]", namespace, pagename, title) end return p