Modul:Vorlage:TOC Präfixindex

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus


--[==[
[[Vorlage:TOC Präfixindex]]
]==]
local Classes = { "hintergrundfarbe1",
                  "rahmenfarbe1" }
local CSS     = { ["border-style"] = "solid",
                  ["border-width"] = "1px",
                  ["display"]      = "table",
                  ["padding"]      = "0.5em" }



local furnish = function ( arglist, frame )
    -- Create
    -- Precondition:
    --     arglist    -- table, with specs
    --     frame      -- object
    -- Postcondition:
    --     Returns string
    local r = mw.html.create( "div" )
    r:addClass( "noprint" )
    if arglist.sub then
        local got   = mw.text.split( arglist.sub, "%s+" )
        local paths = { }
        local s
        for i = 1, #got do
            s = got[ i ]
            if not s:find( "[%%<>%[%]|]" )  and
               not paths[ s ] then
                arglist.paths = arglist.paths  or  { }
                table.insert( arglist.paths, s )
                paths[ s ] = true
            end
        end -- for i
    end
    if arglist.prefix and arglist.paths then
        local url = { }
        local s, sub
        r:addClass( "navigation-not-searchable" )
         :css( CSS )
        if Classes then
            for i = 1, #Classes do
                r:addClass( Classes[ i ] )
            end -- for i
        end
        if arglist.class then
            r:addClass( arglist.class )
        end
        if arglist.style then
            r:cssText( arglist.style )
        end
        r:attr( "role", "navigation" )
        if arglist.id then
            r:attr( "id", arglist.id )
        end
        if arglist.label then
            s = arglist.label
            if s == "-" then
                s = false
            end
        else
            s = mw.message.new( "toc" ):plain()
        end
        if s then
            local e = mw.html.create( "span" )
            e:css( "font-weight", "bold" )
             :wikitext( s .. ":" )
            r:newline()
             :node( e )
        end
        table.insert( url, false )
        if arglist.hideredirects then
            table.insert( url, "hideredirects=1" )
        end
        for i = 1, #arglist.paths do
            sub      = arglist.paths[ i ]
            url[ 1 ] = string.format( "%s/%s%s",
                                      "Special:prefixindex",
                                      arglist.prefix,
                                      sub )
            s        = frame:callParserFunction( "canonicalurl", url )
            r:newline()
             :wikitext( string.format( "[%s %s]", s, sub ) )
        end -- for i
        r:newline()
    else
        r:addClass( "error" )
         :wikitext( "TOC prefix" )
    end
    return tostring( r )
end -- furnish()



-- Export
local p = { }

p.f = function ( frame )
    local params = { "prefix",
                     "sub",
                     "redirects",
                     "label",
                     "class",
                     "style",
                     "id" }
    local values = { }
    local seek, set
    for i = 1, #params do
        seek = params[ i ]
        set  = frame:getParent().args[ seek ]
        if set  and  set ~= "" then
            values[ seek ] = set
        end
    end -- for i
    if values.redirects == "0" then
       values.hideredirects = true
    else    
       values.hideredirects = false
    end
    return furnish( values, frame )
end    -- p.f

return p