Modul:TemplatePar: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
K
per Adminnanfrage
wp>Mabschaaf |
wp>Hephaion K (per Adminnanfrage) |
||
Zeile 1: | Zeile 1: | ||
--[=[ TemplatePar | --[=[ TemplatePar 2015-01-16 | ||
Template parameter utility | Template parameter utility | ||
* assert | * assert | ||
Zeile 28: | Zeile 28: | ||
missing = "#invoke:TemplatePar missing library", | missing = "#invoke:TemplatePar missing library", | ||
multiSpell = "Error in template * multiple spelling of parameter", | multiSpell = "Error in template * multiple spelling of parameter", | ||
noMSGnoCAT = "#invoke:TemplatePar neither message nor category", | |||
noname = "#invoke:TemplatePar missing parameter name", | noname = "#invoke:TemplatePar missing parameter name", | ||
notFound = "Error in template * missing page", | notFound = "Error in template * missing page", | ||
Zeile 46: | Zeile 46: | ||
minmax = "#invoke:TemplatePar min > max", | minmax = "#invoke:TemplatePar min > max", | ||
multiSpell = "Fehler bei Vorlage * Mehrere Parameter-Schreibweisen", | multiSpell = "Fehler bei Vorlage * Mehrere Parameter-Schreibweisen", | ||
noMSGnoCAT = "#invoke:TemplatePar weder Meldung noch Kategorie", | |||
noname = "#invoke:TemplatePar Parameter nicht angegeben", | noname = "#invoke:TemplatePar Parameter nicht angegeben", | ||
notFound = "Fehler bei Vorlage * Seite fehlt", | notFound = "Fehler bei Vorlage * Seite fehlt", | ||
Zeile 198: | Zeile 198: | ||
end | end | ||
if not r then | if not r then | ||
r = "((( | r = string.format( "(((%s)))", say ) | ||
end | end | ||
return r | return r | ||
Zeile 234: | Zeile 234: | ||
if type( options.template ) == "string" then | if type( options.template ) == "string" then | ||
if #options.template > 0 then | if #options.template > 0 then | ||
r = | r = string.format( "%s (%s)", r, options.template ) | ||
end | end | ||
end | end | ||
end | end | ||
if suspect then | if suspect then | ||
r = | r = string.format( "%s: %s", r, suspect ) | ||
end | end | ||
return r | return r | ||
Zeile 261: | Zeile 261: | ||
end | end | ||
if store then | if store then | ||
r = | r = string.format( "%s; %s", store, s ) | ||
else | else | ||
r = s | r = s | ||
Zeile 313: | Zeile 313: | ||
scan = false | scan = false | ||
if options.say then | if options.say then | ||
show = "'" | show = string.format( "'%s'", options.say ) | ||
end | end | ||
if abbr then | if abbr then | ||
Zeile 365: | Zeile 365: | ||
if r then | if r then | ||
if options.say then | if options.say then | ||
show = "'" | show = string.format( "'%s' %s", options.say, s ) | ||
else | else | ||
show = s | show = s | ||
Zeile 386: | Zeile 386: | ||
if not got then | if not got then | ||
if options.say then | if options.say then | ||
show = "'" | show = string.format( "'%s'", options.say ) | ||
end | end | ||
if abbr then | if abbr then | ||
Zeile 397: | Zeile 397: | ||
else | else | ||
r = failure( "badPattern", | r = failure( "badPattern", | ||
string.format( "%s *** %s", scan, got ), | |||
options ) | options ) | ||
end | end | ||
Zeile 525: | Zeile 525: | ||
local function finalize( submit, options ) | local function finalize( submit, options, frame ) | ||
-- Finalize message | -- Finalize message | ||
-- Precondition: | -- Precondition: | ||
-- submit -- string or false or nil; non-empty error message | -- submit -- string or false or nil; non-empty error message | ||
-- options -- table or nil; optional details | -- options -- table or nil; optional details | ||
-- options.format | -- options.format | ||
-- options.preview | |||
-- options.cat | -- options.cat | ||
-- options.template | -- options.template | ||
-- frame -- object, or false | |||
-- Postcondition: | -- Postcondition: | ||
-- Return string or false | -- Return string or false | ||
Zeile 539: | Zeile 540: | ||
-- factory() | -- factory() | ||
local r = false | local r = false | ||
if submit then | if submit then | ||
local opt, s | local opt, s | ||
local lazy = false | local lazy = false | ||
local show = false | local show = false | ||
if type( options ) == "table" then | if type( options ) == "table" then | ||
opt = options | opt = options | ||
show = opt.format | |||
lazy | lazy = ( show == "" or show == "0" or show == "-" ) | ||
s = opt.preview | |||
if type( s ) == "string" and | |||
s ~= "" and s ~= "0" and s ~= "-" then | |||
lazy = false | |||
if not frame then | |||
frame = mw.getCurrentFrame() | |||
end | |||
if frame:preprocess( "{{REVISIONID}}" ) == "" then | |||
if s ~= "1" then | |||
show = s | |||
end | |||
end | |||
end | end | ||
else | else | ||
opt = { } | opt = { } | ||
Zeile 559: | Zeile 566: | ||
if lazy then | if lazy then | ||
if not opt.cat then | if not opt.cat then | ||
r = | r = string.format( "%s %s", | ||
submit, factory( "noMSGnoCAT" ) ) | |||
end | end | ||
else | else | ||
Zeile 565: | Zeile 573: | ||
end | end | ||
if r and not lazy then | if r and not lazy then | ||
local i | |||
if not show or show == "*" then | if not show or show == "*" then | ||
show = "<span class=\"error\">@@@</span>" | show = "<span class=\"error\">@@@</span>" | ||
end | end | ||
i = show:find( "@@@", 1, true ) | |||
if i then | |||
-- No gsub() since r might contain "%3" (e.g. URL) | |||
r = string.format( "%s%s%s", | |||
show:sub( 1, i - 1 ), | |||
r, | |||
show:sub( i + 3 ) ) | |||
else | |||
r = show | |||
end | |||
end | end | ||
s = opt.cat | s = opt.cat | ||
Zeile 589: | Zeile 607: | ||
end -- for i | end -- for i | ||
end | end | ||
end | end | ||
return r | return r | ||
Zeile 644: | Zeile 653: | ||
end -- for k, v | end -- for k, v | ||
if r then | if r then | ||
r = failure( "unknown", | r = failure( "unknown", | ||
string.format( "'%s'", r ), | |||
options ) | |||
else -- all names valid | else -- all names valid | ||
local i, s | local i, s | ||
Zeile 758: | Zeile 769: | ||
local function form( light, options ) | local function form( light, options, frame ) | ||
-- Run parameter analysis on current environment | -- Run parameter analysis on current environment | ||
-- Precondition: | -- Precondition: | ||
Zeile 765: | Zeile 776: | ||
-- options.mandatory | -- options.mandatory | ||
-- options.optional | -- options.optional | ||
-- frame -- object, or false | |||
-- Postcondition: | -- Postcondition: | ||
-- Return string with error message as configured; | -- Return string with error message as configured; | ||
Zeile 796: | Zeile 808: | ||
end | end | ||
end | end | ||
return finalize( r, options ) | return finalize( r, options, frame ) | ||
end -- form() | end -- form() | ||
Zeile 822: | Zeile 834: | ||
if options.max < options.min then | if options.max < options.min then | ||
r = failure( "minmax", | r = failure( "minmax", | ||
string.format( "%d > %d", | |||
options.min, | |||
options.max ), | |||
options ) | options ) | ||
end | end | ||
Zeile 831: | Zeile 843: | ||
show = " <" .. options.min | show = " <" .. options.min | ||
if options.say then | if options.say then | ||
show = | show = string.format( "%s '%s'", show, options.say ) | ||
end | end | ||
r = failure( "tooShort", show, options ) | r = failure( "tooShort", show, options ) | ||
Zeile 844: | Zeile 856: | ||
show = " >" .. options.max | show = " >" .. options.max | ||
if options.say then | if options.say then | ||
show = | show = string.format( "%s '%s'", show, options.say ) | ||
end | end | ||
r = failure( "tooLong", show, options ) | r = failure( "tooLong", show, options ) | ||
Zeile 898: | Zeile 910: | ||
-- mw.text.trim() | -- mw.text.trim() | ||
-- failure() | -- failure() | ||
-- finalize() | |||
-- TemplatePar.valid() | |||
-- TemplatePar.assert() | -- TemplatePar.assert() | ||
local options = { mandatory = { "1" }, | local options = { mandatory = { "1" }, | ||
optional = { "2", | optional = { "2", | ||
Zeile 906: | Zeile 919: | ||
"max", | "max", | ||
"min", | "min", | ||
"format", | "format", | ||
"preview", | |||
"template" }, | "template" }, | ||
template = "#invoke: | template = string.format( "#invoke:%s|%s|", | ||
"TemplatePar", | |||
action ) | |||
} | } | ||
local r = form( false, options ) | local r = form( false, options, frame ) | ||
if not r then | if not r then | ||
local s | local s | ||
options = { cat = frame.args.cat, | options = { cat = frame.args.cat, | ||
low = frame.args.low, | low = frame.args.low, | ||
format = frame.args.format, | format = frame.args.format, | ||
preview = frame.args.preview, | |||
template = frame.args.template | template = frame.args.template | ||
} | } | ||
Zeile 942: | Zeile 957: | ||
end | end | ||
if r then | if r then | ||
r = finalize( r, options ) | r = finalize( r, options, frame ) | ||
else | else | ||
s = frame.args[ 1 ] or "" | s = frame.args[ 1 ] or "" | ||
Zeile 950: | Zeile 965: | ||
end | end | ||
if action == "valid" then | if action == "valid" then | ||
r = TemplatePar.valid( s, options ) | r = TemplatePar.valid( s, options, frame ) | ||
elseif action == "assert" then | elseif action == "assert" then | ||
r = TemplatePar.assert( s, "", options ) | r = TemplatePar.assert( s, "", options ) | ||
Zeile 977: | Zeile 992: | ||
if ( type( append ) == "string" ) then | if ( type( append ) == "string" ) then | ||
if ( append ~= "" ) then | if ( append ~= "" ) then | ||
r = | r = string.format( "%s<br />%s", append, r ) | ||
end | end | ||
else | else | ||
Zeile 999: | Zeile 1.014: | ||
-- Uses: | -- Uses: | ||
-- form() | -- form() | ||
return form( true, options ) | return form( true, options, false ) | ||
end -- TemplatePar.check() | end -- TemplatePar.check() | ||
Zeile 1.060: | Zeile 1.075: | ||
TemplatePar.valid = function ( access, options ) | TemplatePar.valid = function ( access, options, frame ) | ||
-- Check validity of one particular template parameter | -- Check validity of one particular template parameter | ||
-- Precondition: | -- Precondition: | ||
Zeile 1.066: | Zeile 1.081: | ||
-- string or number | -- string or number | ||
-- options -- table or nil; optional details | -- options -- table or nil; optional details | ||
-- frame -- object; #invoke environment | |||
-- Postcondition: | -- Postcondition: | ||
-- Return string with error message as configured; | -- Return string with error message as configured; | ||
Zeile 1.096: | Zeile 1.112: | ||
params = TemplatePar.downcase( options ) | params = TemplatePar.downcase( options ) | ||
else | else | ||
params = | params = frame:getParent() | ||
end | end | ||
r = formatted( params, access, options ) | r = formatted( params, access, options ) | ||
Zeile 1.102: | Zeile 1.118: | ||
r = failure( "noname", false, options ) | r = failure( "noname", false, options ) | ||
end | end | ||
return finalize( r, options ) | return finalize( r, options, frame ) | ||
end -- TemplatePar.valid() | end -- TemplatePar.valid() | ||
Zeile 1.116: | Zeile 1.132: | ||
-- Uses: | -- Uses: | ||
-- form() | -- form() | ||
return form( false, options ) | return form( false, options, false ) | ||
end -- TemplatePar.verify() | end -- TemplatePar.verify() | ||
Zeile 1.152: | Zeile 1.168: | ||
"cat", | "cat", | ||
"low", | "low", | ||
"format", | "format", | ||
"preview", | |||
"template" }, | "template" }, | ||
template = "#invoke:TemplatePar|check|" | template = "#invoke:TemplatePar|check|" | ||
} | } | ||
local r = form( false, options ) | local r = form( false, options, frame ) | ||
if not r then | if not r then | ||
options = { mandatory = fill( frame.args.all ), | options = { mandatory = fill( frame.args.all ), | ||
Zeile 1.163: | Zeile 1.179: | ||
cat = frame.args.cat, | cat = frame.args.cat, | ||
low = frame.args.low, | low = frame.args.low, | ||
format = frame.args.format, | format = frame.args.format, | ||
preview = frame.args.preview, | |||
template = frame.args.template | template = frame.args.template | ||
} | } | ||
r = form( true, options ) | r = form( true, options, frame ) | ||
end | end | ||
return r or "" | return r or "" | ||
Zeile 1.213: | Zeile 1.229: | ||
local options = { cat = frame.args.cat, | local options = { cat = frame.args.cat, | ||
low = frame.args.low, | low = frame.args.low, | ||
format = frame.args.format, | format = frame.args.format, | ||
preview = frame.args.preview, | |||
template = frame.args.template | template = frame.args.template | ||
} | } | ||
Zeile 1.250: | Zeile 1.266: | ||
end -- for k, v | end -- for k, v | ||
options.optional = s | options.optional = s | ||
r = form( true, options ) | r = form( true, options, frame ) | ||
end | end | ||
if not r then | if not r then | ||
Zeile 1.283: | Zeile 1.299: | ||
if lack then | if lack then | ||
if errMiss then | if errMiss then | ||
errMiss = | errMiss = string.format( "%s, '%s'", | ||
errMiss, k ) | |||
else | else | ||
errMiss = "'" | errMiss = string.format( "'%s'", k ) | ||
end | end | ||
elseif not errMiss then | elseif not errMiss then | ||
Zeile 1.301: | Zeile 1.318: | ||
r = failure( "invalid", errValues, options ) | r = failure( "invalid", errValues, options ) | ||
end | end | ||
r = finalize( r, options ) | r = finalize( r, options, frame ) | ||
end | end | ||
end | end |