Module:TemplatePar: Difference between revisions

Content deleted Content added
update
+pagename
Line 1:
--[=[ TemplatePar 2013-06-1028
Template parameter utility
* assert
Line 53:
local Patterns = {
[ "ASCII" ] = "^[ -~]*$",
[ "ASCII+" ] = "^[ -~]+$",
[ "ASCII+1" ] = "^[!-~]+$",
[ "n" ] = "^%-?[0-9]*$",
[ "n>0" ] = "^[0-9]*[1-9][0-9]*$",
[ "N+" ] = "^%-?[1-9][0-9]*$",
[ "N>0" ] = "^[1-9][0-9]*$",
[ "x" ] = "^[0-9A-Fa-f]*$",
[ "x+" ] = "^[0-9A-Fa-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-9]*%.?[0-9]+$",
[ "ID" ] = "^[A-Za-z]?[A-Za-z_0-9]*$",
[ "ID+" ] = "^[A-Za-z][A-Za-z_0-9]*$",
[ "ABC" ] = "^[A-Z]*$",
[ "ABC+" ] = "^[A-Z]+$",
[ "Abc" ] = "^[A-Z]*[a-z]*$",
[ "Abc+" ] = "^[A-Z][a-z]+$",
[ "abc" ] = "^[a-z]*$",
[ "abc+" ] = "^[a-z]+$",
[ "aBc+" ] = "^[a-z]+[A-Z][A-Za-z]*$",
[ "base64" ] = "^[A-Za-z0-9%+/]*$",
[ "base64+" ] = "^[A-Za-z0-9%+/]+$",
[ "aa" ] = "[%a%a].*[%a%a]",
[ "+pagename" ] = string.format( "^[^#<>%S%[%%]|{}%s%-s%s]+$",
1, 31, 127 ),
[ "+" ] = "%S"
}
local patternCJK = false
Line 686 ⟶ 688:
return r
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()
 
 
Line 848 ⟶ 928:
-- Provide external access
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()
 
 
Line 913 ⟶ 1,006:
-- Return string with error message or ""
-- Uses:
-- formfurnish()
return furnish( frame, "valid" )
-- mw.text.trim()
-- TemplatePar.valid()
local options = { mandatory = { "1" },
optional = { "2",
"cat",
"low",
"max",
"min",
"noError",
"template" },
template = "#invoke:TemplatePar|valid|"
}
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.valid( s, options )
end
end
return r or ""
end -- .valid()