Modul:Vorlage:LuaModuleDoc

Aus skandinavien-wiki.net
Version vom 24. April 2013, 21:01 Uhr von wikipedia>PerfektesChaos (Setup)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Die Dokumentation für dieses Modul kann unter Modul:Vorlage:LuaModuleDoc/doc erstellt werden

--[=[ test 2013-04-23
Support {{LuaModuleDoc}}
* nav()
require: LuaWiki
]=]



local LuaWiki = require( "Module:LuaWiki" )



local function navError( say, specific )
    -- Return error message, evaluate page .pageErr
    -- Precondition:
    --     say       -- string; message key
    --     specific  -- string, optional; additional information
    -- Uses:
    --     LuaWiki.getArg()
    --     LuaWiki.transclude()
    local show = LuaWiki.getArg( "pageErr" )
    local r
    if type( show ) == "string" then
        local pars = { say }
        if type( specific ) == "string" then
            table.insert( pars, specific )
        end
        r = LuaWiki.transclude( show, pars )
    else
        r = "<span class='error'>arg 'pageErr' missing</span>"
    end
    return r
end -- navError()



local function navInclude( collect, swift, super, ns, nsDocs )
    -- Include external content into page
    -- Precondition:
    --     collect  -- table; with arguments for pageNav transclusion
    --                 1: basic module name
    --                 2: number of language links  +  1
    --                 3...: language link codes
    --     swift    -- string; pageNav argument
    --     super    -- string or false
    --                 string: full page name of basic documentation
    --                         terminated by slash; then
    --                         include first existing language version
    --                         and brief template documentation
    --     ns       -- number; current namespace
    --     nsDocs   -- number; namespace for doc
    --     args.pageNav defined
    -- Uses:
    --     LuaWiki.getArg()
    --     LuaWiki.transclude()
    --     LuaWiki.getVariable()
    --     LuaWiki.isExisting()
    --     navError()
    local r = LuaWiki.transclude( swift, collect )
    local s
    if not LuaWiki.isExisting( LuaWiki.getVariable( "TALKPAGENAME" ) ) then
        if super then
            s = "NoTalkCentral"
        elseif ns == nsDocs + 1 then
            s = false
        else
            s = "NoTalkRedir"
        end
        if s then
            r = r .. navError( s )
        end
    end
    if super then
        local i
        local loop = true
        for i = 3, #collect do
            if loop then
                s = super .. collect[ i ]
                if LuaWiki.isExisting( s ) then
                    r    = r .. LuaWiki.transclude( s )
                    loop = false
                end
            end
        end -- for i
        s = LuaWiki.getArg( "pageTemplateInsert" )
        if LuaWiki.isExisting( s ) then
           if not LuaWiki.getArg( "noHint", false ) then
               r = r .. LuaWiki.transclude( s,  { collect[ 1 ] } )
            end
        end
    end
    return r
end -- navInclude()



local function navLang( suite, collect, lazy )
    -- Append languages from string to collection
    -- Precondition:
    --     suite    -- string; space separated source
    --     collect  -- table; to be extended
    --                 every element: { langCode, lazy }
    --     lazy     -- true if only existing page is to be linked
    if type( suite ) == "string" then
        local raw = mw.text.split( suite, "%s+" )
        local e, i, j, s
        for i = 1, #raw do
            s = raw[ i ]
            if #s > 1 then
                for j = 1, #collect do
                    if s then
                        e = collect[ j ]
                        if e[ 1 ] == s then
                            s = false
                            if not lazy then
                                e[ 2 ] = false
                            end
                        end
                    end
                end -- for j
                if s then
                    table.insert( collect,  { s, lazy } )
                end
            end
        end -- for i
    end
end -- navLang()



local function navLangs( script, super )
    -- Analyze languages
    -- Precondition:
    --     current page is supposed to transclude LuaModuleDoc
    --     script  -- string; basic module name
    --     super   -- string; central documentation root
    -- Return:
    --     table:  args for navigation templates
    --             [1] basic module name
    --             [2] number of language codes  +  1
    --             [3] first language code
    --             [4] second language code
    --             ... list of further language codes
    -- Uses:
    --     LuaWiki.getArg()
    --     navLang()
    --     LuaWiki.isExisting()
    --     navError()
    local e, i, s
    local r = { }
    local specified = LuaWiki.getArg( "langsRequest", false )
    navLang( LuaWiki.getArg( "langsDefault" ),
             r,
             ( type( specified ) == "string" ) )
    navLang( specified, r, false )
    if #r < 1 then
        r = { { "en", false } }
    end
    for i = #r, 1, -1 do
        e = r[ i ]
        s = e[ 1 ]
        if e[ 2 ] then
            if not LuaWiki.isExisting( super .. "/" .. s ) then
                s = false
            end
        end
        if s then
            r[ i ] = s
        else
            table.remove( r, i )
        end
    end -- for i
    table.insert( r,  1,  script )
    table.insert( r,  2,  tostring( #r ) )
    return r
end -- navLangs()



local function navPage( lead, ns, nsDocs )
    -- Return navigation text; analyze page location
    -- Precondition:
    --     current namespace will support LuaModuleDoc
    --     lead    -- true: Module: namespace;   false: text namespace
    --     ns      -- number; current namespace
    --     nsDocs  -- number; namespace for doc
    -- Uses:
    --     LuaWiki.getArg()
    --     LuaWiki.getVariable()
    --     navError()
    --     navLangs()
    --     navInclude()
    local r
    local start = LuaWiki.getArg( "pageDocRoot" )
    if type( start ) == "string" then
        local light  = false
        local script
        local story  = LuaWiki.getVariable( "PAGENAME" ) .. "/"
        if lead then
            -- any template transclusion happens on a doc page
            script = mw.ustring.match( story, "^([^/]+)/" )
        else
            local sub = "/([^/]+)/(.*)$"
            script, sub = mw.ustring.match( story,  "^" .. start .. sub )
            if type( script ) == "string" then
                light = ( #sub > 0 )
            else
                r      = navError( "BadPage" ) .. story
                script = false
            end
        end
        if script then
            local swift = LuaWiki.getArg( "pageNav" )
            if type( swift ) == "string" then
                -- nsDocs is valid, since it equals ns
                local super = mw.site.namespaces[ nsDocs ].name
                super = super .. ":" .. start
                local collect = navLangs( script, super )
                if ns == nsDocs and
                   story == start .. "/" .. script .. "/" then
                    super = super .. "/" .. script .. "/"
                else
                    super = false
                end
                r = navInclude( collect, swift, super, ns, nsDocs )
            else
                r = navError( "configMissing", "pageNav" )
            end
        end
    else
        r = navError( "configMissing", "pageDocRoot" )
    end
    return r
end -- navPage()



local function navigation()
    -- Start execution; return navigation text; analyze namespace
    -- Uses:
    --     LuaWiki.initVariables()
    --     LuaWiki.getArg()
    --     LuaWiki.getVariable()
    --     navPage()
    --     navError()
    local r
    LuaWiki.initVariables( { { "NAMESPACENUMBER", true },
                               "PAGENAME",
                               "TALKPAGENAME" } )
    local nsDocs =  LuaWiki.getArg( "nsDocs" )
    if nsDocs then
        local ns   = LuaWiki.getVariable( "NAMESPACENUMBER" )
        local lead = ( ns == mw.site.namespaces.Module.id )
        nsDocs = tonumber( nsDocs )
        if lead or  ns == nsDocs  or  ns == nsDocs + 1 then
            r = navPage( lead, ns, nsDocs )
        else
            r = navError( "BadNamespace" )
        end
    else
        r = navError( "configMissing", "nsDocs" )
    end
    return  r
end -- nav()



-- Provide template access

local p = {}

function p.nav( frame )
    -- Uses:
    --     >  LuaWiki
    --     LuaWiki.setFrame()
    --     navigation()
    local r
    if type( LuaWiki ) == "table" then
        LuaWiki.setFrame( frame )
        r = navigation() or ""
    else
        r = "<span class='error'>required 'Module:LuaWiki' not found</span>"
    end
    return r
end

return p