Zum Inhalt springen

Modul:Vorlage:LCCN

Us der alemannische Wikipedia, der freie Dialäkt-Enzyklopedy

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

--[=[ 2015-01-01
{{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 function factory( alert )
    -- Retrieve particular category
    --     alert  -- string, with subcategory key
    -- Returns string, with category
    return string.format( "[[Category:%s/%s]]",
                           Cat.start, Cat[ alert ] )
end -- factory()



local function fault( alert, frame )
    -- Format message with class="error", visible for preview or editor
    --     alert  -- string, with message
    --     frame  -- object or nil
    -- Returns message with markup
    local style, suppress
    if not frame then
        frame = mw.getCurrentFrame()
    end
    if 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( access, frame, URIutil )
    -- Format current as well as old requests of LCCN
    --     access   -- string, with LCCN
    --     frame    -- object or nil
    --     URIutil  -- table or nil, with Module:URIutil library
    -- Returns appropriate string, with link and maintenance category
    local r = ""
    local s = access
    local n, shift, y
    if type( URIutil ) ~= "table" then
        local lucky
        lucky, URIutil = pcall( require, "Module:URIutil" );
        if type( URIutil ) == "table" then
            URIutil = URIutil.URIutil()
        else
            error( 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 = URIutil.linkLCCN( s, "-" )
    if shift == s then
        s = string.format( "%s <code>%s</code>",
                           fault( "Fehler in LCCN:", frame ),
                           access )
        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( access, URIutil, frame )
    -- Format and link LCCN
    -- Precondition:
    --     access   -- string, with LCCN
    --     URIutil  -- table or nil, with Module:URIutil library
    --     frame    -- object or nil
    -- Uses:
    --     fixLCCN()
    return fixLCCN( access, frame, URIutil )
end -- .fixLCCN()



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

return p;