Modul:TemplatePar: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
update
wp>PerfektesChaos (update) |
wp>PerfektesChaos (update) |
||
Zeile 1: | Zeile 1: | ||
--[=[ TemplatePar 2013-05- | --[=[ TemplatePar 2013-05-04 | ||
Template parameter utility | Template parameter utility | ||
* check | * check | ||
Zeile 14: | Zeile 14: | ||
local l10nDef = {} | local l10nDef = {} | ||
l10nDef[ "en" ] = { | l10nDef[ "en" ] = { | ||
dupOpt = " | badPattern = "#invoke:TemplatePar * pattern syntax error", | ||
dupOpt = "#invoke:TemplatePar * repeated optional parameter", | |||
dupRule = "#invoke:TemplatePar * parameter conflict key/pattern", | |||
empty = "Error in template: undefined value for mandatory", | empty = "Error in template: undefined value for mandatory", | ||
invalid = "Error in template: invalid parameter", | invalid = "Error in template: invalid parameter", | ||
invalidPar = " | invalidPar = "#invoke:TemplatePar * invalid parameter", | ||
noname = " | minmax = "#invoke:TemplatePar * min > max", | ||
noname = "#invoke:TemplatePar * missing parameter name", | |||
tooLong = "Error in template: parameter too long", | tooLong = "Error in template: parameter too long", | ||
tooShort = "Error in template: parameter too short", | tooShort = "Error in template: parameter too short", | ||
undefined = "Error in template: mandatory parameter missing", | undefined = "Error in template: mandatory parameter missing", | ||
unknown = "Error in template: unknown parameter name", | unknown = "Error in template: unknown parameter name", | ||
unknownRule = " | unknownRule = "#invoke:TemplatePar * unknown rule" | ||
} | } | ||
l10nDef[ "de" ] = { | l10nDef[ "de" ] = { | ||
dupOpt = " | badPattern = "#invoke:TemplatePar * Syntaxfehler des pattern", | ||
dupOpt = "#invoke:TemplatePar * Optionsparameter wiederholt", | |||
dupRule = "#invoke:TemplatePar * Parameterkonflikt key/pattern", | |||
empty = "Fehler bei Vorlage: Pflichtparameter ohne Wert", | empty = "Fehler bei Vorlage: Pflichtparameter ohne Wert", | ||
invalid = "Fehler bei Vorlage: Parameter ungültig", | invalid = "Fehler bei Vorlage: Parameter ungültig", | ||
invalidPar = " | invalidPar = "#invoke:TemplatePar * Ungültiger Parameter", | ||
noname = " | minmax = "#invoke:TemplatePar * min > max", | ||
noname = "#invoke:TemplatePar * Parametername nicht angegeben", | |||
tooLong = "Fehler bei Vorlage: Parameter zu lang", | tooLong = "Fehler bei Vorlage: Parameter zu lang", | ||
tooShort = "Fehler bei Vorlage: Parameter zu kurz", | tooShort = "Fehler bei Vorlage: Parameter zu kurz", | ||
undefined = "Fehler bei Vorlage: Pflichtparameter fehlt", | undefined = "Fehler bei Vorlage: Pflichtparameter fehlt", | ||
unknown = "Fehler bei Vorlage: Parametername unbekannt", | unknown = "Fehler bei Vorlage: Parametername unbekannt", | ||
unknownRule = " | unknownRule = "#invoke:TemplatePar * Unbekannte Regel" | ||
} | } | ||
local Patterns = { | local Patterns = { | ||
Zeile 41: | Zeile 47: | ||
[ "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]*$", | ||
Zeile 49: | Zeile 55: | ||
[ "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-9]*%.?[0-9]+$", | |||
[ "+" ] = "%S" | [ "+" ] = "%S" | ||
} | } | ||
Zeile 83: | Zeile 96: | ||
-- > messagePrefix | -- > messagePrefix | ||
-- > l10nDef | -- > l10nDef | ||
-- mw.language.getContentLanguage() | |||
-- mw.message.new() | |||
local c = mw.language.getContentLanguage():getCode() | local c = mw.language.getContentLanguage():getCode() | ||
local m = mw.message.new( messagePrefix .. say ) | local m = mw.message.new( messagePrefix .. say ) | ||
Zeile 101: | Zeile 116: | ||
return r | return r | ||
end -- factory() | 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() | |||
Zeile 208: | Zeile 237: | ||
-- Postcondition: | -- Postcondition: | ||
-- Return sequence table | -- Return sequence table | ||
-- Uses: | |||
-- mw.text.split() | |||
local r | local r | ||
if specified then | if specified then | ||
Zeile 395: | Zeile 426: | ||
-- Uses: | -- Uses: | ||
-- > Patterns | -- > Patterns | ||
-- mw.getCurrentFrame()() | |||
-- frame:getParent() | |||
-- failure() | -- failure() | ||
-- | -- failsafe() | ||
local r = false | local r = false | ||
local s | local s | ||
Zeile 405: | Zeile 437: | ||
if type( options.pattern ) == "string" then | if type( options.pattern ) == "string" then | ||
if options.key then | if options.key then | ||
r = failure( " | r = failure( "dupRule", false, options ) | ||
else | else | ||
scan = options.pattern | scan = options.pattern | ||
Zeile 417: | Zeile 449: | ||
scan = Patterns[ s ] | scan = Patterns[ s ] | ||
if type( scan ) == "string" then | if type( scan ) == "string" then | ||
if s == "n" then | if s == "n" or s == "0,0" or s == "0.0" then | ||
if not story:match( "[0-9]" ) then | |||
scan = false | scan = false | ||
r = failure( "invalid", | r = failure( "invalid", | ||
Zeile 430: | Zeile 462: | ||
end | end | ||
if scan then | if scan then | ||
local legal, got = pcall( failsafe, story, scan ) | |||
r = failure( "invalid", "'" .. seek .. "'", options ) | if legal then | ||
if not got then | |||
r = failure( "invalid", "'" .. seek .. "'", options ) | |||
end | |||
else | |||
r = failure( "badPattern", | |||
scan .. " *** " .. got, | |||
options ) | |||
end | end | ||
end | end | ||
if options.min and not r then | if options.min and not r then | ||
if type( options.min ) == "number" then | if type( options.min ) == "number" then | ||
if #story < options.min then | if type( options.max ) == "number" then | ||
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", | r = failure( "tooShort", | ||
" <" .. options.min .. " '" .. seek .. "'", | " <" .. options.min .. " '" .. seek .. "'", | ||
Zeile 593: | Zeile 641: | ||
-- Uses: | -- Uses: | ||
-- trim() | -- trim() | ||
-- mw.ustring.gsub() | |||
-- TemplatePar.valid() | -- TemplatePar.valid() | ||
local r = false | local r = false | ||
Zeile 604: | Zeile 653: | ||
local sub = s:match( "^/(.*%S)/$" ) | local sub = s:match( "^/(.*%S)/$" ) | ||
if type( sub ) == "string" then | if type( sub ) == "string" then | ||
sub = sub:gsub( "%%!", "|" ) | |||
sub = sub:gsub( "%%%(%(", "{{" ) | |||
sub = sub:gsub( "%%%)%)", "}}" ) | |||
options.pattern = sub | options.pattern = sub | ||
else | else |