Content deleted Content added
update |
update |
||
Line 1:
--[=[ TemplatePar 2013-05-
Template parameter utility
* check
Line 14:
local l10nDef = {}
l10nDef[ "en" ] = {
dupOpt = "#invoke:TemplatePar * repeated optional parameter",
dupRule = "#invoke:TemplatePar * parameter conflict key/pattern",
empty = "Error in template: undefined value for mandatory",
invalid = "Error in template: invalid parameter",
invalidPar = "
noname = "#invoke:TemplatePar * missing parameter name",
tooLong = "Error in template: parameter too long",
tooShort = "Error in template: parameter too short",
undefined = "Error in template: mandatory parameter missing",
unknown = "Error in template: unknown parameter name",
unknownRule = "
}
l10nDef[ "de" ] = {
dupOpt = "#invoke:TemplatePar * Optionsparameter wiederholt",
dupRule = "#invoke:TemplatePar * Parameterkonflikt key/pattern",
empty = "Fehler bei Vorlage: Pflichtparameter ohne Wert",
invalid = "Fehler bei Vorlage: Parameter ungültig",
invalidPar = "
noname = "#invoke:TemplatePar * Parametername nicht angegeben",
tooLong = "Fehler bei Vorlage: Parameter zu lang",
tooShort = "Fehler bei Vorlage: Parameter zu kurz",
undefined = "Fehler bei Vorlage: Pflichtparameter fehlt",
unknown = "Fehler bei Vorlage: Parametername unbekannt",
unknownRule = "
}
local Patterns = {
Line 41 ⟶ 47:
[ "ASCII+" ] = "^[ -~]+$",
[ "ASCII+1" ] = "^[!-~]+$",
[ "n" ] = "^%-?[0-9]*$",
[ "n>0" ] = "^[0-9]*[1-9][0-9]*$",
[ "N+" ] = "^%-?[1-9][0-9]*$",
Line 49 ⟶ 55:
[ "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]+$",
[ "+" ] = "%S"
}
Line 83 ⟶ 96:
-- > messagePrefix
-- > l10nDef
-- mw.language.getContentLanguage()
local c = mw.language.getContentLanguage():getCode()
local m = mw.message.new( messagePrefix .. say )
Line 101 ⟶ 116:
return r
end -- factory()
local function failsafe( story, scan )
-- Test for match (possibly user-defined with synrax error)
-- Precondition:
-- story -- string; parameter value
-- scan -- string; pattern
-- Postcondition:
-- Return nil, if not matching, else non-nil
-- Uses:
-- mw.ustring.match()
return mw.ustring.match( story, scan )
end -- failsafe()
Line 208 ⟶ 237:
-- Postcondition:
-- Return sequence table
-- Uses:
-- mw.text.split()
local r
if specified then
Line 395 ⟶ 426:
-- Uses:
-- > Patterns
-- mw.getCurrentFrame()()
-- frame:getParent()
-- failure()
--
▲ -- frame:getParent()
local r = false
local s
Line 405 ⟶ 437:
if type( options.pattern ) == "string" then
if options.key then
r = failure( "
else
scan = options.pattern
Line 417 ⟶ 449:
scan = Patterns[ s ]
if type( scan ) == "string" then
if s == "n" or s == "0,0" or s == "0.0" then
scan = false
r = failure( "invalid",
Line 430 ⟶ 462:
end
if scan then
if legal then
r = failure( "invalid", "'" .. seek .. "'", options )▼
if not got then
▲ r = failure( "invalid", "'" .. seek .. "'", options )
end
else
r = failure( "badPattern",
scan .. " *** " .. got,
options )
end
end
if options.min and not r then
if type( options.min ) == "number" then
if
if options.max < options.min then
r = failure( "minmax",
tostring( options.min )
.. " > " ..
tostring( options.max ),
options )
end
end
if #story < options.min and not r then
r = failure( "tooShort",
" <" .. options.min .. " '" .. seek .. "'",
Line 593 ⟶ 641:
-- Uses:
-- trim()
-- mw.ustring.gsub()
-- TemplatePar.valid()
local r = false
Line 604 ⟶ 653:
local sub = s:match( "^/(.*%S)/$" )
if type( sub ) == "string" then
sub = sub:gsub( "%%!", "|" )
sub = sub:gsub( "%%%(%(", "{{" )
sub = sub:gsub( "%%%)%)", "}}" )
options.pattern = sub
else
|