Modul:TemplatePar: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
bugfix (valid unnamed) + kl. updates
wp>PerfektesChaos (updates + match) |
wp>PerfektesChaos (bugfix (valid unnamed) + kl. updates) |
||
Zeile 1: | Zeile 1: | ||
--[=[ TemplatePar 2013- | --[=[ TemplatePar 2013-09-06 | ||
Template parameter utility | Template parameter utility | ||
* assert | * assert | ||
Zeile 5: | Zeile 5: | ||
* count | * count | ||
* countNotEmpty | * countNotEmpty | ||
* downcase | * downcase() | ||
* match | * match | ||
* valid | * valid | ||
* verify | * verify() | ||
* TemplatePar() | * TemplatePar() | ||
]=] | ]=] | ||
Zeile 18: | Zeile 18: | ||
local messagePrefix = "lua-module-TemplatePar-" | local messagePrefix = "lua-module-TemplatePar-" | ||
local l10nDef = {} | local l10nDef = {} | ||
l10nDef | l10nDef.en = { | ||
badPattern = "#invoke:TemplatePar pattern syntax error", | badPattern = "#invoke:TemplatePar pattern syntax error", | ||
dupOpt = "#invoke:TemplatePar repeated optional parameter", | dupOpt = "#invoke:TemplatePar repeated optional parameter", | ||
Zeile 35: | Zeile 35: | ||
unknownRule = "#invoke:TemplatePar unknown rule" | unknownRule = "#invoke:TemplatePar unknown rule" | ||
} | } | ||
l10nDef | l10nDef.de = { | ||
badPattern = "#invoke:TemplatePar Syntaxfehler des pattern", | badPattern = "#invoke:TemplatePar Syntaxfehler des pattern", | ||
dupOpt = "#invoke:TemplatePar Optionsparameter wiederholt", | dupOpt = "#invoke:TemplatePar Optionsparameter wiederholt", | ||
Zeile 53: | Zeile 53: | ||
} | } | ||
local Patterns = { | local Patterns = { | ||
[ "ASCII" ] | [ "ASCII" ] = "^[ -~]*$", | ||
[ "ASCII+" ] = "^[ -~]+$", | [ "ASCII+" ] = "^[ -~]+$", | ||
[ "ASCII+1" ] = "^[!-~]+$", | [ "ASCII+1" ] = "^[!-~]+$", | ||
[ "n" ] = "^%-?[0-9]*$", | [ "n" ] = "^[%-]?[0-9]*$", | ||
[ "n>0" ] = "^[0-9]*[1-9][0-9]*$", | [ "n>0" ] = "^[0-9]*[1-9][0-9]*$", | ||
[ "N+" ] = "^%-?[1-9][0-9]*$", | [ "N+" ] = "^[%-]?[1-9][0-9]*$", | ||
[ "N>0" ] = "^[1-9][0-9]*$", | [ "N>0" ] = "^[1-9][0-9]*$", | ||
[ "x" ] = "^[0-9A-Fa-f]*$", | [ "x" ] = "^[0-9A-Fa-f]*$", | ||
Zeile 64: | Zeile 64: | ||
[ "X" ] = "^[0-9A-F]*$", | [ "X" ] = "^[0-9A-F]*$", | ||
[ "X+" ] = "^[0-9A-F]+$", | [ "X+" ] = "^[0-9A-F]+$", | ||
[ "0,0" ] = "^%-?[0-9]*,?[0-9]*$", | [ "0,0" ] = "^[%-]?[0-9]*,?[0-9]*$", | ||
[ "0,0+" ] = "^%-?[0-9]+,[0-9]+$", | [ "0,0+" ] = "^[%-]?[0-9]+,[0-9]+$", | ||
[ "0,0+?" ] = "^%-?[0-9]+,?[0-9]*$", | [ "0,0+?" ] = "^[%-]?[0-9]+,?[0-9]*$", | ||
[ "0.0" ] = "^%-?[0-9]*%.?[0-9]*$", | [ "0.0" ] = "^[%-]?[0-9]*[%.]?[0-9]*$", | ||
[ "0.0+" ] = "^%-?[0-9]+%.[0-9]+$", | [ "0.0+" ] = "^[%-]?[0-9]+%.[0-9]+$", | ||
[ "0.0+?" ] = "^%-?[0-9]+%.?[0-9]*$", | [ "0.0+?" ] = "^[%-]?[0-9]+[%.]?[0-9]*$", | ||
[ ".0+" ] = "^%-?[0-9]*%.?[0-9]+$", | [ ".0+" ] = "^[%-]?[0-9]*[%.]?[0-9]+$", | ||
[ "ID" ] = "^[A-Za-z]?[A-Za-z_0-9]*$", | [ "ID" ] = "^[A-Za-z]?[A-Za-z_0-9]*$", | ||
[ "ID+" ] = "^[A-Za-z][A-Za-z_0-9]*$", | [ "ID+" ] = "^[A-Za-z][A-Za-z_0-9]*$", | ||
Zeile 83: | Zeile 83: | ||
[ "base64+" ] = "^[A-Za-z0-9%+/]+$", | [ "base64+" ] = "^[A-Za-z0-9%+/]+$", | ||
[ "aa" ] = "[%a%a].*[%a%a]", | [ "aa" ] = "[%a%a].*[%a%a]", | ||
[ "pagename" ] = string.format( "^[^#<>%%[%%]|{}% | [ "pagename" ] = string.format( "^[^#<>%%[%%]|{}%c-%c%c]+$", | ||
1, 31, 127 ), | 1, 31, 127 ), | ||
[ "+" ] = "%S" | [ "+" ] = "%S" | ||
Zeile 265: | Zeile 265: | ||
end | end | ||
elseif s ~= "*" then | elseif s ~= "*" then | ||
local op, n, plus = s:match( "([<!>]=?)([-0-9][%S]*)(+?)" ) | local op, n, plus = s:match( "([<!=>]=?)([-0-9][%S]*)(+?)" ) | ||
if op then | if op then | ||
n = tonumber( n ) | n = tonumber( n ) | ||
Zeile 279: | Zeile 279: | ||
elseif op == ">=" then | elseif op == ">=" then | ||
i = ( i >= n ) | i = ( i >= n ) | ||
elseif op == "==" then | |||
i = ( i == n ) | |||
elseif op == "!=" then | elseif op == "!=" then | ||
i = ( i ~= n ) | i = ( i ~= n ) | ||
Zeile 848: | Zeile 850: | ||
s = r | s = r | ||
end | end | ||
r = TemplatePar | if action == "valid" then | ||
r = TemplatePar.valid( s, options ) | |||
elseif action == "assert" then | |||
r = TemplatePar.assert( s, "", options ) | |||
end | |||
end | end | ||
end | end | ||
Zeile 959: | Zeile 965: | ||
-- Precondition: | -- Precondition: | ||
-- access -- id of parameter in template transclusion | -- access -- id of parameter in template transclusion | ||
-- string or number | |||
-- options -- table or nil; optional details | -- options -- table or nil; optional details | ||
-- Postcondition: | -- Postcondition: | ||
Zeile 971: | Zeile 978: | ||
-- failure() | -- failure() | ||
-- finalize() | -- finalize() | ||
local r | local r = type( access ) | ||
if r == "string" then | |||
r = mw.text.trim( access ) | r = mw.text.trim( access ) | ||
if #r == 0 then | if #r == 0 then | ||
r = false | r = false | ||
end | end | ||
elseif r == "number" then | |||
r = access | |||
else | |||
r = false | |||
end | end | ||
if r then | if r then |