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-10 | ||
Template parameter utility | Template parameter utility | ||
* check | * check | ||
* count | * count | ||
* downcase | |||
* valid | * valid | ||
* verify | * verify | ||
Zeile 22: | Zeile 23: | ||
invalidPar = "#invoke:TemplatePar * invalid parameter", | invalidPar = "#invoke:TemplatePar * invalid parameter", | ||
minmax = "#invoke:TemplatePar * min > max", | minmax = "#invoke:TemplatePar * min > max", | ||
multiSpell = "Error in template * multiple spelling of parameter", | |||
noErrorCat = "#invoke:TemplatePar * noError and missing category", | noErrorCat = "#invoke:TemplatePar * noError and missing category", | ||
noname = "#invoke:TemplatePar * missing parameter name", | noname = "#invoke:TemplatePar * missing parameter name", | ||
notLow = "#invoke:TemplatePar * not in lowercase mode", | |||
tooLong = "Error in template * parameter too long", | tooLong = "Error in template * parameter too long", | ||
tooShort = "Error in template * parameter too short", | tooShort = "Error in template * parameter too short", | ||
Zeile 38: | Zeile 41: | ||
invalidPar = "#invoke:TemplatePar * Ungültiger Parameter", | invalidPar = "#invoke:TemplatePar * Ungültiger Parameter", | ||
minmax = "#invoke:TemplatePar * min > max", | minmax = "#invoke:TemplatePar * min > max", | ||
multiSpell = "Fehler bei Vorlage * mehrere Parameter-Schreibweisen", | |||
noErrorCat = "#invoke:TemplatePar * noError und keine Kategorie", | noErrorCat = "#invoke:TemplatePar * noError und keine Kategorie", | ||
noname = "#invoke:TemplatePar * Parametername nicht angegeben", | noname = "#invoke:TemplatePar * Parametername nicht angegeben", | ||
notLow = "#invoke:TemplatePar * Nicht in Kleinbuchstaben", | |||
tooLong = "Fehler bei Vorlage * Parameter zu lang", | tooLong = "Fehler bei Vorlage * Parameter zu lang", | ||
tooShort = "Fehler bei Vorlage * Parameter zu kurz", | tooShort = "Fehler bei Vorlage * Parameter zu kurz", | ||
Zeile 87: | Zeile 92: | ||
-- Postcondition: | -- Postcondition: | ||
-- Return trimmed string or nil | -- Return trimmed string or nil | ||
if type( s ) == "string" then | if type( s ) == "string" then | ||
if s:match( "^%s*$" ) then | if s:match( "^%s*$" ) then | ||
s = "" | |||
else | else | ||
s = s:match( "^%s*(%S.*)$" ):match( "^(.*%S)%s*$" ) | |||
end | end | ||
end | end | ||
return | return s | ||
end -- trim() | end -- trim() | ||
Zeile 216: | Zeile 219: | ||
local function fetch( | local function fetch( light, options ) | ||
-- Return regular table with all | -- Return regular table with all parameters | ||
-- Precondition: | -- Precondition: | ||
-- | -- light -- true: template transclusion; false: #invoke | ||
-- options -- table; optional details | |||
-- options.low | |||
-- Postcondition: | -- Postcondition: | ||
-- Return table; whitespace-only values as false | -- Return table; whitespace-only values as false | ||
-- Uses: | -- Uses: | ||
-- TemplatePar.downcase() | |||
-- mw.getCurrentFrame() | -- mw.getCurrentFrame() | ||
-- frame:getParent() | -- frame:getParent() | ||
local k, v | local g, k, v | ||
local r = { } | 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 | end | ||
for k, v in pairs( g ) do | |||
for k, v in pairs( | |||
if type( v ) == "string" then | if type( v ) == "string" then | ||
if v:match( "^%s*$" ) then | if v:match( "^%s*$" ) then | ||
Zeile 327: | Zeile 337: | ||
-- Find needle in haystack sequence | -- Find needle in haystack sequence | ||
-- Precondition: | -- Precondition: | ||
-- haystack -- table; sequence of key names | -- haystack -- table; sequence of key names, downcased if low | ||
-- needle -- any; key name | -- needle -- any; key name | ||
-- Postcondition: | -- Postcondition: | ||
Zeile 342: | Zeile 352: | ||
local function fit( | local function fit( options ) | ||
-- Merge two tables, create new sequence if both not empty | -- Merge two tables, create new sequence if both not empty | ||
-- Precondition: | -- Precondition: | ||
-- | -- options -- table; details | ||
-- | -- options.mandatory sequence to keep unchanged | ||
-- options.optional sequence to be appended | |||
-- options.low downcased expected | |||
-- Postcondition: | -- Postcondition: | ||
-- Return merged table, or message string if | -- Return merged table, or message string if error | ||
-- Uses: | -- Uses: | ||
-- finder() | -- finder() | ||
-- fault() | -- 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 #base == 0 then | ||
if #extend == 0 then | if #extend == 0 then | ||
Zeile 363: | Zeile 379: | ||
r = base | r = base | ||
else | else | ||
e = false | |||
for i = 1, #extend do | for i = 1, #extend do | ||
s = extend[ i ] | s = extend[ i ] | ||
if finder( base, s ) then | if finder( base, s ) then | ||
e = fault( e, s ) | |||
end | end | ||
end -- for i | end -- for i | ||
if | if e then | ||
r = failure( "dupOpt", e, options ) | |||
else | |||
r = { } | r = { } | ||
for i = 1, #base do | for i = 1, #base do | ||
Zeile 380: | Zeile 397: | ||
end -- for i | end -- for i | ||
end | 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 | ||
end | end | ||
Zeile 437: | Zeile 468: | ||
local function fold( | local function fold( light, options ) | ||
-- Run parameter analysis on current environment | -- Run parameter analysis on current environment | ||
-- Precondition: | -- Precondition: | ||
-- | -- light -- true: template transclusion; false: #invoke | ||
-- options -- table or nil; optional details | -- options -- table or nil; optional details | ||
-- options.mandatory | -- options.mandatory | ||
Zeile 455: | Zeile 486: | ||
local duty, r | local duty, r | ||
if type( options ) == "table" then | if type( options ) == "table" then | ||
if type( options.mandatory ) | if type( options.mandatory ) ~= "table" then | ||
options.mandatory = { } | |||
end | end | ||
duty = options.mandatory | |||
if type( options.optional ) ~= "table" then | if type( options.optional ) ~= "table" then | ||
options.optional = { } | options.optional = { } | ||
end | end | ||
r = fit( | r = fit( options ) | ||
else | else | ||
duty = { } | options = { } | ||
r | duty = { } | ||
r = { } | |||
end | end | ||
if type( r ) == " | if type( r ) == "table" then | ||
local got = fetch( light, options ) | |||
if type( got ) == "table" then | |||
r = fix( r, duty, got, options ) | |||
else | |||
r = got | |||
end | |||
end | end | ||
return finalize( r, options ) | return finalize( r, options ) | ||
Zeile 486: | Zeile 519: | ||
-- options.pattern | -- options.pattern | ||
-- options.key | -- options.key | ||
-- options.low | |||
-- options.min | -- options.min | ||
-- options.max | -- options.max | ||
Zeile 493: | Zeile 527: | ||
-- Uses: | -- Uses: | ||
-- > Patterns | -- > Patterns | ||
-- mw.getCurrentFrame | -- TemplatePar.downcase() | ||
-- mw.getCurrentFrame() | |||
-- frame:getParent() | -- frame:getParent() | ||
-- failure() | -- failure() | ||
Zeile 500: | Zeile 535: | ||
local s | local s | ||
local scan = false | local scan = false | ||
local story | local story | ||
if type( options ) ~= "table" then | |||
if type( options | options = { } | ||
if options. | end | ||
if options.low then | |||
story = TemplatePar.downcase( options ) | |||
else | else | ||
if type( options. | 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 | else | ||
s = "+" | if type( options.key ) == "string" then | ||
s = trim( options.key ) | |||
else | |||
s = "+" | |||
end | |||
scan = Patterns[ s ] | |||
if type( scan ) == "string" then | |||
if s == "n" or s == "0,0" or s == "0.0" then | |||
if not story:match( "[0-9]" ) then | |||
scan = false | |||
r = failure( "invalid", | |||
"'" .. seek .. "'", | |||
options ) | |||
end | |||
end | end | ||
else | |||
r = failure( "unknownRule", s, options ) | |||
end | end | ||
end | end | ||
else | |||
r = story | |||
end | end | ||
if scan then | if scan then | ||
Zeile 608: | Zeile 655: | ||
return r | return r | ||
end -- TemplatePar.count() | 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() | |||
Zeile 672: | Zeile 753: | ||
"2", | "2", | ||
"cat", | "cat", | ||
"low", | |||
"noError", | "noError", | ||
"template" }, | "template" }, | ||
Zeile 681: | Zeile 763: | ||
optional = fill( frame.args[ 2 ] ), | optional = fill( frame.args[ 2 ] ), | ||
cat = frame.args.cat, | cat = frame.args.cat, | ||
low = frame.args.low, | |||
noError = frame.args.noError, | noError = frame.args.noError, | ||
template = frame.args.template | template = frame.args.template | ||
Zeile 711: | Zeile 794: | ||
-- fold() | -- fold() | ||
-- trim() | -- trim() | ||
-- TemplatePar.valid() | -- TemplatePar.valid() | ||
local options = { mandatory = { "1" }, | local options = { mandatory = { "1" }, | ||
optional = { "2", | optional = { "2", | ||
"cat", | "cat", | ||
"low", | |||
"max", | "max", | ||
"min", | "min", | ||
Zeile 726: | Zeile 809: | ||
local s = trim( frame.args[ 2 ] ) | local s = trim( frame.args[ 2 ] ) | ||
options = { cat = frame.args.cat, | options = { cat = frame.args.cat, | ||
low = frame.args.low, | |||
noError = frame.args.noError, | noError = frame.args.noError, | ||
template = frame.args.template | template = frame.args.template |