Module:TemplatePar: Difference between revisions

Content deleted Content added
update
update
Line 1:
--[=[ TemplatePar 2013-05-1020
Template parameter utility
* check
Line 26:
noErrorCat = "#invoke:TemplatePar * noError and missing category",
noname = "#invoke:TemplatePar * missing parameter name",
notLow = "#invoke:TemplatePar * not in lowercase mode",
tooLong = "Error in template * parameter too long",
tooShort = "Error in template * parameter too short",
Line 41 ⟶ 40:
invalidPar = "#invoke:TemplatePar * Ungültiger Parameter",
minmax = "#invoke:TemplatePar * min > max",
multiSpell = "Fehler bei Vorlage * mehrereMehrere Parameter-Schreibweisen",
noErrorCat = "#invoke:TemplatePar * noError und keine Kategorie",
noname = "#invoke:TemplatePar * Parametername nicht angegeben",
notLow = "#invoke:TemplatePar * Nicht in Kleinbuchstaben",
tooLong = "Fehler bei Vorlage * Parameter zu lang",
tooShort = "Fehler bei Vorlage * Parameter zu kurz",
Line 242 ⟶ 240:
g = g.args
end
forif k, v in pairstype( g ) do== "table" then
ifr type( v ) == "string"{ then}
for k, v in if v:matchpairs( "^%s*$"g ) thendo
if type( v ) == "string" then
if v:match( "^%s*$" ) then
v = false
end
else
v = false
end
if type( k ) == "number" then
else
v k = falsetostring( k )
end
if type( r[ k )] == "number" thenv
end -- k = tostring(for k, )v
endelse
r[ k ] = vg
end -- for k, v
return r
end -- fetch()
Line 352 ⟶ 355:
 
 
local function fitfix( 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 flat( collection, options )
-- Return all table elements with downcased string
-- Precondition:
-- collection -- table; k=v pairs
-- options -- table or nil; optional messaging details
-- Postcondition:
-- Return table, may be empty; or string with error message.
-- Uses:
-- mw.ustring.lower()
-- fault()
-- failure()
local k, v
local r = { }
local e = false
for k, v in pairs( collection ) do
if type ( k ) == "string" then
k = mw.ustring.lower( k )
if r[ k ] then
e = fault( e, k )
end
end
r[ k ] = v
end -- for k, v
if e then
r = failure( "multiSpell", e, options )
end
return r
end -- flat()
 
 
 
local function fold( options )
-- Merge two tables, create new sequence if both not empty
-- Precondition:
Line 365 ⟶ 449:
-- fault()
-- failure()
-- mw.ustring.lowerflat()
local i, e, r, s
local base = options.mandatory
Line 399 ⟶ 483:
end
end
if options.low and type( r ) == "table" then
er = falseflat( r, options )
for i = 1, #r do
s = r[ i ]
if type( s ) == "string" then
if s ~= mw.ustring.lower( s ) then
e = fault( e, s )
end
end
end -- for i
if e then
r = failure( "notLow", e, options )
end
end
return r
end -- fitfold()
 
 
 
local function fixform( valid, duty, gotlight, 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( light, options )
-- Run parameter analysis on current environment
-- Precondition:
Line 479 ⟶ 502:
-- false if valid
-- Uses:
-- fitfold()
-- failure()
-- fetch()
Line 493 ⟶ 516:
options.optional = { }
end
r = fitfold( options )
else
options = { }
Line 508 ⟶ 531:
end
return finalize( r, options )
end -- foldform()
 
 
Line 633 ⟶ 656:
-- false if valid
-- Uses:
-- foldform()
return foldform( true, options )
end -- TemplatePar.check()
 
Line 670 ⟶ 693:
-- fault()
-- failure()
local k, v
local r = { }
local t = mw.getCurrentFrame():getParent()
localreturn o =flat( t.args, options )
local e = false
for k, v in pairs( o ) do
if type ( k ) == "string" then
k = mw.ustring.lower( k )
if r[ k ] then
e = fault( e, k )
end
end
r[ k ] = v
end -- for k, v
if e then
r = failure( "multiSpell", e, options )
end
return r
end -- TemplatePar.downcase()
 
Line 730 ⟶ 737:
-- false if valid
-- Uses:
-- foldform()
return foldform( false, options )
end -- TemplatePar.verify()
 
Line 748 ⟶ 755:
-- Return string with error message or ""
-- Uses:
-- foldform()
-- fill()
local options = { optional = { "1",
Line 758 ⟶ 765:
template = "#invoke:TemplatePar|check|"
}
local r = foldform( false, options )
if not r then
options = { mandatory = fill( frame.args[ 1 ] ),
Line 767 ⟶ 774:
template = frame.args.template
}
r = foldform( true, options )
end
return r or ""
Line 792 ⟶ 799:
-- Return string with error message or ""
-- Uses:
-- foldform()
-- trim()
-- TemplatePar.valid()
Line 805 ⟶ 812:
template = "#invoke:TemplatePar|valid|"
}
local r = foldform( false, options )
if not r then
local s = trim( frame.args[ 2 ] )