Aller au contenu

Module:Template documentation

De Wiki Undertale FR

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 = {}
    local args
    if frame.args.documentation then
        -- We can't use the template for purposes of documenting
        -- that template due to the template loop...
        args = frame.args
    else
        args = frame:getParent().args
    end
    for _, value in ipairs(args) do
        local sample = mw.text.trim(mw.text.decode(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, sample)
        table.insert(ret, '\n')
    end
    return frame:preprocess(table.concat(ret)) .. '\n'
end

return p
--  </nowiki>