Module:Markup example

From Final Fantasy XIV Online Wiki
Jump to navigation Jump to search
Documentation for Module:Markup example [view] [edit] [history] [purge] (How does this work?)

This template implements {{Markup example}}.

local nowikiParameter = require("Module:Nowiki parameter")
local yesno = require("Module:Yesno")

local inlineFormat = [[<code>%s</code> &rarr; %s]]

local tableFormat = [[<table class="markup-table wikitable">
    <tr>
        <th>Markup</th>
        <th>Renders as</th>
    </tr>
    <tr>
        <td><pre>%s</pre></td>
        <td class="markup-table-output">%s</td>
    </tr>
</table>]]

function renderInline(frame, input)
    return inlineFormat:format(
        mw.text.encode(input, "<>&\"'["), -- also encode brackets to make sure SMW syntax doesn't get transformed
        frame:preprocess(input)
    )
end

function renderTable(frame, input)
    return tableFormat: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

    if yesno(frame.args.list) then
        -- split input into lines, render each line as a list item
        local output = ""
        for line in mw.text.gsplit(input, "\n", true) do
            line = mw.text.trim(line or "")
            if line ~= "" then
                output = output .. "\n* " .. renderInline(frame, line)
            end
        end
        return output:sub(2) -- remove leading newline
    elseif yesno(frame.args.inline) then
        return renderInline(frame, input)
    else
        return renderTable(frame, input)
    end
end

return p