Modul:TemplatePar: Unterschied zwischen den Versionen

Aus FreeWiki
Wechseln zu: Navigation, Suche
te>PerfektesChaos
(update)
te>PerfektesChaos
(update)
Zeile 1: Zeile 1:
--[=[ TemplatePar 2013-05-05
+
--[=[ TemplatePar 2013-05-06
 
Template parameter utility
 
Template parameter utility
 
* check
 
* check
 
* count
 
* count
 
* valid
 
* valid
 +
* verify
 
* TemplatePar()
 
* TemplatePar()
 
]=]
 
]=]
Zeile 17: Zeile 18:
 
     dupOpt      = "#invoke:TemplatePar * repeated optional parameter",
 
     dupOpt      = "#invoke:TemplatePar * repeated optional parameter",
 
     dupRule    = "#invoke:TemplatePar * parameter conflict key/pattern",
 
     dupRule    = "#invoke:TemplatePar * parameter conflict key/pattern",
     empty      = "Error in template: undefined value for mandatory",
+
     empty      = "Error in template * undefined value for mandatory",
     invalid    = "Error in template: invalid parameter",
+
     invalid    = "Error in template * invalid parameter",
 
     invalidPar  = "#invoke:TemplatePar * invalid parameter",
 
     invalidPar  = "#invoke:TemplatePar * invalid parameter",
 
     minmax      = "#invoke:TemplatePar * min > max",
 
     minmax      = "#invoke:TemplatePar * min > max",
 +
    noErrorCat  = "#invoke:TemplatePar * noError and missing category",
 
     noname      = "#invoke:TemplatePar * missing parameter name",
 
     noname      = "#invoke:TemplatePar * missing parameter name",
     tooLong    = "Error in template: parameter too long",
+
     tooLong    = "Error in template * parameter too long",
     tooShort    = "Error in template: parameter too short",
+
     tooShort    = "Error in template * parameter too short",
     undefined  = "Error in template: mandatory parameter missing",
+
     undefined  = "Error in template * mandatory parameter missing",
     unknown    = "Error in template: unknown parameter name",
+
     unknown    = "Error in template * unknown parameter name",
 
     unknownRule = "#invoke:TemplatePar * unknown rule"
 
     unknownRule = "#invoke:TemplatePar * unknown rule"
 
}
 
}
Zeile 32: Zeile 34:
 
     dupOpt      = "#invoke:TemplatePar * Optionsparameter wiederholt",
 
     dupOpt      = "#invoke:TemplatePar * Optionsparameter wiederholt",
 
     dupRule    = "#invoke:TemplatePar * Parameterkonflikt key/pattern",
 
     dupRule    = "#invoke:TemplatePar * Parameterkonflikt key/pattern",
     empty      = "Fehler bei Vorlage: Pflichtparameter ohne Wert",
+
     empty      = "Fehler bei Vorlage * Pflichtparameter ohne Wert",
     invalid    = "Fehler bei Vorlage: Parameter ungültig",
+
     invalid    = "Fehler bei Vorlage * Parameter ungültig",
 
     invalidPar  = "#invoke:TemplatePar * Ungültiger Parameter",
 
     invalidPar  = "#invoke:TemplatePar * Ungültiger Parameter",
 
     minmax      = "#invoke:TemplatePar * min > max",
 
     minmax      = "#invoke:TemplatePar * min > max",
 +
    noErrorCat  = "#invoke:TemplatePar * noError und keine Kategorie",
 
     noname      = "#invoke:TemplatePar * Parametername nicht angegeben",
 
     noname      = "#invoke:TemplatePar * Parametername nicht angegeben",
     tooLong    = "Fehler bei Vorlage: Parameter zu lang",
+
     tooLong    = "Fehler bei Vorlage * Parameter zu lang",
     tooShort    = "Fehler bei Vorlage: Parameter zu kurz",
+
     tooShort    = "Fehler bei Vorlage * Parameter zu kurz",
     undefined  = "Fehler bei Vorlage: Pflichtparameter fehlt",
+
     undefined  = "Fehler bei Vorlage * Pflichtparameter fehlt",
     unknown    = "Fehler bei Vorlage: Parametername unbekannt",
+
     unknown    = "Fehler bei Vorlage * Parametername unbekannt",
 
     unknownRule = "#invoke:TemplatePar * Unbekannte Regel"
 
     unknownRule = "#invoke:TemplatePar * Unbekannte Regel"
 
}
 
}
Zeile 164: Zeile 167:
 
     end
 
     end
 
     if suspect then
 
     if suspect then
         r = r .. " " .. suspect
+
         r = r .. ": " .. suspect
 
     end
 
     end
 
     return r
 
     return r
Zeile 177: Zeile 180:
 
     --    key    -- string or number; to be appended
 
     --    key    -- string or number; to be appended
 
     -- Postcondition:
 
     -- Postcondition:
     --   Return string; extended
+
     --     Return string; extended
 
     local r
 
     local r
 
     local s
 
     local s
Zeile 201: Zeile 204:
 
     --    needle    -- any; identifier
 
     --    needle    -- any; identifier
 
     -- Postcondition:
 
     -- Postcondition:
     --   Return true iff found
+
     --     Return true iff found
 
     local k, v
 
     local k, v
 
     for k, v in pairs( haystack ) do
 
     for k, v in pairs( haystack ) do
Zeile 213: Zeile 216:
  
  
local function fetch()
+
local function fetch( low )
 
     -- Return regular table with all template transclusion parameters
 
     -- Return regular table with all template transclusion parameters
 +
    -- Precondition:
 +
    --    low  -- true: template transclusion;  false: #invoke
 
     -- Postcondition:
 
     -- Postcondition:
     --   Return table; whitespace-only values as false
+
     --     Return table; whitespace-only values as false
 
     -- Uses:
 
     -- Uses:
 
     --    mw.getCurrentFrame()
 
     --    mw.getCurrentFrame()
Zeile 222: Zeile 227:
 
     local k, v
 
     local k, v
 
     local r = { }
 
     local r = { }
     local t = mw.getCurrentFrame():getParent()
+
     local t = mw.getCurrentFrame()
 +
    if low then
 +
        t = t:getParent()
 +
    end
 
     local o = t.args
 
     local o = t.args
 
     for k, v in pairs( o ) do
 
     for k, v in pairs( o ) do
Zeile 247: Zeile 255:
 
     --    specified  -- string or nil; requested parameter set
 
     --    specified  -- string or nil; requested parameter set
 
     -- Postcondition:
 
     -- Postcondition:
     --   Return sequence table
+
     --     Return sequence table
 
     -- Uses:
 
     -- Uses:
 
     --    mw.text.split()
 
     --    mw.text.split()
Zeile 278: Zeile 286:
 
     -- Postcondition:
 
     -- Postcondition:
 
     --    Return string or false
 
     --    Return string or false
 +
    -- Uses:
 +
    --    factory()
 
     local r = false
 
     local r = false
 
     if submit then
 
     if submit then
Zeile 287: Zeile 297:
 
         end
 
         end
 
         if opt.noError then
 
         if opt.noError then
             r = false
+
             if not opt.cat then
 +
                r = submit .. " " .. factory( "noErrorCat" )
 +
            end
 
         else
 
         else
             r = "<span class='error'>" .. submit .. "</span>"
+
            r = submit
 +
        end
 +
        if r then
 +
             r = "<span class='error'>" .. r .. "</span>"
 
         end
 
         end
 
         s = opt.cat
 
         s = opt.cat
Zeile 315: Zeile 330:
 
     --    needle    -- any; key name
 
     --    needle    -- any; key name
 
     -- Postcondition:
 
     -- Postcondition:
     --   Return true iff found
+
     --     Return true iff found
 
     local i
 
     local i
 
     for i = 1, #haystack do
 
     for i = 1, #haystack do
Zeile 372: Zeile 387:
  
  
local function fix( valid, duty, options )
+
local function fix( valid, duty, got, options )
     -- Perform transclusion parameter analysis
+
     -- Perform parameter analysis
 
     -- Precondition:
 
     -- Precondition:
 
     --    valid    -- table; unique sequence of known parameters
 
     --    valid    -- table; unique sequence of known parameters
 
     --    duty    -- table; sequence of mandatory parameters
 
     --    duty    -- table; sequence of mandatory parameters
 +
    --    got      -- table; sequence of current parameters
 
     --    options  -- table or nil; optional details
 
     --    options  -- table or nil; optional details
 
     -- Postcondition:
 
     -- Postcondition:
 
     --    Return string as configured; empty if valid
 
     --    Return string as configured; empty if valid
 
     -- Uses:
 
     -- Uses:
    --    fetch()
 
 
     --    finder()
 
     --    finder()
 
     --    fault()
 
     --    fault()
Zeile 387: Zeile 402:
 
     --    fed()
 
     --    fed()
 
     local k, v
 
     local k, v
     local r   = false
+
     local r = false
    local got = fetch()
 
 
     for k, v in pairs( got ) do
 
     for k, v in pairs( got ) do
 
         if not finder( valid, k ) then
 
         if not finder( valid, k ) then
Zeile 420: Zeile 434:
 
     return r
 
     return r
 
end -- fix()
 
end -- fix()
 +
 +
 +
 +
local function fold( low , options )
 +
    -- Run parameter analysis on current environment
 +
    -- Precondition:
 +
    --    low      -- true: template transclusion;  false: #invoke
 +
    --    options  -- table or nil; optional details
 +
    --                options.mandatory
 +
    --                options.optional
 +
    -- Postcondition:
 +
    --    Return string with error message as configured;
 +
    --            false if valid
 +
    -- Uses:
 +
    --    fit()
 +
    --    failure()
 +
    --    fetch()
 +
    --    fix()
 +
    --    finalize()
 +
    local duty, r
 +
    if type( options ) == "table" then
 +
        if type( options.mandatory ) == "table" then
 +
            duty = options.mandatory
 +
        else
 +
            duty = { }
 +
        end
 +
        if type( options.optional ) ~= "table" then
 +
            options.optional = { }
 +
        end
 +
        r = fit( duty, options.optional )
 +
    else
 +
        duty = { }
 +
        r    = { }
 +
    end
 +
    if type( r ) == "string" then
 +
        r = failure( "dupOpt", r, options )
 +
    else
 +
        local got = fetch( low )
 +
        r = fix( r, duty, got, options )
 +
    end
 +
    return finalize( r, options )
 +
end -- fold()
  
  
Zeile 521: Zeile 577:
  
 
TemplatePar.check = function ( options )
 
TemplatePar.check = function ( options )
     -- Run parameter analysis
+
     -- Run parameter analysis on current template environment
 
     -- Precondition:
 
     -- Precondition:
 
     --    options  -- table or nil; optional details
 
     --    options  -- table or nil; optional details
Zeile 528: Zeile 584:
 
     -- Postcondition:
 
     -- Postcondition:
 
     --    Return string with error message as configured;
 
     --    Return string with error message as configured;
     --            false if valid or no answer permitted
+
     --            false if valid
 
     -- Uses:
 
     -- Uses:
     --    fit()
+
     --    fold()
    --    failure()
+
     return fold( true, options )
    --    fix()
 
    --    finalize()
 
    local duty, r
 
    if type( options ) == "table" then
 
        if type( options.mandatory ) == "table" then
 
            duty = options.mandatory
 
        else
 
            duty = { }
 
        end
 
        if type( options.optional ) ~= "table" then
 
            options.optional = { }
 
        end
 
        r = fit( duty, options.optional )
 
    else
 
        duty = { }
 
        r    = { }
 
    end
 
    if type( r ) == "string" then
 
        r = failure( "dupOpt", r, options )
 
    else
 
        r = fix( r, duty, options )
 
    end
 
     return finalize( r, options )
 
 
end -- TemplatePar.check()
 
end -- TemplatePar.check()
  
Zeile 562: Zeile 595:
 
     -- Return number of template parameters
 
     -- Return number of template parameters
 
     -- Postcondition:
 
     -- Postcondition:
     --   Return number, starting at 0
+
     --     Return number, starting at 0
 
     -- Uses:
 
     -- Uses:
 
     --    mw.getCurrentFrame()
 
     --    mw.getCurrentFrame()
Zeile 605: Zeile 638:
 
     return finalize( r, options )
 
     return finalize( r, options )
 
end -- TemplatePar.valid()
 
end -- TemplatePar.valid()
 +
 +
 +
 +
TemplatePar.verify = function ( options )
 +
    -- Perform #invoke parameter analysis
 +
    -- Precondition:
 +
    --    options  -- table or nil; optional details
 +
    -- Postcondition:
 +
    --    Return string with error message as configured;
 +
    --            false if valid
 +
    -- Uses:
 +
    --    fold()
 +
    return fold( false, options )
 +
end -- TemplatePar.verify()
  
  
Zeile 620: Zeile 667:
 
     --    Return string with error message or ""
 
     --    Return string with error message or ""
 
     -- Uses:
 
     -- Uses:
 +
    --    fold()
 
     --    fill()
 
     --    fill()
    --    TemplatePar.check()
+
     local options = { optional  = { "1",
     local options = { mandatory = fill( frame.args[ 1 ] ),
+
                                    "2",
                      optional  = fill( frame.args[ 2 ] ),
+
                                    "cat",
                      cat      = frame.args.cat,
+
                                    "noError",
                      noError  = frame.args.noError,
+
                                    "template" }
                      template  = frame.args.template
 
 
                     }
 
                     }
     return TemplatePar.check( options ) or ""
+
     local r = fold( false, options )
 +
    if not r then
 +
        options = { mandatory = fill( frame.args[ 1 ] ),
 +
                    optional  = fill( frame.args[ 2 ] ),
 +
                    cat      = frame.args.cat,
 +
                    noError  = frame.args.noError,
 +
                    template  = frame.args.template
 +
                  }
 +
        r      = fold( true, options )
 +
    end
 +
    return r or ""
 
end -- .check()
 
end -- .check()
  
Zeile 651: Zeile 708:
 
     --    Return string with error message or ""
 
     --    Return string with error message or ""
 
     -- Uses:
 
     -- Uses:
 +
    --    fold()
 
     --    trim()
 
     --    trim()
 
     --    mw.ustring.gsub()
 
     --    mw.ustring.gsub()
 
     --    TemplatePar.valid()
 
     --    TemplatePar.valid()
    local r = false
+
     local options = { mandatory = { "1" },
    local s
+
                       optional = { "2",
     local options = { cat      = frame.args.cat,
+
                                    "cat",
                       noError = frame.args.noError,
+
                                    "max",
                      template = frame.args.template
+
                                    "min",
 +
                                    "noError",
 +
                                    "template" }
 
                     }
 
                     }
     s = trim( frame.args[ 2 ] )
+
     local r = fold( false, options )
    if type( s ) == "string" then
+
    if not r then
        local sub = s:match( "^/(.*%S)/$" )
+
        local s = trim( frame.args[ 2 ] )
        if type( sub ) == "string" then
+
        options = { cat      = frame.args.cat,
            sub = sub:gsub( "%%!", "|" )
+
                    noError  = frame.args.noError,
            sub = sub:gsub( "%%%(%(", "{{" )
+
                    template = frame.args.template
            sub = sub:gsub( "%%%)%)", "}}" )
+
                  }
            options.pattern = sub
+
        if type( s ) == "string" then
         else
+
            local sub = s:match( "^/(.*%S)/$" )
             options.key = s
+
            if type( sub ) == "string" then
 +
                sub = sub:gsub( "%%!", "|" )
 +
                sub = sub:gsub( "%%%(%(", "{{" )
 +
                sub = sub:gsub( "%%%)%)", "}}" )
 +
                options.pattern = sub
 +
            else
 +
                options.key = s
 +
            end
 +
         end
 +
        if type( frame.args.min ) == "string" then
 +
            s = frame.args.min:match( "^%s*([0-9]+)%s*$" )
 +
             if s then
 +
                options.min = tonumber( s )
 +
            else
 +
                r = failure( "invalidPar",
 +
                            "min=" .. frame.args.min,
 +
                            options )
 +
            end
 
         end
 
         end
    end
+
        if type( frame.args.max ) == "string" then
    if type( frame.args.min ) == "string" then
+
            s = frame.args.max:match( "^%s*([1-9][0-9]*)%s*$" )
        s = frame.args.min:match( "^%s*([0-9]+)%s*$" )
+
            if s then
        if s then
+
                options.max = tonumber( s )
            options.min = tonumber( s )
+
            else
        else
+
                r = failure( "invalidPar",
            r = failure( "invalidPar",
+
                            "max=" .. frame.args.max,
                        "min=" .. frame.args.min,
+
                            options )
                        options )
+
            end
 
         end
 
         end
    end
+
         if r then
    if type( frame.args.max ) == "string" then
+
             r = finalize( r, options )
        s = frame.args.max:match( "^%s*([1-9][0-9]*)%s*$" )
 
         if s then
 
             options.max = tonumber( s )
 
 
         else
 
         else
             r = failure( "invalidPar",
+
             s = frame.args[ 1 ] or ""
                        "max=" .. frame.args.max,
+
            r = TemplatePar.valid( s, options )
                        options )
 
 
         end
 
         end
 
     end
 
     end
     if r then
+
     return r or ""
        r = finalize( r, options )
 
    else
 
        s = frame.args[ 1 ] or ""
 
        r = TemplatePar.valid( s, options ) or ""
 
    end
 
    return r
 
 
end -- .valid()
 
end -- .valid()
  

Version vom 6. Mai 2013, 22:05 Uhr

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

--[=[ TemplatePar 2013-05-06
Template parameter utility
* check
* count
* valid
* verify
* TemplatePar()
]=]



-- Module globals
local TemplatePar = { }
local messagePrefix = "lua-module-TemplatePar-"
local l10nDef = {}
l10nDef[ "en" ] = {
    badPattern  = "#invoke:TemplatePar * pattern syntax error",
    dupOpt      = "#invoke:TemplatePar * repeated optional parameter",
    dupRule     = "#invoke:TemplatePar * parameter conflict key/pattern",
    empty       = "Error in template * undefined value for mandatory",
    invalid     = "Error in template * invalid parameter",
    invalidPar  = "#invoke:TemplatePar * invalid parameter",
    minmax      = "#invoke:TemplatePar * min > max",
    noErrorCat  = "#invoke:TemplatePar * noError and missing category",
    noname      = "#invoke:TemplatePar * missing parameter name",
    tooLong     = "Error in template * parameter too long",
    tooShort    = "Error in template * parameter too short",
    undefined   = "Error in template * mandatory parameter missing",
    unknown     = "Error in template * unknown parameter name",
    unknownRule = "#invoke:TemplatePar * unknown rule"
}
l10nDef[ "de" ]  = {
    badPattern  = "#invoke:TemplatePar * Syntaxfehler des pattern",
    dupOpt      = "#invoke:TemplatePar * Optionsparameter wiederholt",
    dupRule     = "#invoke:TemplatePar * Parameterkonflikt key/pattern",
    empty       = "Fehler bei Vorlage * Pflichtparameter ohne Wert",
    invalid     = "Fehler bei Vorlage * Parameter ungültig",
    invalidPar  = "#invoke:TemplatePar * Ungültiger Parameter",
    minmax      = "#invoke:TemplatePar * min > max",
    noErrorCat  = "#invoke:TemplatePar * noError und keine Kategorie",
    noname      = "#invoke:TemplatePar * Parametername nicht angegeben",
    tooLong     = "Fehler bei Vorlage * Parameter zu lang",
    tooShort    = "Fehler bei Vorlage * Parameter zu kurz",
    undefined   = "Fehler bei Vorlage * Pflichtparameter fehlt",
    unknown     = "Fehler bei Vorlage * Parametername unbekannt",
    unknownRule = "#invoke:TemplatePar * Unbekannte Regel"
}
local Patterns = {
    [ "ASCII" ]   = "^[ -~]*$",
    [ "ASCII+" ]  = "^[ -~]+$",
    [ "ASCII+1" ] = "^[!-~]+$",
    [ "n" ]       = "^%-?[0-9]*$",
    [ "n>0" ]     = "^[0-9]*[1-9][0-9]*$",
    [ "N+" ]      = "^%-?[1-9][0-9]*$",
    [ "N>0" ]     = "^[1-9][0-9]*$",
    [ "x" ]       = "^[0-9A-Fa-f]*$",
    [ "x+" ]      = "^[0-9A-Fa-f]+$",
    [ "X" ]       = "^[0-9A-F]*$",
    [ "X+" ]      = "^[0-9A-F]+$",
    [ "0,0" ]     = "^%-?[0-9]*,?[0-9]*$",
    [ "0,0+" ]    = "^%-?[0-9]+,[0-9]+$",
    [ "0,0+?" ]   = "^%-?[0-9]+,?[0-9]*$",
    [ "0.0" ]     = "^%-?[0-9]*%.?[0-9]*$",
    [ "0.0+" ]    = "^%-?[0-9]+%.[0-9]+$",
    [ "0.0+?" ]   = "^%-?[0-9]+%.?[0-9]*$",
    [ ".0+" ]     = "^%-?[0-9]*%.?[0-9]+$",
    [ "ID" ]      = "^[A-Za-z]?[A-Za-z_0-9]*$",
    [ "ID+" ]     = "^[A-Za-z][A-Za-z_0-9]*$",
    [ "ABC" ]     = "^[A-Z]*$",
    [ "ABC+" ]    = "^[A-Z]+$",
    [ "Abc" ]     = "^[A-Z]*[a-z]*$",
    [ "Abc+" ]    = "^[A-Z][a-z]+$",
    [ "abc" ]     = "^[a-z]*$",
    [ "abc+" ]    = "^[a-z]+$",
    [ "aBc+" ]    = "^[a-z]+[A-Z][A-Za-z]*$",
    [ "base64" ]  = "^[A-Za-z0-9%+/]*$",
    [ "base64+" ] = "^[A-Za-z0-9%+/]+$",
    [ "+" ]       = "%S"
}



function trim( s )
    -- Trim string
    -- Precondition:
    --     s  -- string or nil; to be trimmed
    -- Postcondition:
    --     Return trimmed string or nil
    local r = s
    if type( s ) == "string" then
        if s:match( "^%s*$" ) then
            r = ""
        else
            r = s:match( "^%s*(%S.*)$" )
            r = r:match( "^(.*%S)%s*$" )
        end
    end
    return r
end -- trim()



local function factory( say )
    -- Retrieve localized message string in content language
    -- Precondition:
    --     say  -- string; message ID
    -- Postcondition:
    --     Return some message string
    -- Uses:
    --     >  messagePrefix
    --     >  l10nDef
    --     mw.language.getContentLanguage()
    --     mw.message.new()
    local c = mw.language.getContentLanguage():getCode()
    local m = mw.message.new( messagePrefix .. say )
    local r = false
    if m:isBlank() then
        local l10n = l10nDef[ c ]
        if not l10n then
            l10n = l10nDef[ "en" ]
        end
        r = l10n[ say ]
    else
        m:inLanguage( c )
        r = m:plain()
    end
    if not r then
        r = "(((".. say .. ")))"
    end
    return r
end -- factory()



local function failsafe( story, scan )
    -- Test for match (possibly user-defined with synrax error)
    -- Precondition:
    --     story  -- string; parameter value
    --     scan   -- string; pattern
    -- Postcondition:
    --     Return nil, if not matching, else non-nil
    -- Uses:
    --     mw.ustring.match()
    return  mw.ustring.match( story, scan )
end -- failsafe()



local function failure( spec, suspect, options )
    -- Submit localized error message
    -- Precondition:
    --     spec     -- string; message ID
    --     suspect  -- string or nil; additional information
    --     options  -- table or nil; optional details
    --                 options.template
    -- Postcondition:
    --     Return string
    -- Uses:
    --     factory()
    local r = factory( spec )
    if type( options ) == "table" then
        if type( options.template ) == "string" then
            if #options.template > 0 then
                r = r .. " (" .. options.template .. ")"
            end
        end
    end
    if suspect then
        r = r .. ": " .. suspect
    end
    return r
end -- failure()



local function fault( store, key )
    -- Add key to collection string and insert separator
    -- Precondition:
    --     store  -- string or nil or false; collection string
    --     key    -- string or number; to be appended
    -- Postcondition:
    --     Return string; extended
    local r
    local s
    if type( key ) == "number" then
        s = tostring( key )
    else
        s = key
    end
    if store then
        r = store .. "; " .. s
    else
        r = s
    end
    return r
end -- fault()



local function fed( haystack, needle )
    -- Find needle in haystack map
    -- Precondition:
    --     haystack  -- table; map of key values
    --     needle    -- any; identifier
    -- Postcondition:
    --     Return true iff found
    local k, v
    for k, v in pairs( haystack ) do
        if k == needle then
            return true
        end
    end -- for k, v
    return false
end -- fed()



local function fetch( low )
    -- Return regular table with all template transclusion parameters
    -- Precondition:
    --     low  -- true: template transclusion;  false: #invoke
    -- Postcondition:
    --     Return table; whitespace-only values as false
    -- Uses:
    --     mw.getCurrentFrame()
    --     frame:getParent()
    local k, v
    local r = { }
    local t = mw.getCurrentFrame()
    if low then
        t = t:getParent()
    end
    local o = t.args
    for k, v in pairs( o ) do
        if type( v ) == "string" then
            if v:match( "^%s*$" ) then
                v = false
            end
        else
            v = false
        end
        if type( k ) == "number" then
            k = tostring( k )
        end
        r[ k ] = v
    end -- for k, v
    return r
end -- fetch()



local function fill( specified )
    -- Split requirement string separated by '='
    -- Precondition:
    --     specified  -- string or nil; requested parameter set
    -- Postcondition:
    --     Return sequence table
    -- Uses:
    --     mw.text.split()
    local r
    if specified then
        local i, s
        r = mw.text.split( specified, "%s*=%s*" )
        for i = #r, 1, -1 do
            s = r[ i ]
            if #s == 0 then
                table.remove( r, i )
            end
        end -- for i, -1
    else
        r = { }
    end
    return r
end -- fill()



local function finalize( submit, options )
    -- Finalize message
    -- Precondition:
    --     submit   -- string or false or nil; non-empty error message
    --     options  -- table or nil; optional details
    --                 options.noError
    --                 options.cat
    --                 options.template
    -- Postcondition:
    --     Return string or false
    -- Uses:
    --     factory()
    local r = false
    if submit then
        local opt, s
        if type( options ) == "table" then
            opt = options
        else
            opt = { }
        end
        if opt.noError then
            if not opt.cat then
                r = submit .. " " .. factory( "noErrorCat" )
            end
        else
            r = submit
        end
        if r then
            r = "<span class='error'>" .. r .. "</span>"
        end
        s = opt.cat
        if type( s ) == "string" then
            if not r then
               r = ""
            end
            if s:find( "@@@" ) then
                if type( opt.template ) == "string" then
                    s = s:gsub( "@@@", opt.template )
                end
            end
            r = r .. "[[Category:" .. s .. "]]"
        end
    end
    return r
end -- finalize()



local function finder( haystack, needle )
    -- Find needle in haystack sequence
    -- Precondition:
    --     haystack  -- table; sequence of key names
    --     needle    -- any; key name
    -- Postcondition:
    --     Return true iff found
    local i
    for i = 1, #haystack do
        if haystack[ i ] == needle then
            return true
        end
    end -- for i
    return false
end -- finder()



local function fit( base, extend )
    -- Merge two tables, create new sequence if both not empty
    -- Precondition:
    --     base    -- table; sequence kept unchanged
    --     extend  -- table; sequence to be appended
    -- Postcondition:
    --     Return merged table, or message string if duplicated entries
    -- Uses:
    --     finder()
    --     fault()
    local r
    if #base == 0 then
        if #extend == 0 then
            r = { }
        else
            r = extend
        end
    else
        if #extend == 0 then
            r = base
        else
            local i, s
            r = false
            for i = 1, #extend do
                s = extend[ i ]
                if finder( base, s ) then
                    r = fault( r, s )
                end
            end -- for i
            if not r then
                r = { }
                for i = 1, #base do
                    table.insert( r, base[ i ] )
                end -- for i
                for i = 1, #extend do
                    table.insert( r, extend[ i ] )
                end -- for i
            end
        end
    end
    return r
end -- fit()



local function fix( valid, duty, got, options )
    -- Perform parameter analysis
    -- Precondition:
    --     valid    -- table; unique sequence of known parameters
    --     duty     -- table; sequence of mandatory parameters
    --     got      -- table; sequence of current parameters
    --     options  -- table or nil; optional details
    -- Postcondition:
    --     Return string as configured; empty if valid
    -- Uses:
    --     finder()
    --     fault()
    --     failure()
    --     fed()
    local k, v
    local r = false
    for k, v in pairs( got ) do
        if not finder( valid, k ) then
            r = fault( r, k )
        end
    end -- for k, v
    if r then
        r = failure( "unknown", r, options )
    else -- all names valid
        local i, s
        for i = 1, #duty do
            s = duty[ i ]
            if not fed( got, s ) then
                r = fault( r, s )
            end
        end -- for i
        if r then
            r = failure( "undefined", r, options )
        else -- all mandatory present
            for i = 1, #duty do
                s = duty[ i ]
                if not got[ s ] then
                    r = fault( r, s )
                end
            end -- for i
            if r then
                r = failure( "empty", r, options )
            end
        end
    end
    return r
end -- fix()



local function fold( low , options )
    -- Run parameter analysis on current environment
    -- Precondition:
    --     low      -- true: template transclusion;  false: #invoke
    --     options  -- table or nil; optional details
    --                 options.mandatory
    --                 options.optional
    -- Postcondition:
    --     Return string with error message as configured;
    --            false if valid
    -- Uses:
    --     fit()
    --     failure()
    --     fetch()
    --     fix()
    --     finalize()
    local duty, r
    if type( options ) == "table" then
        if type( options.mandatory ) == "table" then
            duty = options.mandatory
        else
            duty = { }
        end
        if type( options.optional ) ~= "table" then
            options.optional = { }
        end
        r = fit( duty, options.optional )
    else
        duty = { }
        r    = { }
    end
    if type( r ) == "string" then
        r = failure( "dupOpt", r, options )
    else
        local got = fetch( low )
        r = fix( r, duty, got, options )
    end
    return finalize( r, options )
end -- fold()



local function format( seek, options )
    -- Check validity of one particular template parameter
    -- Precondition:
    --     seek     -- string non-empty; name of template parameter
    --     options  -- table or nil; optional details
    --                 options.pattern
    --                 options.key
    --                 options.min
    --                 options.max
    -- Postcondition:
    --     Return string with error message as configured;
    --            false if valid or no answer permitted
    -- Uses:
    --     >  Patterns
    --     mw.getCurrentFrame()()
    --     frame:getParent()
    --     failure()
    --     failsafe()
    local r = false
    local s
    local scan  = false
    local story = mw.getCurrentFrame():getParent()
    story = ( story.args[ seek ] or "" )
    if type( options.pattern ) == "string" then
        if options.key then
            r = failure( "dupRule", false, options )
        else
            scan = options.pattern
        end
    else
        if type( options.key ) == "string" then
            s = trim( options.key )
        else
            s = "+"
        end
        scan = Patterns[ s ]
        if type( scan ) == "string" then
            if s == "n" or s == "0,0" or s == "0.0" then
               if not story:match( "[0-9]" ) then
                    scan = false
                    r    = failure( "invalid",
                                    "'" .. seek .. "'",
                                    options )
                end
            end
        else
            r = failure( "unknownRule", s, options )
        end
    end
    if scan then
        local legal, got = pcall( failsafe, story, scan )
        if legal then
            if not got then
                r = failure( "invalid",  "'" .. seek .. "'",  options )
            end
        else
            r = failure( "badPattern",
                         scan .. " *** " .. got,
                         options )
        end
    end
    if options.min  and  not r then
        if type( options.min ) == "number" then
            if type( options.max ) == "number" then
                if options.max < options.min then
                    r = failure( "minmax",
                                 tostring( options.min )
                                 .. " > " ..
                                 tostring( options.max ),
                                 options )
                end
            end
            if #story < options.min  and  not r then
                r = failure( "tooShort",
                             " <" .. options.min .. " '" .. seek .. "'",
                             options )
            end
        else
            r = failure( "invalidPar", "min", options )
        end
    end
    if options.max  and  not r then
        if type( options.max ) == "number" then
            if #story > options.max then
                r = failure( "tooLong",
                             " >" .. options.max .. " '" .. seek .. "'",
                             options )
            end
        else
            r = failure( "invalidPar", "max", options )
        end
    end
    return r
end -- format()



TemplatePar.check = function ( options )
    -- Run parameter analysis on current template environment
    -- Precondition:
    --     options  -- table or nil; optional details
    --                 options.mandatory
    --                 options.optional
    -- Postcondition:
    --     Return string with error message as configured;
    --            false if valid
    -- Uses:
    --     fold()
    return fold( true, options )
end -- TemplatePar.check()



TemplatePar.count = function ()
    -- Return number of template parameters
    -- Postcondition:
    --     Return number, starting at 0
    -- Uses:
    --     mw.getCurrentFrame()
    --     frame:getParent()
    local k, v
    local r = 0
    local t = mw.getCurrentFrame():getParent()
    local o = t.args
    for k, v in pairs( o ) do
        r = r + 1
    end -- for k, v
    return r
end -- TemplatePar.count()



TemplatePar.valid = function ( seek, options )
    -- Check validity of one particular template parameter
    -- Precondition:
    --     seek     -- string; name of template parameter
    --     options  -- table or nil; optional details
    -- Postcondition:
    --     Return string with error message as configured;
    --            false if valid or no answer permitted
    -- Uses:
    --     trim()
    --     format()
    --     failure()
    --     finalize()
    local r
    if type( seek ) == "string" then
        r = trim( seek )
        if #r == 0 then
            r = false
        end
    end
    if r then
        r = format( seek, options )
    else
        r = failure( "noname", false, options )
    end
    return finalize( r, options )
end -- TemplatePar.valid()



TemplatePar.verify = function ( options )
    -- Perform #invoke parameter analysis
    -- Precondition:
    --     options  -- table or nil; optional details
    -- Postcondition:
    --     Return string with error message as configured;
    --            false if valid
    -- Uses:
    --     fold()
    return fold( false, options )
end -- TemplatePar.verify()



-- Provide external access
local p = {}



function p.check( frame )
    -- Check validity of template parameters
    -- Precondition:
    --     frame  -- object; #invoke environment
    -- Postcondition:
    --     Return string with error message or ""
    -- Uses:
    --     fold()
    --     fill()
    local options = { optional  = { "1",
                                    "2",
                                    "cat",
                                    "noError",
                                    "template" }
                    }
    local r = fold( false, options )
    if not r then
        options = { mandatory = fill( frame.args[ 1 ] ),
                    optional  = fill( frame.args[ 2 ] ),
                    cat       = frame.args.cat,
                    noError   = frame.args.noError,
                    template  = frame.args.template
                  }
        r       = fold( true, options )
    end
    return r or ""
end -- .check()



function p.count( frame )
    -- Count number of template parameters
    -- Postcondition:
    --     Return string with digits including "0"
    -- Uses:
    --     TemplatePar.count()
    return tostring( TemplatePar.count() )
end -- .count()



function p.valid( frame )
    -- Check validity of one particular template parameter
    -- Precondition:
    --     frame  -- object; #invoke environment
    -- Postcondition:
    --     Return string with error message or ""
    -- Uses:
    --     fold()
    --     trim()
    --     mw.ustring.gsub()
    --     TemplatePar.valid()
    local options = { mandatory = { "1" },
                      optional  = { "2",
                                    "cat",
                                    "max",
                                    "min",
                                    "noError",
                                    "template" }
                    }
    local r = fold( false, options )
    if not r then
        local s = trim( frame.args[ 2 ] )
        options = { cat      = frame.args.cat,
                    noError  = frame.args.noError,
                    template = frame.args.template
                  }
        if type( s ) == "string" then
            local sub = s:match( "^/(.*%S)/$" )
            if type( sub ) == "string" then
                sub = sub:gsub( "%%!", "|" )
                sub = sub:gsub( "%%%(%(", "{{" )
                sub = sub:gsub( "%%%)%)", "}}" )
                options.pattern = sub
            else
                options.key = s
            end
        end
        if type( frame.args.min ) == "string" then
            s = frame.args.min:match( "^%s*([0-9]+)%s*$" )
            if s then
                options.min = tonumber( s )
            else
                r = failure( "invalidPar",
                             "min=" .. frame.args.min,
                             options )
            end
        end
        if type( frame.args.max ) == "string" then
            s = frame.args.max:match( "^%s*([1-9][0-9]*)%s*$" )
            if s then
                options.max = tonumber( s )
            else
                r = failure( "invalidPar",
                             "max=" .. frame.args.max,
                             options )
            end
        end
        if r then
            r = finalize( r, options )
        else
            s = frame.args[ 1 ] or ""
            r = TemplatePar.valid( s, options )
        end
    end
    return r or ""
end -- .valid()



function p.TemplatePar()
    -- Retrieve function access for modules
    -- Postcondition:
    --     Return table with functions
    return TemplatePar
end -- .TemplatePar()



return p