打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:Documentation

来自电棍ottowiki
OctoberSama留言 | 贡献2025年6月17日 (二) 22:03的版本

package.lua第80行Lua错误:module 'Module:Message box' not found

-- This module implements {{documentation}}.

-- Get required modules.
local getArgs = require('Module:Arguments').getArgs
local messageBox = require('Module:Message box')

-- Get the config table.
local cfg = mw.loadData('Module:Documentation/config')

local p = {}

-- Often-used functions.
local ugsub = mw.ustring.gsub

-------------------------------------------------------------------------------
-- Helper functions
-------------------------------------------------------------------------------

local function message(cfgKey, valArray, expectType)
    local msg = cfg[cfgKey]
    expectType = expectType or 'string'
    if type(msg) ~= expectType then
        error('message: type error in message cfg.' .. cfgKey .. ' (' ..
            expectType .. ' expected, got ' .. type(msg) .. ')', 2)
    end
    if not valArray then
        return msg
    end

    local function getMessageVal(match)
        match = tonumber(match)
        return valArray[match] or error('message: no value found for key $' .. match .. ' in message cfg.' .. cfgKey, 4)
    end

    local ret = ugsub(msg, '$([1-9][0-9]*)', getMessageVal)
    return ret
end

p.message = message

local function makeWikilink(page, display)
    if display then
        return mw.ustring.format('[[%s|%s]]', page, display)
    else
        return mw.ustring.format('[[%s]]', page)
    end
end

p.makeWikilink = makeWikilink

local function makeCategoryLink(cat, sort)
    local catns = mw.site.namespaces[14].name
    return makeWikilink(catns .. ':' .. cat, sort)
end

p.makeCategoryLink = makeCategoryLink

local function makeUrlLink(url, display)
    return mw.ustring.format('[%s %s]', url, display)
end

p.makeUrlLink = makeUrlLink

local function makeToolbar(...)
    local ret = {}
    local lim = select('#', ...)
    if lim < 1 then
        return nil
    end
    for i = 1, lim do
        ret[#ret + 1] = select(i, ...)
    end
    return '<small style="font-style: normal;">(' .. table.concat(ret, ' &#124; ') .. ')</small>'
end

p.makeToolbar = makeToolbar

-------------------------------------------------------------------------------
-- Argument processing
-------------------------------------------------------------------------------

local function makeInvokeFunc(funcName)
    return function (frame)
        local args = getArgs(frame, {
            valueFunc = function (key, value)
                if type(value) == 'string' then
                    value = value:match('^%s*(.-)%s*$')
                    if key == 'heading' or value ~= '' then
                        return value
                    else
                        return nil
                    end
                else
                    return value
                end
            end
        })
        return p[funcName](args)
    end
end

-------------------------------------------------------------------------------
-- Entry points
-------------------------------------------------------------------------------

function p.nonexistent(frame)
    if mw.title.getCurrentTitle().subpageText == 'testcases' then
        return frame:expandTemplate{title = 'module test cases notice'}
    else
        return p.main(frame)
    end
end

-------------------------------------------------------------------------------
-- Main function
-------------------------------------------------------------------------------

p.main = makeInvokeFunc('_main')

function p._main(args)
    local env = p.getEnvironment(args)
    local root = mw.html.create()
    root
        :wikitext(p._getModuleWikitext(args, env))
        :wikitext(p.protectionTemplate(env))
        :tag('div')
            :attr('id', message('main-div-id'))
            :addClass(message('main-div-classes'))
            :newline()
            :wikitext(p._startBox(args, env))
            :wikitext(p._content(args, env))
            :tag('div')
                :css('clear', 'both')
                :newline()
                :done()
            :done()
        :wikitext(p._endBox(args, env))
        :wikitext(p.addTrackingCategories(env))
    return tostring(root)
end

-------------------------------------------------------------------------------
-- Environment settings
-------------------------------------------------------------------------------

function p.getEnvironment(args)
    local env, envFuncs = {}, {}
    setmetatable(env, {
        __index = function (t, key)
            local envFunc = envFuncs[key]
            if envFunc then
                local success, val = pcall(envFunc)
                if success then
                    env[key] = val
                    return val
                end
            end
            return nil
        end
    })

    function envFuncs.title()
        local titleArg = args.page
        if titleArg then
            return mw.title.new(titleArg)
        else
            return mw.title.getCurrentTitle()
        end
    end

    function envFuncs.templateTitle()
        local subjectSpace = env.subjectSpace
        local title = env.title
        local subpage = title.subpageText
        if subpage == message('sandbox-subpage') or subpage == message('testcases-subpage') then
            return mw.title.makeTitle(subjectSpace, title.baseText)
        else
            return mw.title.makeTitle(subjectSpace, title.text)
        end
    end

    function envFuncs.docTitle()
        local title = env.title
        local docname = args[1]
        local docpage
        if docname then
            docpage = docname
        else
            docpage = env.docpageBase .. '/' .. message('doc-subpage')
        end
        return mw.title.new(docpage)
    end

    function envFuncs.sandboxTitle()
        return mw.title.new(env.docpageBase .. '/' .. message('sandbox-subpage'))
    end

    function envFuncs.testcasesTitle()
        return mw.title.new(env.docpageBase .. '/' .. message('testcases-subpage'))
    end

    function envFuncs.printTitle()
        return env.templateTitle:subPageTitle(message('print-subpage'))
    end

    function envFuncs.protectionLevels()
        return env.title.protectionLevels
    end

    function envFuncs.subjectSpace()
        return mw.site.namespaces[env.title.namespace].subject.id
    end

    function envFuncs.docSpace()
        return mw.site.namespaces[env.title.namespace].subject.id
    end

    function envFuncs.docpageBase()
        local title = env.title
        local subject = mw.site.namespaces[title.namespace].subject.id
        if subject == title.namespace then
            return title.prefixedText
        else
            return title.prefixedText:match('^(.-)/')
        end
    end

    function envFuncs.compareUrl()
        if env.sandboxTitle and env.sandboxTitle.exists then
            return mw.title.makeTitle(4, 'Special:ComparePages'):fullUrl{
                page = env.sandboxTitle.prefixedText,
                target = env.templateTitle.prefixedText
            }
        else
            return nil
        end
    end

    return env
end

-------------------------------------------------------------------------------
-- End box (link box) construction
-------------------------------------------------------------------------------

function p._endBox(args, env)
    -- Get environment data.
    env = env or p.getEnvironment(args)
    local subjectSpace = env.subjectSpace
    local docTitle = env.docTitle
    if not subjectSpace or not docTitle then
        return nil
    end

    -- Decide if end box should be shown.
    local linkBox = args['link box']
    if linkBox == 'off'
            or not (
                docTitle.exists
                or subjectSpace == 2
                or subjectSpace == 828
                or subjectSpace == 10
            )
    then
        return nil
    end

    -- Assemble the arguments for {{fmbox}}.
    local fmargs = {}
    fmargs.id = message('fmbox-id')
    fmargs.image = 'none'
    fmargs.style = message('fmbox-style')
    fmargs.textstyle = message('fmbox-textstyle')

    -- Assemble the fmbox text field.
    local text = ''
    if linkBox then
        text = text .. linkBox
    else
        if subjectSpace == 2 or subjectSpace == 10 or subjectSpace == 828 then
            if not args.content and not args[1] then
                text = text .. (p.makeCategoriesBlurb(args, env) or '')
            end
        end
    end
    fmargs.text = text

    return messageBox.main('fmbox', fmargs)
end

function p.makeCategoriesBlurb(args, env)
    local docTitle = env.docTitle
    if not docTitle then
        return nil
    end
    local docPathLink = makeWikilink(docTitle.prefixedText, message('doc-link-display'))
    return message('add-categories-blurb', {docPathLink})
end

-- Tracking categories
function p.addTrackingCategories(env)
    local title = env.title
    local subjectSpace = env.subjectSpace
    if not title or not subjectSpace then
        return nil
    end
    local subpage = title.subpageText
    local ret = ''
    if message('display-strange-usage-category', nil, 'boolean')
            and (
                subpage == message('doc-subpage')
                or (subjectSpace ~= 828 and subpage == message('testcases-subpage'))
            )
    then
        ret = ret .. p.makeCategoryLink(message('strange-usage-category'))
    end
    return ret
end

return p