Modul:TemplatePar: Unterschied zwischen den Versionen
update
wp>PerfektesChaos (update) |
wp>PerfektesChaos (update) |
||
Zeile 1: | Zeile 1: | ||
--[=[ TemplatePar 2013-05- | --[=[ TemplatePar 2013-05-20 | ||
Template parameter utility | Template parameter utility | ||
* check | * check | ||
Zeile 26: | Zeile 26: | ||
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", | ||
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 41: | Zeile 40: | ||
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 * | 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", | ||
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 242: | Zeile 240: | ||
g = g.args | g = g.args | ||
end | end | ||
for k, v in pairs( g ) do | if type( g ) == "table" then | ||
r = { } | |||
for k, v in pairs( g ) do | |||
if type( v ) == "string" then | |||
if v:match( "^%s*$" ) then | |||
v = false | |||
end | |||
else | |||
v = false | v = false | ||
end | end | ||
if type( k ) == "number" then | |||
k = tostring( k ) | |||
end | |||
r[ k ] = v | |||
end -- for k, v | |||
else | |||
r = g | |||
end | |||
return r | return r | ||
end -- fetch() | end -- fetch() | ||
Zeile 352: | Zeile 355: | ||
local function | local function fix( 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 | -- Merge two tables, create new sequence if both not empty | ||
-- Precondition: | -- Precondition: | ||
Zeile 365: | Zeile 449: | ||
-- fault() | -- fault() | ||
-- failure() | -- failure() | ||
-- | -- flat() | ||
local i, e, r, s | local i, e, r, s | ||
local base = options.mandatory | local base = options.mandatory | ||
Zeile 399: | Zeile 483: | ||
end | end | ||
end | end | ||
if options.low | if options.low and type( r ) == "table" then | ||
r = flat( r, options ) | |||
end | end | ||
return r | return r | ||
end -- | end -- fold() | ||
local function | local function form( light, options ) | ||
-- Run parameter analysis on current environment | -- Run parameter analysis on current environment | ||
-- Precondition: | -- Precondition: | ||
Zeile 479: | Zeile 502: | ||
-- false if valid | -- false if valid | ||
-- Uses: | -- Uses: | ||
-- | -- fold() | ||
-- failure() | -- failure() | ||
-- fetch() | -- fetch() | ||
Zeile 493: | Zeile 516: | ||
options.optional = { } | options.optional = { } | ||
end | end | ||
r = | r = fold( options ) | ||
else | else | ||
options = { } | options = { } | ||
Zeile 508: | Zeile 531: | ||
end | end | ||
return finalize( r, options ) | return finalize( r, options ) | ||
end -- | end -- form() | ||
Zeile 633: | Zeile 656: | ||
-- false if valid | -- false if valid | ||
-- Uses: | -- Uses: | ||
-- | -- form() | ||
return | return form( true, options ) | ||
end -- TemplatePar.check() | end -- TemplatePar.check() | ||
Zeile 670: | Zeile 693: | ||
-- fault() | -- fault() | ||
-- failure() | -- failure() | ||
local t = mw.getCurrentFrame():getParent() | local t = mw.getCurrentFrame():getParent() | ||
return flat( t.args, options ) | |||
end -- TemplatePar.downcase() | end -- TemplatePar.downcase() | ||
Zeile 730: | Zeile 737: | ||
-- false if valid | -- false if valid | ||
-- Uses: | -- Uses: | ||
-- | -- form() | ||
return | return form( false, options ) | ||
end -- TemplatePar.verify() | end -- TemplatePar.verify() | ||
Zeile 748: | Zeile 755: | ||
-- Return string with error message or "" | -- Return string with error message or "" | ||
-- Uses: | -- Uses: | ||
-- | -- form() | ||
-- fill() | -- fill() | ||
local options = { optional = { "1", | local options = { optional = { "1", | ||
Zeile 758: | Zeile 765: | ||
template = "#invoke:TemplatePar|check|" | template = "#invoke:TemplatePar|check|" | ||
} | } | ||
local r = | local r = form( false, options ) | ||
if not r then | if not r then | ||
options = { mandatory = fill( frame.args[ 1 ] ), | options = { mandatory = fill( frame.args[ 1 ] ), | ||
Zeile 767: | Zeile 774: | ||
template = frame.args.template | template = frame.args.template | ||
} | } | ||
r = | r = form( true, options ) | ||
end | end | ||
return r or "" | return r or "" | ||
Zeile 792: | Zeile 799: | ||
-- Return string with error message or "" | -- Return string with error message or "" | ||
-- Uses: | -- Uses: | ||
-- | -- form() | ||
-- trim() | -- trim() | ||
-- TemplatePar.valid() | -- TemplatePar.valid() | ||
Zeile 805: | Zeile 812: | ||
template = "#invoke:TemplatePar|valid|" | template = "#invoke:TemplatePar|valid|" | ||
} | } | ||
local r = | local r = form( false, options ) | ||
if not r then | if not r then | ||
local s = trim( frame.args[ 2 ] ) | local s = trim( frame.args[ 2 ] ) |