Modul:TemplatePar: Unterschied zwischen den Versionen
+pagename
wp>PerfektesChaos (update) |
wp>PerfektesChaos (+pagename) |
||
Zeile 1: | Zeile 1: | ||
--[=[ TemplatePar 2013-06- | --[=[ TemplatePar 2013-06-28 | ||
Template parameter utility | Template parameter utility | ||
* assert | * assert | ||
Zeile 53: | Zeile 53: | ||
local Patterns = { | local Patterns = { | ||
[ "ASCII" ] = "^[ -~]*$", | [ "ASCII" ] = "^[ -~]*$", | ||
[ "ASCII+" ] | [ "ASCII+" ] = "^[ -~]+$", | ||
[ "ASCII+1" ] = "^[!-~]+$", | [ "ASCII+1" ] = "^[!-~]+$", | ||
[ "n" ] | [ "n" ] = "^%-?[0-9]*$", | ||
[ "n>0" ] | [ "n>0" ] = "^[0-9]*[1-9][0-9]*$", | ||
[ "N+" ] | [ "N+" ] = "^%-?[1-9][0-9]*$", | ||
[ "N>0" ] | [ "N>0" ] = "^[1-9][0-9]*$", | ||
[ "x" ] | [ "x" ] = "^[0-9A-Fa-f]*$", | ||
[ "x+" ] | [ "x+" ] = "^[0-9A-Fa-f]+$", | ||
[ "X" ] | [ "X" ] = "^[0-9A-F]*$", | ||
[ "X+" ] | [ "X+" ] = "^[0-9A-F]+$", | ||
[ "0,0" ] | [ "0,0" ] = "^%-?[0-9]*,?[0-9]*$", | ||
[ "0,0+" ] | [ "0,0+" ] = "^%-?[0-9]+,[0-9]+$", | ||
[ "0,0+?" ] | [ "0,0+?" ] = "^%-?[0-9]+,?[0-9]*$", | ||
[ "0.0" ] | [ "0.0" ] = "^%-?[0-9]*%.?[0-9]*$", | ||
[ "0.0+" ] | [ "0.0+" ] = "^%-?[0-9]+%.[0-9]+$", | ||
[ "0.0+?" ] | [ "0.0+?" ] = "^%-?[0-9]+%.?[0-9]*$", | ||
[ ".0+" ] | [ ".0+" ] = "^%-?[0-9]*%.?[0-9]+$", | ||
[ "ID" ] | [ "ID" ] = "^[A-Za-z]?[A-Za-z_0-9]*$", | ||
[ "ID+" ] | [ "ID+" ] = "^[A-Za-z][A-Za-z_0-9]*$", | ||
[ "ABC" ] | [ "ABC" ] = "^[A-Z]*$", | ||
[ "ABC+" ] | [ "ABC+" ] = "^[A-Z]+$", | ||
[ "Abc" ] | [ "Abc" ] = "^[A-Z]*[a-z]*$", | ||
[ "Abc+" ] | [ "Abc+" ] = "^[A-Z][a-z]+$", | ||
[ "abc" ] | [ "abc" ] = "^[a-z]*$", | ||
[ "abc+" ] | [ "abc+" ] = "^[a-z]+$", | ||
[ "aBc+" ] | [ "aBc+" ] = "^[a-z]+[A-Z][A-Za-z]*$", | ||
[ "base64" ] | [ "base64" ] = "^[A-Za-z0-9%+/]*$", | ||
[ "base64+" ] = "^[A-Za-z0-9%+/]+$", | [ "base64+" ] = "^[A-Za-z0-9%+/]+$", | ||
[ "aa" ] | [ "aa" ] = "[%a%a].*[%a%a]", | ||
[ "+" ] | [ "pagename" ] = string.format( "^[^#<>%%[%%]|{}%s%-s%s]+$", | ||
1, 31, 127 ), | |||
[ "+" ] = "%S" | |||
} | } | ||
local patternCJK = false | local patternCJK = false | ||
Zeile 686: | Zeile 688: | ||
return r | return r | ||
end -- formatted() | end -- formatted() | ||
local function furnish( frame, action ) | |||
-- Prepare #invoke evaluation of .assert() or .valid() | |||
-- Precondition: | |||
-- frame -- object; #invoke environment | |||
-- action -- "assert" or "valid" | |||
-- Postcondition: | |||
-- Return string with error message or "" | |||
-- Uses: | |||
-- form() | |||
-- mw.text.trim() | |||
-- TemplatePar.assert() | |||
-- TemplatePar.valid() | |||
local options = { mandatory = { "1" }, | |||
optional = { "2", | |||
"cat", | |||
"low", | |||
"max", | |||
"min", | |||
"noError", | |||
"template" }, | |||
template = "#invoke:TemplatePar|".. action .. "|" | |||
} | |||
local r = form( false, options ) | |||
if not r then | |||
local s = mw.text.trim( frame.args[ 2 ] ) | |||
options = { cat = frame.args.cat, | |||
low = frame.args.low, | |||
noError = frame.args.noError, | |||
template = frame.args.template | |||
} | |||
if type( s ) == "string" then | |||
local sub = s:match( "^/(.*%S)/$" ) | |||
if type( sub ) == "string" then | |||
sub = sub:gsub( "%%!", "|" ) | |||
sub = sub:gsub( "%%%(%(", "{{" ) | |||
sub = sub:gsub( "%%%)%)", "}}" ) | |||
options.pattern = sub | |||
else | |||
options.key = s | |||
end | |||
end | |||
if type( frame.args.min ) == "string" then | |||
s = frame.args.min:match( "^%s*([0-9]+)%s*$" ) | |||
if s then | |||
options.min = tonumber( s ) | |||
else | |||
r = failure( "invalidPar", | |||
"min=" .. frame.args.min, | |||
options ) | |||
end | |||
end | |||
if type( frame.args.max ) == "string" then | |||
s = frame.args.max:match( "^%s*([1-9][0-9]*)%s*$" ) | |||
if s then | |||
options.max = tonumber( s ) | |||
else | |||
r = failure( "invalidPar", | |||
"max=" .. frame.args.max, | |||
options ) | |||
end | |||
end | |||
if r then | |||
r = finalize( r, options ) | |||
else | |||
s = frame.args[ 1 ] or "" | |||
r = tonumber( s ) | |||
if ( r ) then | |||
s = r | |||
end | |||
r = TemplatePar[ action ]( s, options ) | |||
end | |||
end | |||
return r or "" | |||
end -- furnish() | |||
Zeile 848: | Zeile 928: | ||
-- Provide external access | -- Provide external access | ||
local p = {} | local p = {} | ||
function p.assert( frame ) | |||
-- Perform parameter analysis on some single string | |||
-- Precondition: | |||
-- frame -- object; #invoke environment | |||
-- Postcondition: | |||
-- Return string with error message or "" | |||
-- Uses: | |||
-- furnish() | |||
return furnish( frame, "assert" ) | |||
end -- .assert() | |||
Zeile 913: | Zeile 1.006: | ||
-- Return string with error message or "" | -- Return string with error message or "" | ||
-- Uses: | -- Uses: | ||
-- | -- furnish() | ||
return furnish( frame, "valid" ) | |||
end -- .valid() | end -- .valid() | ||