Module:TemplatePar: Difference between revisions

Content deleted Content added
update
update
Line 1:
--[=[ TemplatePar 2013-05-0506
Template parameter utility
* check
* count
* valid
* verify
* TemplatePar()
]=]
Line 17 ⟶ 18:
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"
}
Line 32 ⟶ 34:
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"
}
Line 164 ⟶ 167:
end
if suspect then
r = r .. ": " .. suspect
end
return r
Line 177 ⟶ 180:
-- key -- string or number; to be appended
-- Postcondition:
-- Return string; extended
local r
local s
Line 201 ⟶ 204:
-- needle -- any; identifier
-- Postcondition:
-- Return true iff found
local k, v
for k, v in pairs( haystack ) do
Line 213 ⟶ 216:
 
 
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()
Line 222 ⟶ 227:
local k, v
local r = { }
local t = mw.getCurrentFrame():getParent()
if low then
t = t:getParent()
end
local o = t.args
for k, v in pairs( o ) do
Line 247 ⟶ 255:
-- specified -- string or nil; requested parameter set
-- Postcondition:
-- Return sequence table
-- Uses:
-- mw.text.split()
Line 278 ⟶ 286:
-- Postcondition:
-- Return string or false
-- Uses:
-- factory()
local r = false
if submit then
Line 287 ⟶ 297:
end
if opt.noError then
rif =not falseopt.cat then
r = submit .. " " .. factory( "noErrorCat" )
end
else
r = "<span class='error'>" .. submit .. "</span>"
end
if r then
r = "<span class='error'>" .. r .. "</span>"
end
s = opt.cat
Line 315 ⟶ 330:
-- needle -- any; key name
-- Postcondition:
-- Return true iff found
local i
for i = 1, #haystack do
Line 372 ⟶ 387:
 
 
local function fix( valid, duty, got, options )
-- Perform transclusion 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:
-- fetch()
-- finder()
-- fault()
Line 387 ⟶ 402:
-- fed()
local k, v
local r = false
local got = fetch()
for k, v in pairs( got ) do
if not finder( valid, k ) then
Line 420 ⟶ 434:
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()
 
 
Line 521 ⟶ 577:
 
TemplatePar.check = function ( options )
-- Run parameter analysis on current template environment
-- Precondition:
-- options -- table or nil; optional details
Line 528 ⟶ 584:
-- Postcondition:
-- Return string with error message as configured;
-- false if valid or no answer permitted
-- Uses:
-- fitfold()
--return fold( true, options failure()
-- 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()
 
Line 562 ⟶ 595:
-- Return number of template parameters
-- Postcondition:
-- Return number, starting at 0
-- Uses:
-- mw.getCurrentFrame()
Line 605 ⟶ 638:
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()
 
 
Line 620 ⟶ 667:
-- Return string with error message or ""
-- Uses:
-- fold()
-- fill()
local options = { optional = { "1",
-- TemplatePar.check()
"2",
local options = { mandatory = fill( frame.args[ 1 ] ),
optional = fill( frame.args[ 2 ] ) "cat",
cat = frame.args.cat "noError",
noError = frame.args.noError, "template" }
template = frame.args.template
}
returnlocal TemplatePar.checkr = fold( false, options ) or ""
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()
 
Line 651 ⟶ 708:
-- Return string with error message or ""
-- Uses:
-- fold()
-- trim()
-- mw.ustring.gsub()
-- TemplatePar.valid()
local roptions = false{ mandatory = { "1" },
optional = { "2",
local s
local options = { cat = frame.args. "cat",
noError = frame.args.noError "max",
template = frame.args.template "min",
"noError",
"template" }
}
slocal r = trimfold( frame.args[ 2false, ]options )
if type(not s ) == "string"r then
local subs = s:matchtrim( "^/(frame.*%S)/$"args[ 2 ] )
ifoptions type(= sub{ )cat == "string" then = frame.args.cat,
sub = sub:gsub( "%%!", "|" ) noError = frame.args.noError,
sub = sub:gsub( "%%%(%(", "{{" ) template = frame.args.template
sub = sub:gsub( "%%%)%)", "}}" ) }
if type( s options.pattern) == "string" subthen
local sub = s:match( "^/(.*%S)/$" )
else
options.keyif type( sub ) == "string" sthen
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
end
if type( s = frame.args.min:match( "^%s*([0-9]+) == "string%s*$" then)
s = frame.args.min:match( "^%s*([0-9]+)% if s*$" )then
if options.min = tonumber( s then)
options.min = tonumber( s )else
r = failure( "invalidPar",
else
r = failure( "invalidParmin=" .. frame.args.min,
"min=" .. frame.args.min, options )
options )end
end
if type( frame.args.max ) == "string" then
end
if type( s = frame.args.max:match( "^%s*([1-9][0-9]*) == "string%s*$" then)
s = frame.args.max:match( "^%s*([1-9][0-9]*)% if s*$" )then
if options.max = tonumber( 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
rs = failure(frame.args[ 1 ] or "invalidPar",
r "max=" TemplatePar..valid( frame.args.maxs, options )
options )
end
end
ifreturn r thenor ""
r = finalize( r, options )
else
s = frame.args[ 1 ] or ""
r = TemplatePar.valid( s, options ) or ""
end
return r
end -- .valid()