Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch English

Modul: Dokumentation

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

Dies ist die (produktive) Mutterversion eines global benutzten Lua-Moduls.
Wenn die serial-Information nicht übereinstimmt, müsste eine Kopie hiervon in das lokale Wiki geschrieben werden.
Versionsbezeichnung auf WikiData: 2023-10-25

local HorizontalList = { suite  = "HorizontalList",
                         serial = "2023-10-25",
                         item   = 100938243 }
--[=[
Horizontal list of items by HTML/CSS list
]=]



local CSS      = { classesBlock  = { },
                   classesNowrap = { },
                   classesPrefix = { },
                   classesSep    = { } }
local Failsafe = HorizontalList



local fix = function ( at )
    for k, v in pairs( CSS.classesNowrap ) do
        at:addClass( v )
    end -- for k, v
end -- fix()



HorizontalList.f = function ( all, ahead, altogether, access, alien,
                              apart, frame )
    -- Generate horizontal list from wikitext
    -- Parameter:
    --     all         -- string, with wikitext
    --                    each line starting with one of * or #
    --     ahead       -- string, to prefix if desired
    --     altogether  -- true, if nowrap around each regular item
    --     access      -- string, with fragment, if desired
    --     alien       -- string, with language code, if desired
    --     apart       -- string, with separator, if desired
    --     frame       -- object, if available
    -- Returns string
    local r
    if type( all ) == "string" then
        local story = mw.text.trim( all )
        local s     = story:sub( 1, 1 )
        if s == "#" or s == "*" then
            local list = ( s == "#" )
            local items, got
            if list then
                s = "\n%s*#%s*"
            else
                s = "\n%s*%*%s*"
            end
            items = mw.text.split( story:sub( 2 ),  s )
            for i = 1, #items do
                s = mw.text.trim( items[ i ] )
                if s ~= "" then
                    got = got  or  { }
                    table.insert( got, s )
                end
            end -- for i
            if got then
                local start
                if type( ahead ) == "string"  and  ahead ~= "" then
                    start = ahead
                end
                r = HorizontalList.fiat( got,
                                         list,
                                         start,
                                         altogether,
                                         access,
                                         alien,
                                         apart,
                                         frame )
            else
                r = ""
            end
        else
            r = story
        end
    elseif all then
        r = tostring( all )
    else
        r = ""
    end
    return r
end -- HorizontalList.f()



HorizontalList.fiat = function ( all, advance, ahead, altogether,
                                 access, alien, apart, frame )
    -- Generate horizontal list from item sequence
    -- Parameter:
    --     all         -- table, with sequence of items
    --                    each item is a string or a mw.html object
    --     ahead       -- string, to prefix if desired
    --     advance     -- true, if ordered list requested
    --     altogether  -- true, if nowrap around each item
    --     access      -- string, with fragment, if desired
    --     alien       -- string, with language code, if desired
    --     apart       -- string, with separator, if desired
    --     frame       -- object, if available
    -- Returns string
    local r
    if type( all ) == "table" then
        local e
        if #all > 1 then
            local es, ou, s
            if advance then
                s = "ol"
            else
                s = "ul"
            end
            ou = mw.html.create( s )
            for k, v in pairs( CSS.classesBlock ) do
                ou:addClass( v )
            end -- for k, v
            if CSS.style then
                ou:cssText( CSS.style )
            end
            if CSS.css then
                ou:css( CSS.css )
            end
            if type( access ) == "string" then
                s = mw.text.trim( access )
                if s ~= "" then
                    ou:attr( "id", s )
                end
            end
            if alien then
                s = mw.text.trim( alien )
                if s ~= "" then
                    ou:attr( "lang", s )
                end
            end
            if type( apart ) == "string" then
                es = mw.html.create( "span" )
                            :node( mw.html.create( "span" )
                                          :attr( "aria-hidden", "true" )
                                          :wikitext( apart ) )
                for k, v in pairs( CSS.classesSep ) do
                    es:addClass( v )
                end -- for k, v
            end
            if type( ahead ) == "string" then
                e = mw.html.create( "li" )
                           :wikitext( ahead )
                for k, v in pairs( CSS.classesPrefix ) do
                    e:addClass( v )
                end -- for k, v
                ou:newline()
                  :node( e )
            end
            for i = 1, #all do
                e = mw.html.create( "li" )
                s = all[ i ]
                if type( s ) == "table" then
                    e:node( s )
                else
                    e:wikitext( tostring( s ) )
                end
                if CSS.styleItem then
                    e:cssText( CSS.styleItem )
                end
                if es  and  i < #all then
                    e:node( es )
                end
                if altogether then
                    fix( e )
                end
                ou:newline()
                  :node( e )
            end -- for i
            if type( frame ) ~= "table" then
                frame = mw.getCurrentFrame()
            end
            if CSS.styles then
                r = frame:extensionTag( "templatestyles",
                                        nil,
                                        { src = CSS.styles } )
            else
                r = ""
            end
            r = r .. tostring( ou )
        else
            r = all[ 1 ]
            if altogether then
                if type( r ) ~= "table" then
                    r = mw.html.create( "span" )
                               :wikitext( tostring( r ) )

                end
                fix( r )
            end
            r = tostring( r )
            if type( ahead ) == "string" then
                r = string.format( "%s %s", ahead, r )
            end
        end
    end
    return r or ""
end -- HorizontalList.fiat()



HorizontalList.first = function ( arglist )
    -- Configure CSS environment
    -- Parameter:
    --     arglist  -- table, with optional components
    --                 styleItem     -- style per item
    --                                  -- string
    --                 styles        -- templatestyles page
    --                                  -- string, with name
    --                                  -- table, with title object
    --                 classesBlock  -- class(es) for block element
    --                                  -- string, with class(es)
    --                                  -- table, with particular mapping
    --                 classesNowrap -- class(es) for nowrap element
    --                                  -- string, with class(es)
    --                                  -- table, with particular mapping
    --                 classesPrefix -- class(es) for prefix element
    --                                  -- string, with class(es)
    --                                  -- table, with particular mapping
    --                 classesSep    -- class(es) for separator element
    --                                  -- string, with class(es)
    --                                  -- table, with particular mapping
    if type( arglist ) == "table" then
        local s, val
        for k, v in pairs( CSS ) do
            if type( v ) == "table" then
                val = arglist[ k ]
                s   = type( val )
                if s == "string" then
                    s = mw.text.trim( val )
                    if s ~= "" then
                        table.insert( v, s )
                    end
                elseif s == "table" then
                    for kk, vv in pairs( val ) do
                        if type( vv ) == "string" then
                            s = mw.text.trim( vv )
                            if s == "" then
                                s = nil
                            end
                        else
                            s = nil
                        end
                        v[ kk ] = s
                    end -- for kk, vvv
                end
            end
        end -- for k, v
        val = arglist.styles
        s   = type( val )
        if s == "string" then
            s = mw.text.trim( val )
        elseif s == "table"  and
               type( val.prefixedText ) == "string"  and
               type( val.exists ) == "boolean"  and
               val.exists then
            s = val.prefixedText
        else
            s = false
        end
        if s  and  s:match( ".+:.+%.css$") then
            CSS.styles = s
        end
        val = arglist.style
        s   = type( val )
        if s == "string" then
            s = mw.text.trim( val )
            if s ~= "" then
                CSS.style = s
            end
        elseif s == "table" then
            CSS.css = val
        end
        val = arglist.styleItem
        if type( val ) == "string" then
            s = mw.text.trim( val )
            if s ~= "" then
                CSS.styleItem = s
            end
        end
    end
end -- HorizontalList.first()



Failsafe.failsafe = function ( atleast )
    -- Retrieve versioning and check for compliance
    -- Precondition:
    --     atleast  -- string, with required version
    --                         or "wikidata" or "~" or "@" or false
    -- Postcondition:
    --     Returns  string  -- with queried version/item, also if problem
    --              false   -- if appropriate
    -- 2020-08-17
    local since = atleast
    local last    = ( since == "~" )
    local linked  = ( since == "@" )
    local link    = ( since == "item" )
    local r
    if last  or  link  or  linked  or  since == "wikidata" then
        local item = Failsafe.item
        since = false
        if type( item ) == "number"  and  item > 0 then
            local suited = string.format( "Q%d", item )
            if link then
                r = suited
            else
                local entity = mw.wikibase.getEntity( suited )
                if type( entity ) == "table" then
                    local seek = Failsafe.serialProperty or "P348"
                    local vsn  = entity:formatPropertyValues( seek )
                    if type( vsn ) == "table"  and
                       type( vsn.value ) == "string"  and
                       vsn.value ~= "" then
                        if last  and  vsn.value == Failsafe.serial then
                            r = false
                        elseif linked then
                            if mw.title.getCurrentTitle().prefixedText
                               ==  mw.wikibase.getSitelink( suited ) then
                                r = false
                            else
                                r = suited
                            end
                        else
                            r = vsn.value
                        end
                    end
                end
            end
        end
    end
    if type( r ) == "nil" then
        if not since  or  since <= Failsafe.serial then
            r = Failsafe.serial
        else
            r = false
        end
    end
    return r
end -- Failsafe.failsafe()



-- Export
local p = { }

p.f = function ( frame )
    -- Template call
    HorizontalList.first( { styles        = frame.args.styles,
                            classesBlock  = frame.args.classesBlock,
                            classesNowrap = frame.args.classesNowrap,
                            classesPrefix = frame.args.classesPrefix,
                            classesSep    = frame.args.classesSep,
                            style         = frame.args.style,
                            styleItem     = frame.args.styleItem } )
    return HorizontalList.f( frame.args[ 1 ],
                             frame.args.prefix,
                             frame.args.nowrap == "1",
                             frame.args.id,
                             frame.args.lang,
                             frame.args.sep,
                             frame )
end -- p.f

p.failsafe = function ( frame )
    -- Versioning interface
    local s = type( frame )
    local since
    if s == "table" then
        since = frame.args[ 1 ]
    elseif s == "string" then
        since = frame
    end
    if since then
        since = mw.text.trim( since )
        if since == "" then
            since = false
        end
    end
    return Failsafe.failsafe( since )  or  ""
end -- p.failsafe

p.HorizontalList = function ()
    -- Module interface
    return HorizontalList
end

p.Auflistung = function ()
    -- Module interface @dewiki
    return HorizontalList
end

return p