Modul:TemplatePar: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
update
wp>PerfektesChaos (update) |
wp>PerfektesChaos (update) |
||
Zeile 1: | Zeile 1: | ||
--[=[ TemplatePar 2013-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 | empty = "Error in template * undefined value for mandatory", | ||
invalid = "Error in template | 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 | tooLong = "Error in template * parameter too long", | ||
tooShort = "Error in template | tooShort = "Error in template * parameter too short", | ||
undefined = "Error in template | undefined = "Error in template * mandatory parameter missing", | ||
unknown = "Error in template | 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 | empty = "Fehler bei Vorlage * Pflichtparameter ohne Wert", | ||
invalid = "Fehler bei Vorlage | 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 | tooLong = "Fehler bei Vorlage * Parameter zu lang", | ||
tooShort = "Fehler bei Vorlage | tooShort = "Fehler bei Vorlage * Parameter zu kurz", | ||
undefined = "Fehler bei Vorlage | undefined = "Fehler bei Vorlage * Pflichtparameter fehlt", | ||
unknown = "Fehler bei Vorlage | 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 | ||
local r | local r | ||
local s | local s | ||
Zeile 201: | Zeile 204: | ||
-- needle -- any; identifier | -- needle -- any; identifier | ||
-- Postcondition: | -- Postcondition: | ||
-- | -- 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 | ||
-- 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 | ||
-- 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 = | if not opt.cat then | ||
r = submit .. " " .. factory( "noErrorCat" ) | |||
end | |||
else | else | ||
r = "<span class='error'>" .. | 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 | ||
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 | -- 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: | ||
-- finder() | -- finder() | ||
-- fault() | -- fault() | ||
Zeile 387: | Zeile 402: | ||
-- fed() | -- fed() | ||
local k, v | local k, v | ||
local r | local r = false | ||
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 | -- false if valid | ||
-- Uses: | -- Uses: | ||
-- | -- fold() | ||
return fold( true, options ) | |||
return | |||
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 | ||
-- 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() | ||
local options = { optional = { "1", | |||
local options = { | "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() | 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 options = { mandatory = { "1" }, | |||
optional = { "2", | |||
local options = { | "cat", | ||
"max", | |||
"min", | |||
"noError", | |||
"template" } | |||
} | } | ||
s = trim( frame.args[ 2 ] ) | 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)/$" ) | |||
options. | 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 | ||
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 | end | ||
if r then | |||
r = finalize( r, options ) | |||
if | |||
else | else | ||
s = frame.args[ 1 ] or "" | |||
r = TemplatePar.valid( s, options ) | |||
end | end | ||
end | end | ||
return r or "" | |||
end -- .valid() | end -- .valid() | ||