Module:Markup example
Jump to navigation
Jump to search
This template implements {{Markup example}}.
local nowikiParameter = require("Module:Nowiki parameter")
local yesno = require("Module:Yesno")
local inlineFormat = [[<code>%s</code> → %s]]
local listFormat = "\n* " .. inlineFormat
local tableRowFormat = [[
<tr>
<td><pre>%s</pre></td>
<td class="markup-table-output">%s</td>
</tr>
]]
local tableFormat = [[<table class="markup-table wikitable">
<tr>
<th>Markup</th>
<th>Renders as</th>
</tr>
%s
</table>]]
local function renderSingleExample(format, input, frame)
return format:format(
mw.text.encode(input, "<>&\"'["), -- also encode brackets to make sure SMW syntax doesn't get transformed
frame:preprocess(input)
)
end
local p = {}
function p.main(frame)
--- @type string|nil
local input = frame.args[1]
-- if input is a single nowiki tag and nothing else, use the tag's raw contents as the source markup
local unwrapped, usedNowiki = nowikiParameter(input)
if usedNowiki then
input = unwrapped
end
if not input then
return "<span class=error>Error: No input.</span>"
end
local list = yesno(frame.args.list)
local table = yesno(frame.args.table)
local inline = yesno(frame.args.inline)
if list or table then
-- split input by line and render each as a list item or table row
local format = table and tableRowFormat or listFormat
-- split input into lines, render each line into the format
local output = ""
for line in mw.text.gsplit(input, "\n", true) do
line = mw.text.trim(line or "")
if line ~= "" then
output = output .. renderSingleExample(format, line, frame)
end
end
if table then
-- wrap table rows in overarching <table>
return tableFormat:format(output)
else
-- remove leading newline from list format
return output:sub(2) -- remove leading newline
end
elseif inline then
-- inline with no list/table/etc
return renderSingleExample(inlineFormat, input, frame)
else
-- multiline input in a one-row table
return tableFormat:format(
renderSingleExample(tableRowFormat, input, frame)
)
end
end
return p