Module:TemplatePar: Difference between revisions

Content deleted Content added
update
update
Line 1:
--[=[ TemplatePar 2013-05-0710
Template parameter utility
* check
* count
* downcase
* valid
* verify
Line 22 ⟶ 23:
invalidPar = "#invoke:TemplatePar * invalid parameter",
minmax = "#invoke:TemplatePar * min > max",
multiSpell = "Error in template * multiple spelling of parameter",
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 38 ⟶ 41:
invalidPar = "#invoke:TemplatePar * Ungültiger Parameter",
minmax = "#invoke:TemplatePar * min > max",
multiSpell = "Fehler bei Vorlage * mehrere 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 87 ⟶ 92:
-- Postcondition:
-- Return trimmed string or nil
local r = s
if type( s ) == "string" then
if s:match( "^%s*$" ) then
rs = ""
else
rs = s:match( "^%s*(%S.*)$" ):match( "^(.*%S)%s*$" )
r = r:match( "^(.*%S)%s*$" )
end
end
return rs
end -- trim()
 
Line 216 ⟶ 219:
 
 
local function fetch( lowlight, options )
-- Return regular table with all template transclusion parameters
-- Precondition:
-- lowlight -- true: template transclusion; false: #invoke
-- options -- table; optional details
-- options.low
-- Postcondition:
-- Return table; whitespace-only values as false
-- Uses:
-- TemplatePar.downcase()
-- mw.getCurrentFrame()
-- frame:getParent()
local g, k, v
local r = { }
if options.low then
local t = mw.getCurrentFrame()
g = TemplatePar.downcase( options )
if low then
else
t = t:getParent()
g = mw.getCurrentFrame()
if light then
g = g:getParent()
end
g = g.args
end
localfor ok, =v t.argsin pairs( g ) do
for k, v in pairs( o ) do
if type( v ) == "string" then
if v:match( "^%s*$" ) then
Line 327 ⟶ 337:
-- Find needle in haystack sequence
-- Precondition:
-- haystack -- table; sequence of key names, downcased if low
-- needle -- any; key name
-- Postcondition:
Line 342 ⟶ 352:
 
 
local function fit( base, extendoptions )
-- Merge two tables, create new sequence if both not empty
-- Precondition:
-- base options -- table; sequence kept unchangeddetails
-- extend -- table; options.mandatory sequence to bekeep appendedunchanged
-- options.optional sequence to be appended
-- options.low downcased expected
-- Postcondition:
-- Return merged table, or message string if duplicated entrieserror
-- Uses:
-- finder()
-- fault()
local-- r failure()
-- mw.ustring.lower()
local i, e, r, s
local base = options.mandatory
local extend = options.optional
if #base == 0 then
if #extend == 0 then
Line 363 ⟶ 379:
r = base
else
locale i,= sfalse
r = false
for i = 1, #extend do
s = extend[ i ]
if finder( base, s ) then
re = fault( re, s )
end
end -- for i
if not re then
r = failure( "dupOpt", e, options )
else
r = { }
for i = 1, #base do
Line 380 ⟶ 397:
end -- for i
end
end
end
if options.low then
e = false
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
Line 437 ⟶ 468:
 
 
local function fold( low light, options )
-- Run parameter analysis on current environment
-- Precondition:
-- low light -- true: template transclusion; false: #invoke
-- options -- table or nil; optional details
-- options.mandatory
Line 455 ⟶ 486:
local duty, r
if type( options ) == "table" then
if type( options.mandatory ) =~= "table" then
duty = options.mandatory = { }
else
duty = { }
end
duty = options.mandatory
if type( options.optional ) ~= "table" then
options.optional = { }
end
r = fit( duty, options.optional )
else
dutyoptions = { }
rduty = { }
r = { }
end
if type( r ) == "stringtable" then
rlocal got = failurefetch( "dupOpt", rlight, options )
if type( got ) == "table" then
else
local got r = fetchfix( lowr, duty, got, options )
else
r = fix( r, duty, got, options )
r = got
end
end
return finalize( r, options )
Line 486 ⟶ 519:
-- options.pattern
-- options.key
-- options.low
-- options.min
-- options.max
Line 493 ⟶ 527:
-- Uses:
-- > Patterns
-- mwTemplatePar.getCurrentFrame()downcase()
-- mw.getCurrentFrame()
-- frame:getParent()
-- failure()
Line 500 ⟶ 535:
local s
local scan = false
local story = mw.getCurrentFrame():getParent()
story =if type( story.args[options seek) ] or~= "table" )then
if type( options.pattern ) =options = "string"{ then}
end
if options.keylow then
rstory = failureTemplatePar.downcase( "dupRule", false, options )
else
scan = options.pattern
end
else
story = mw.getCurrentFrame():getParent()
if type( options.key ) == "string" then
end
s = trim( options.key )
if type( story ) == "table" then
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
sif type( options.key ) == "+string" then
end s = trim( options.key )
scan = Patterns[ s ]else
if type( scan ) = s = "string+" then
if s == "n" or s == "0,0" or s == "0.0" thenend
scan = Patterns[ ifs not story:match( "[0-9]" ) then
if type( scan ) == "string" scan = falsethen
if s == "n" ror s == "0,0" =or failure(s == "invalid0.0", then
if not story:match( "'[0-9]" ..) seek .. "'",then
scan = options )false
r = failure( "invalid",
"'" .. seek .. "'",
options )
end
end
else
r = failure( "unknownRule", s, options )
end
else
r = failure( "unknownRule", s, options )
end
else
r = story
end
if scan then
Line 608 ⟶ 655:
return r
end -- TemplatePar.count()
 
 
 
TemplatePar.downcase = function ( options )
-- Return all template parameters with downcased name
-- Precondition:
-- options -- table or nil; optional messaging details
-- Postcondition:
-- Return table, may be empty; or string with error message.
-- Uses:
-- mw.getCurrentFrame()
-- frame:getParent()
-- mw.ustring.lower()
-- fault()
-- failure()
local k, v
local r = { }
local t = mw.getCurrentFrame():getParent()
local o = t.args
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 672 ⟶ 753:
"2",
"cat",
"low",
"noError",
"template" },
Line 681 ⟶ 763:
optional = fill( frame.args[ 2 ] ),
cat = frame.args.cat,
low = frame.args.low,
noError = frame.args.noError,
template = frame.args.template
Line 711 ⟶ 794:
-- fold()
-- trim()
-- mw.ustring.gsub()
-- TemplatePar.valid()
local options = { mandatory = { "1" },
optional = { "2",
"cat",
"low",
"max",
"min",
Line 726 ⟶ 809:
local s = trim( frame.args[ 2 ] )
options = { cat = frame.args.cat,
low = frame.args.low,
noError = frame.args.noError,
template = frame.args.template