Jump to content
Discussions

Module:Template documentation

From Undertale Wiki

Does formatting of our template documentation, including Template:Template category and Template:Samples.

Documentation

Package items

template_documentation.category(frame) (function)
Template entrypoint for Template:Template category.
Parameter: frame Scribunto frame object (table)
Returns: Categories for the template or the doc page (string)
template_documentation.samples(frame) (function)
Template entrypoint for Template:Samples.
Parameter: frame Scribunto frame object (table)
Returns: Samples to display on the template page (string)
--- Does formatting of our template documentation, including
--  [[Template:Template category]] and [[Template:Samples]].
--  @module             template_documentation
--  @alias              p
--  <nowiki>
local p = {}

--  Package items.

--- Template entrypoint for [[Template:Template category]].
--  @function           p.category
--  @param              {table} frame Scribunto frame object
--  @returns            {string} Categories for the template or the doc page
function p.category(frame)
    local title = mw.title.getCurrentTitle()
    local arg = frame:getParent().args[1]
    if title.namespace == 10 then
        -- We're in template namespace
        if title.subpageText == 'doc' then
            -- We're on a documentation subpage
            return '[[Category:Template documentation]]'
        elseif arg and arg ~= '' then
        	return '[[Category:' .. arg .. ']]'
        end
    end
end

--- Template entrypoint for [[Template:Samples]].
--  @function           p.samples
--  @param              {table} frame Scribunto frame object
--  @returns            {string} Samples to display on the template page
function p.samples(frame)
    local ret = {}
    for _, value in ipairs(frame.args) do
    	if value == nil or value == '' then
    		break
    	end
        local sample = mw.text.trim(mw.text.unstrip(value))
        if mw.ustring.match(sample, '\n') then
            table.insert(ret, '<pre class="template-documentation__example">')
            table.insert(ret, sample)
            table.insert(ret, '</pre><p>gives...</p>')
        else
            table.insert(ret, '<p class="template-documentation__example"><code><nowiki>')
            table.insert(ret, sample)
            table.insert(ret, '</nowiki></code> gives...</p>')
        end
        table.insert(ret, mw.text.decode(sample))
        table.insert(ret, '\n')
    end
    return frame:preprocess(table.concat(ret)) .. '\n'
end

return p
--  </nowiki>