Content deleted Content added
update |
update |
||
Line 1:
--[=[ TemplatePar 2013-05-
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
invalid = "Error in template
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
tooShort = "Error in template
undefined = "Error in template
unknown = "Error in template
unknownRule = "#invoke:TemplatePar * unknown rule"
}
Line 32 ⟶ 34:
dupOpt = "#invoke:TemplatePar * Optionsparameter wiederholt",
dupRule = "#invoke:TemplatePar * Parameterkonflikt key/pattern",
empty = "Fehler bei Vorlage
invalid = "Fehler bei Vorlage
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
tooShort = "Fehler bei Vorlage
undefined = "Fehler bei Vorlage
unknown = "Fehler bei Vorlage
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
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
r = submit .. " " .. factory( "noErrorCat" )
end
else
r =
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
-- 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()
Line 387 ⟶ 402:
-- fed()
local k, v
local r
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
-- Uses:
--
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",
"2",
}
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
optional = { "2",
"noError",
"template" }
}
if
local
if type( s
local sub = s:match( "^/(.*%S)/$" )
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
r = failure( "invalidPar",
end
if type( frame.args.max ) == "string" then
r = failure( "invalidPar",
"max=" .. frame.args.max,
options )
end
end
if r then
r = finalize( r, options )
else
r
end
end
end -- .valid()
|