Modul:Vorlage:LCCN

Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch English

Modul: Dokumentation

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


--[=[ 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;

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.