Modul:Vorlage:LCCN

Aus Fahrradmonteur
Wechseln zu: Navigation, Suche

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

--[=[ 2015-02-11
{{LCCN}}
Library of Congress Control Number
require: URIutil
]=]



local Cat = { start  = "Wikipedia:Vorlagenfehler/Parameter:LCCN",
              slash  = "Schrägstrich",
              slash1 = "Schrägstrich zu Beginn",
              space  = "Leerzeichen",
              serial = "Jahreszahl falsch",
              scream = "Unerlaubt"
}
local Config



local function factory( alert )
    -- Retrieve particular category
    --     alert  -- string, with subcategory key
    -- Returns string, with category
    if Config.errNS then
        local ns = mw.title.getCurrentTitle().namespace
        local st = type( Config.errNS )
        if st == "string" then
            local space  = string.format( ".*%%s%d%%s.*", ns )
            local spaces = string.format( " %s ", Config.errNS )
            if spaces:match( space ) then
                Config.errNS = false
            end
        elseif st == "table" then
            for i = 1, #Config.errNS do
                if Config.errNS[ i ] == ns then
                    Config.errNS = false
                    break    -- for i
                end
            end -- for i
        end
    end
    if Config.errNS then
        r = ""
    else
        r = string.format( "[[Category:%s/%s]]",
                           Cat.start, Cat[ alert ] )
    end
    return r
end -- factory()



local function fault( alert )
    -- Format message with class="error", visible for preview or editor
    --     alert  -- string, with message
    -- Returns message with markup
    local style, suppress
    if not Config.frame then
        Config.frame = mw.getCurrentFrame()
    end
    if Config.frame:preprocess( "{{REVISIONID}}" ) == "" then
        style    = ""
        suppress = ""
    else
        style    = " style='display:none'"
        suppress = "editoronly"
    end
    style = string.format( "<span class=\"error %s\"%s>%%s</span>",
                           suppress, style )
    return string.format( style, alert )
end -- fault()



local function fixLCCN()
    -- Format current as well as old requests of LCCN
    -- Returns appropriate string, with link and maintenance category
    local r = ""
    local s = Config.code
    local n, shift, y
    if type( Config.URIutil ) ~= "table" then
        local lucky
        lucky, Config.URIutil = pcall( require, "Module:URIutil" );
        if type( Config.URIutil ) == "table" then
            Config.URIutil = Config.URIutil.URIutil()
        else
            error( Config.URIutil, 0 );
        end
    end
    if s:match( "%S%s%S" ) then
        r = r .. factory( "space" )
    end
    s = s:gsub( "%s+", "" )
    if s:sub( 1, 1 ) == "/" then
        s = s:sub( 2 )
        r = r .. factory( "slash1" )
        if s:sub( 1, 1 ) == "/" then
            s = s:sub( 2 )
        end
    end
    y, n = s:match( "^(%d%d%d?%d?)/(%d+)$" )
    if y then
        s = string.format( "%s-%s", y, n )
        r = r .. factory( "slash" )
        y = tonumber( y )
        if y > 100  and  y < 2000 then
            r = r .. factory( "serial" )
        end
    end
    shift = Config.URIutil.linkLCCN( s, "-" )
    if shift == s then
        s = string.format( "%s <code>%s</code>",
                           fault( "Fehler in LCCN:" ),
                           Config.code )
        r = string.format( "%s%s%s", s, r, factory( "scream" ) )
    else
        r = string.format( "%s%s", shift, r )
    end
    r = "[[Library of Congress Control Number|LCCN]]&nbsp;" .. r
    return r
end -- fixLCCN()



-- Export
local p = {};

function p.fixLCCN( arglist )
    -- Format and link LCCN
    -- Precondition:
    --     arglist  -- table, with parameters
    --                 code     -- string, with LCCN
    --                 errNS    -- table or string, with no-cat NS
    --                 frame    -- object or nil
    --                 URIutil  -- table or nil, with Module:URIutil
    -- Uses:
    --     fixLCCN()
    local r
    if type( arglist ) == "table"  and
       type( arglist.code ) == "string" then
        local lucky
        lucky, r = pcall( fixLCCN )
    else
        r = false
    end
    return r
end -- .fixLCCN()



function p.f( frame )
    local pT = frame:getParent().args
    local r  = pT[ 1 ]
    if r then
        local lucky
        Config = { code  = r,
                   errNS = frame.args.errNS,
                   frame = frame }
        lucky, r = pcall( fixLCCN )
    else
        r = ""
    end
    return r
end -- .f()

return p;