跳转到内容

Module:Template documentation

来自Undertale Wiki

此模組用於格式化模板的說明文件。


--- 负责格式化本 Wiki 的模板文档,包括
--  [[Template:Template category]] 与 [[Template:Samples]]。
--  @module             template_documentation
--  @alias              p
--  <nowiki>
local p = {}

--  模块内容(功能项)。

--- [[Template:Template category]] 的模板入口函数。
--  @function           p.category
--  @param              {table} frame Scribunto 框架对象
--  @returns            {string} 返回该模板或其文档页应加入的分类字符串
function p.category(frame)
    local title = mw.title.getCurrentTitle()
    local arg = frame:getParent().args[1]
    if title.namespace == 10 then
        -- 当前位于“模板”命名空间
        if title.subpageText == 'doc' then
            -- 当前位于模板的文档子页面
            return '[[Category:Template documentation]]'
        elseif arg and arg ~= '' then
        	return '[[Category:' .. arg .. ']]'
        end
    end
end

--- [[Template:Samples]] 的模板入口函数。
--  @function           p.samples
--  @param              {table} frame Scribunto 框架对象
--  @returns            {string} 返回要在模板页面显示的示例内容
function p.samples(frame)
    local ret = {}
    local args
    if frame.args.documentation then
        -- 由于会产生模板自我调用的循环,
        -- 无法在用于记录该模板文档的场合直接使用该模板......
        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, '<p>输入 </p>')
            table.insert(ret, '<pre class="template-documentation__example">')
            table.insert(ret, sample)
            table.insert(ret, '</pre><p> 的效果:</p>')
        else
            table.insert(ret, '<p>输入 <code class="template-documentation__example"><nowiki>')
            table.insert(ret, sample)
            table.insert(ret, '</nowiki></code> 的效果:</p>')
        end
        table.insert(ret, sample)
        table.insert(ret, '\n')
    end
    return frame:preprocess(table.concat(ret)) .. '\n'
end

return p
--  </nowiki>