Modul:Vorlage:PersonZelle

Die Dokumentation für dieses Modul kann unter Modul:Vorlage:PersonZelle/Doku erstellt werden

local Serial = "2018-01-04"
--[=[
Vorlage:PersonZelle
]=]



local Category = "Wikipedia:Vorlagenfehler/Vorlage:PersonZelle"



local function fetch( access )
    -- Fetch library module
    -- Parameter:
    --     access  -- string, with library name
    -- Returns:
    --     table with library
    -- Throws:
    --     error, module not found
    local lucky, r = pcall( require,  "Module:" .. access )
    if type( r ) == "table" then
        r = r[ access ]()
    else
        error( r )
    end
    return r
end -- fetch()



local function fault( alert, frame )
    -- Format error message
    -- Parameter:
    --     alert  -- string, with complaint
    --     frame  -- object, or nil
    -- Returns string
    local TemplUtl = fetch( "TemplUtl" )
    local staff    = "editoronly"
    local live
    if mw.title.getCurrentTitle().namespace ~= 0 then
        live  = true
        staff = false
    end
    return string.format( " %s [[Category:%s]]",
                          TemplUtl.failure( alert,
                                            live,
                                            staff,
                                            frame ),
                          Category )
end -- fault()



local function fiat( arglist, frame )
    -- Do it.
    -- Parameter:
    --     arglist  -- table, with parameters
    --     frame    -- object, or nil
    -- Returns:
    --     string
    local params = { [ 1 ] = false,
                     [ 2 ] = false,
                     [ 3 ] = false,
                     k     = false,
                     nl    = false,
                     fett  = false }
    local r, s, show, unknown
    for k, v in pairs( arglist ) do
        if type( params[ k ] ) == "boolean" then
            v = mw.text.trim( v )
            if v ~= "" then
                params[ k ] = v
            end
        else
            if unknown then
                unknown = string.format( "%s, %s", unknown, k )
            else
                unknown = k
            end
        end
    end -- for k, v in arglist
    show = params[ 2 ]
    if show then
        local slot = params[ 3 ]
        local sort = show
        local strange
        s = show
        if params.nl == "0" then
            params.nl = false
        end
        if params.fett == "0" then
            params.fett = false
        end
        if params[ 1 ] then
            show = string.format( "%s %s", params[ 1 ], show )
            sort = string.format( "%s, %s", sort, params[ 1 ] )
        end
        if params.k then
            if params.nl then
                strange = "Widerspruch k=... und nl=1"
            elseif slot then
                strange = "Widerspruch k=... und 3=..."
            else
                slot = string.format( "%s (%s)", show, params.k )
            end
            sort = string.format( "%s @%s", sort, params.k )
        end
        if not params.nl then
            if slot then
                show = string.format( "%s|%s", slot, show )
            end
            show = string.format( "[[%s]]", show )
        end
        if params.fett then
            show = tostring( mw.html.create( "span" )
                                    :css( "font-weight", "bold" )
                                    :wikitext( show ) )
        end
        if sort:find( "[^ -~]" ) then
            local Sort = fetch( "Sort" )
            sort = Sort.lex( sort, "latin" )
        end
        if params[ 1 ]  or  sort ~= params[ 2 ] then
            r = string.format( " data-sort-value=\"%s\"| %s",
                               sort, show )
        else
            r = "| " .. show
        end
        if strange then
            r = r .. fault( strange, frame )
        end
    else
        r = "|"
        s = "Pflichtparameter (Familienname) fehlt"
        if params[ 1 ] then
            r = r .. params[ 1 ]
            s = string.format( "%s – %s", s, params[ 1 ] )
        end
        r = r .. fault( s, frame )
    end
    if unknown then
        r = r  ..  fault( "Unbekannter Parametername(n) " .. unknown,
                          frame )
    end
    return r
end -- fiat()



-- Export
local p = { }

function p.test( arglist )
    return fiat( arglist )
end

function p.f( frame )
    local lucky, r = pcall( fiat, frame:getParent().args, frame )
    if not lucky then
        r = fault( r )
    end
    return r
end

return p