Content deleted Content added
update |
update |
||
Line 1:
--[=[ TemplatePar 2013-05-
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
if type( s ) == "string" then
if s:match( "^%s*$" ) then
else
end
end
return
end -- trim()
Line 216 ⟶ 219:
local function fetch(
-- Return regular table with all
-- Precondition:
--
-- 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
g = TemplatePar.downcase( options )
else
g = mw.getCurrentFrame()
if light then
g = g:getParent()
end
g = g.args
end
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(
-- Merge two tables, create new sequence if both not empty
-- Precondition:
--
--
-- options.optional sequence to be appended
-- options.low downcased expected
-- Postcondition:
-- Return merged table, or message string if
-- Uses:
-- finder()
-- fault()
-- 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
for i = 1, #extend do
s = extend[ i ]
if finder( base, s ) then
end
end -- for i
if
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(
-- Run parameter analysis on current environment
-- Precondition:
--
-- options -- table or nil; optional details
-- options.mandatory
Line 455 ⟶ 486:
local duty, r
if type( options ) == "table" then
if type( options.mandatory )
end
duty = options.mandatory
if type( options.optional ) ~= "table" then
options.optional = { }
end
r = fit(
else
r = { }
end
if type( r ) == "
if type( got ) == "table" then
else
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
--
-- mw.getCurrentFrame()
-- frame:getParent()
-- failure()
Line 500 ⟶ 535:
local s
local scan = false
local story
end
if options. else
story = mw.getCurrentFrame():getParent()
end
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
scan = Patterns[
if type( scan ) == "string"
if s == "n"
if not
scan =
r = failure( "invalid",
"'" .. seek .. "'",
options )
end
end
else
r = failure( "unknownRule", s, options )
end
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()
-- 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
|