Bearbeiten von „Modul:TemplatePar“
Zur Navigation springen
Zur Suche springen
Die Bearbeitung kann rückgängig gemacht werden. Bitte prüfe den Vergleich unten, um sicherzustellen, dass du dies tun möchtest, und veröffentliche dann unten deine Änderungen, um die Bearbeitung rückgängig zu machen.
Aktuelle Version | Dein Text | ||
Zeile 1: | Zeile 1: | ||
--[=[ TemplatePar 2014-12-22 | |||
-- | |||
Template parameter utility | Template parameter utility | ||
* assert | * assert | ||
Zeile 11: | Zeile 6: | ||
* countNotEmpty | * countNotEmpty | ||
* downcase() | * downcase() | ||
* match | * match | ||
* valid | * valid | ||
* verify() | * verify() | ||
* TemplatePar() | * TemplatePar() | ||
]=] | ]=] | ||
Zeile 22: | Zeile 15: | ||
-- Module globals | -- Module globals | ||
local TemplatePar = { } | |||
local MessagePrefix = "lua-module-TemplatePar-" | local MessagePrefix = "lua-module-TemplatePar-" | ||
local L10nDef = {} | local L10nDef = {} | ||
Zeile 34: | Zeile 28: | ||
missing = "#invoke:TemplatePar missing library", | missing = "#invoke:TemplatePar missing library", | ||
multiSpell = "Error in template * multiple spelling of parameter", | multiSpell = "Error in template * multiple spelling of parameter", | ||
noErrorCat = "#invoke:TemplatePar noError and missing category", | |||
noname = "#invoke:TemplatePar missing parameter name", | noname = "#invoke:TemplatePar missing parameter name", | ||
notFound = "Error in template * missing page", | notFound = "Error in template * missing page", | ||
Zeile 42: | Zeile 36: | ||
unknown = "Error in template * unknown parameter name", | unknown = "Error in template * unknown parameter name", | ||
unknownRule = "#invoke:TemplatePar unknown rule" | unknownRule = "#invoke:TemplatePar unknown rule" | ||
} | |||
L10nDef.de = { | |||
badPattern = "#invoke:TemplatePar Syntaxfehler des pattern", | |||
dupOpt = "#invoke:TemplatePar Optionsparameter wiederholt", | |||
dupRule = "#invoke:TemplatePar Konflikt key/pattern", | |||
empty = "Fehler bei Vorlage * Pflichtparameter ohne Wert", | |||
invalid = "Fehler bei Vorlage * Parameter ungültig", | |||
invalidPar = "#invoke:TemplatePar Ungültiger Parameter", | |||
minmax = "#invoke:TemplatePar min > max", | |||
multiSpell = "Fehler bei Vorlage * Mehrere Parameter-Schreibweisen", | |||
noErrorCat = "#invoke:TemplatePar noError und keine Kategorie", | |||
noname = "#invoke:TemplatePar Parameter nicht angegeben", | |||
notFound = "Fehler bei Vorlage * Seite fehlt", | |||
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 = "#invoke:TemplatePar Unbekannte Regel" | |||
} | } | ||
local Patterns = { | local Patterns = { | ||
Zeile 78: | Zeile 90: | ||
[ "pagename" ] = string.format( "^[^#<>%%[%%]|{}%c-%c%c]+$", | [ "pagename" ] = string.format( "^[^#<>%%[%%]|{}%c-%c%c]+$", | ||
1, 31, 127 ), | 1, 31, 127 ), | ||
[ "+" ] = "%S" | [ "+" ] = "%S" | ||
} | } | ||
Zeile 128: | Zeile 137: | ||
-- Return error keyword, or false | -- Return error keyword, or false | ||
-- Uses: | -- Uses: | ||
-- Module:FileMedia | -- Module:FileMedia | ||
-- FileMedia.isType() | -- FileMedia.isType() | ||
local r | local r | ||
if attempt and attempt ~= "" then | if attempt and attempt ~= "" then | ||
local lucky, FileMedia = pcall( require, "Module:FileMedia" ) | |||
if type( FileMedia ) == "table" then | if type( FileMedia ) == "table" then | ||
FileMedia = FileMedia.FileMedia() | |||
local s, live = accept:match( "^([a-z]+)(:?)%+?$" ) | local s, live = accept:match( "^([a-z]+)(:?)%+?$" ) | ||
if live then | if live then | ||
Zeile 191: | Zeile 190: | ||
local l10n = L10nDef[ c ] | local l10n = L10nDef[ c ] | ||
if not l10n then | if not l10n then | ||
l10n = L10nDef[ "en" ] | |||
end | end | ||
r = l10n[ say ] | r = l10n[ say ] | ||
else | else | ||
m:inLanguage( c ) | m:inLanguage( c ) | ||
Zeile 211: | Zeile 198: | ||
end | end | ||
if not r then | if not r then | ||
r = | r = "(((".. say .. ")))" | ||
end | end | ||
return r | return r | ||
end -- factory() | end -- factory() | ||
local function failsafe( story, scan ) | |||
-- Test for match (possibly user-defined with syntax 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 233: | Zeile 234: | ||
if type( options.template ) == "string" then | if type( options.template ) == "string" then | ||
if #options.template > 0 then | if #options.template > 0 then | ||
r = | r = r .. " (" .. options.template .. ")" | ||
end | end | ||
end | end | ||
end | end | ||
if suspect then | if suspect then | ||
r = | r = r .. ": " .. suspect | ||
end | end | ||
return r | return r | ||
end -- failure() | end -- failure() | ||
Zeile 380: | Zeile 261: | ||
end | end | ||
if store then | if store then | ||
r = | r = store .. "; " .. s | ||
else | else | ||
r = s | r = s | ||
Zeile 406: | Zeile 287: | ||
-- mw.text.trim() | -- mw.text.trim() | ||
-- facility() | -- facility() | ||
-- | -- failsafe() | ||
-- containsCJK() | -- containsCJK() | ||
local r = false | local r = false | ||
Zeile 429: | Zeile 310: | ||
if type( scan ) == "string" then | if type( scan ) == "string" then | ||
if s == "n" or s == "0,0" or s == "0.0" then | if s == "n" or s == "0,0" or s == "0.0" then | ||
if not analyze:match( "[0-9] | if not analyze:match( "[0-9]" ) then | ||
scan = false | scan = false | ||
if options.say then | if options.say then | ||
show = | show = "'" .. options.say .. "'" | ||
end | end | ||
if abbr then | if abbr then | ||
Zeile 475: | Zeile 355: | ||
r = facility( s, analyze ) | r = facility( s, analyze ) | ||
n = true | n = true | ||
elseif s:match( " | elseif s:match( "langW?%+?" ) then | ||
n = "lang" | |||
n = | -- lang lang+ | ||
-- langW langW+ | |||
end | end | ||
if not n and not r then | if not n and not r then | ||
r = "unknownRule" | r = "unknownRule" | ||
Zeile 490: | Zeile 365: | ||
if r then | if r then | ||
if options.say then | if options.say then | ||
show = | show = "'" .. options.say .. "' " .. s | ||
else | else | ||
show = s | show = s | ||
Zeile 503: | Zeile 378: | ||
end | end | ||
if scan then | if scan then | ||
local legal, got = pcall( | local legal, got = pcall( failsafe, analyze, scan ) | ||
if legal then | if legal then | ||
if not got then | if not got then | ||
Zeile 511: | Zeile 386: | ||
if not got then | if not got then | ||
if options.say then | if options.say then | ||
show = | show = "'" .. options.say .. "'" | ||
end | end | ||
if abbr then | if abbr then | ||
Zeile 522: | Zeile 397: | ||
else | else | ||
r = failure( "badPattern", | r = failure( "badPattern", | ||
scan .. " *** " .. got, | |||
options ) | options ) | ||
end | end | ||
Zeile 538: | Zeile 413: | ||
-- Postcondition: | -- Postcondition: | ||
-- Return true iff found | -- Return true iff found | ||
local k, v | local k, v | ||
for k, v in pairs( haystack ) do | for k, v in pairs( haystack ) do | ||
if k == needle then | if k == needle then | ||
return true | |||
end | end | ||
end -- for k, v | end -- for k, v | ||
return | return false | ||
end -- fed() | end -- fed() | ||
Zeile 566: | Zeile 441: | ||
g = TemplatePar.downcase( options ) | g = TemplatePar.downcase( options ) | ||
else | else | ||
g = mw.getCurrentFrame() | |||
if light then | if light then | ||
g = g:getParent() | g = g:getParent() | ||
Zeile 611: | Zeile 485: | ||
if type( sub ) == "string" then | if type( sub ) == "string" then | ||
sub = sub:gsub( "%%!", "|" ) | sub = sub:gsub( "%%!", "|" ) | ||
sub = sub:gsub( "%%%(%(", "{{" ) | |||
sub = sub:gsub( "%%%)%)", "}}" ) | |||
options.pattern = sub | options.pattern = sub | ||
options.key = nil | options.key = nil | ||
Zeile 657: | Zeile 530: | ||
-- submit -- string or false or nil; non-empty error message | -- submit -- string or false or nil; non-empty error message | ||
-- options -- table or nil; optional details | -- options -- table or nil; optional details | ||
-- options.noError -- OBSOLETE | |||
-- options.format | -- options.format | ||
-- options.cat | -- options.cat | ||
-- options.template | -- options.template | ||
Zeile 666: | Zeile 539: | ||
-- factory() | -- factory() | ||
local r = false | local r = false | ||
local lapsus -- OBSOLETE | |||
if submit then | if submit then | ||
local opt, s | local opt, s | ||
local lazy = false | |||
local show = false | |||
lapsus = false -- OBSOLETE | |||
if type( options ) == "table" then | if type( options ) == "table" then | ||
opt | opt = options | ||
if opt.noError then -- OBSOLETE | |||
lazy = true | |||
lapsus = true | |||
end | |||
show = opt.format | show = opt.format | ||
lazy = ( show == "" or show == "0" or show == "-" ) | lazy = lazy or | ||
( show == "" or show == "0" or show == "-" ) | |||
else | else | ||
opt = { } | opt = { } | ||
Zeile 700: | Zeile 559: | ||
if lazy then | if lazy then | ||
if not opt.cat then | if not opt.cat then | ||
r = | r = submit .. " " .. factory( "noErrorCat" ) -- OBSOLETE | ||
end | end | ||
else | else | ||
Zeile 707: | Zeile 565: | ||
end | end | ||
if r and not lazy then | if r and not lazy then | ||
if not show or show == "*" then | if not show or show == "*" then | ||
show = "<span class=\"error\">@@@</span>" | |||
end | end | ||
r = mw.ustring.gsub( show, "@@@", r ) | |||
end | end | ||
s = opt.cat | s = opt.cat | ||
if type( s ) == "string" then | if type( s ) == "string" then | ||
if not r then | |||
if | r = "" | ||
end | |||
if s:find( "@@@" ) then | |||
if type( opt.template ) == "string" then | |||
s = s:gsub( "@@@", opt.template ) | |||
end | end | ||
end | end | ||
local i | |||
local cats = mw.text.split( s, "%s*#%s*" ) | |||
if | for i = 1, #cats do | ||
s = mw.text.trim( cats[ i ] ) | |||
if #s > 0 then | |||
r = string.format( "%s[[Category:%s]]", r, s ) | |||
end | end | ||
end -- for i | |||
end | |||
elseif type( options ) == "table" then | |||
lapsus = options.noError -- OBSOLETE | |||
end | |||
if lapsus then -- OBSOLETE | |||
if not r then | |||
r = "" | |||
end | end | ||
r = r .. | |||
"[[Category:Wikipedia:Wartung Modul:TemplatePar noError]]" | |||
end | end | ||
return r | return r | ||
Zeile 834: | Zeile 644: | ||
end -- for k, v | end -- for k, v | ||
if r then | if r then | ||
r = failure( "unknown", | r = failure( "unknown", "'" .. r .. "'", options ) | ||
else -- all names valid | else -- all names valid | ||
local i, s | local i, s | ||
Zeile 950: | Zeile 758: | ||
local function form( light, options | local function form( light, options ) | ||
-- Run parameter analysis on current environment | -- Run parameter analysis on current environment | ||
-- Precondition: | -- Precondition: | ||
Zeile 957: | Zeile 765: | ||
-- options.mandatory | -- options.mandatory | ||
-- options.optional | -- options.optional | ||
-- Postcondition: | -- Postcondition: | ||
-- Return string with error message as configured; | -- Return string with error message as configured; | ||
-- false if valid | -- false if valid | ||
-- Uses: | -- Uses: | ||
-- fold() | -- fold() | ||
-- fetch() | -- fetch() | ||
Zeile 968: | Zeile 774: | ||
-- finalize() | -- finalize() | ||
local duty, r | local duty, r | ||
if type( options ) == "table" then | if type( options ) == "table" then | ||
if type( options.mandatory ) ~= "table" then | if type( options.mandatory ) ~= "table" then | ||
Zeile 1.014: | Zeile 815: | ||
-- feasible() | -- feasible() | ||
-- failure() | -- failure() | ||
local r = feasible( analyze, options, false ) | local r = feasible( analyze, options, false ) | ||
local show | local show | ||
if options.min and not r then | if options.min and not r then | ||
Zeile 1.021: | Zeile 822: | ||
if options.max < options.min then | if options.max < options.min then | ||
r = failure( "minmax", | r = failure( "minmax", | ||
tostring( options.min ) | |||
.. " > " .. | |||
tostring( options.max ), | |||
options ) | options ) | ||
end | end | ||
Zeile 1.030: | Zeile 831: | ||
show = " <" .. options.min | show = " <" .. options.min | ||
if options.say then | if options.say then | ||
show = | show = show .. " '" .. options.say .. "'" | ||
end | end | ||
r = failure( "tooShort", show, options ) | r = failure( "tooShort", show, options ) | ||
Zeile 1.043: | Zeile 844: | ||
show = " >" .. options.max | show = " >" .. options.max | ||
if options.say then | if options.say then | ||
show = | show = show .. " '" .. options.say .. "'" | ||
end | end | ||
r = failure( "tooLong", show, options ) | r = failure( "tooLong", show, options ) | ||
Zeile 1.066: | Zeile 867: | ||
-- false if valid or no answer permitted | -- false if valid or no answer permitted | ||
-- Uses: | -- Uses: | ||
-- format() | -- format() | ||
-- failure() | -- failure() | ||
local r = false | local r = false | ||
if type( assignment ) == "table" then | if type( assignment ) == "table" then | ||
local story = assignment.args[ access ] | local story = assignment.args[ access ] | ||
if type( | if type( story ) == "string" then | ||
if type( options ) ~= "table" then | |||
options = { } | |||
end | |||
options.say = access | |||
r = format( story, options ) | |||
else | |||
r = failure( "invalid", access, options ) | |||
end | end | ||
end | end | ||
return r | return r | ||
Zeile 1.095: | Zeile 896: | ||
-- Uses: | -- Uses: | ||
-- form() | -- form() | ||
-- mw.text.trim() | |||
-- failure() | -- failure() | ||
-- | -- TemplatePar.assert() | ||
-- TemplatePar.valid() | -- TemplatePar.valid() | ||
local options = { mandatory = { "1" }, | local options = { mandatory = { "1" }, | ||
optional = { "2", | optional = { "2", | ||
"cat", | "cat", | ||
"low", | "low", | ||
"max", | "max", | ||
"min", | "min", | ||
"noError", -- OBSOLETE | |||
"format", | "format", | ||
"template" }, | "template" }, | ||
template = | template = "#invoke:TemplatePar|".. action .. "|" | ||
} | } | ||
local r = form( false, options | local r = form( false, options ) | ||
if not r then | if not r then | ||
local s | local s | ||
options = { cat = frame.args.cat, | options = { cat = frame.args.cat, | ||
low = frame.args.low, | low = frame.args.low, | ||
noError = frame.args.noError, -- OBSOLETE | |||
format = frame.args.format, | format = frame.args.format, | ||
template = frame.args.template | template = frame.args.template | ||
} | } | ||
Zeile 1.180: | Zeile 977: | ||
if ( type( append ) == "string" ) then | if ( type( append ) == "string" ) then | ||
if ( append ~= "" ) then | if ( append ~= "" ) then | ||
r = | r = append .. "<br />" .. r | ||
end | end | ||
else | else | ||
Zeile 1.202: | Zeile 999: | ||
-- Uses: | -- Uses: | ||
-- form() | -- form() | ||
return form( true, options | return form( true, options ) | ||
end -- TemplatePar.check() | end -- TemplatePar.check() | ||
Zeile 1.260: | Zeile 1.057: | ||
return flat( t.args, options ) | return flat( t.args, options ) | ||
end -- TemplatePar.downcase() | end -- TemplatePar.downcase() | ||
Zeile 1.309: | Zeile 1.070: | ||
-- false if valid or no answer permitted | -- false if valid or no answer permitted | ||
-- Uses: | -- Uses: | ||
-- mw.text.trim() | -- mw.text.trim() | ||
-- TemplatePar.downcase() | -- TemplatePar.downcase() | ||
-- mw.getCurrentFrame() | |||
-- frame:getParent() | -- frame:getParent() | ||
-- formatted() | -- formatted() | ||
Zeile 1.335: | Zeile 1.096: | ||
params = TemplatePar.downcase( options ) | params = TemplatePar.downcase( options ) | ||
else | else | ||
params = mw.getCurrentFrame():getParent() | |||
end | end | ||
r = formatted( params, access, options ) | r = formatted( params, access, options ) | ||
Zeile 1.344: | Zeile 1.102: | ||
r = failure( "noname", false, options ) | r = failure( "noname", false, options ) | ||
end | end | ||
return finalize( r, options | return finalize( r, options ) | ||
end -- TemplatePar.valid() | end -- TemplatePar.valid() | ||
Zeile 1.358: | Zeile 1.116: | ||
-- Uses: | -- Uses: | ||
-- form() | -- form() | ||
return form( false, options | return form( false, options ) | ||
end -- TemplatePar.verify() | end -- TemplatePar.verify() | ||
Zeile 1.377: | Zeile 1.135: | ||
-- furnish() | -- furnish() | ||
return furnish( frame, "assert" ) | return furnish( frame, "assert" ) | ||
end -- | end -- .assert() | ||
Zeile 1.393: | Zeile 1.151: | ||
"opt", | "opt", | ||
"cat", | "cat", | ||
"low", | "low", | ||
"noError", -- OBSOLETE | |||
"format", | "format", | ||
"template" }, | "template" }, | ||
template = "#invoke:TemplatePar|check|" | template = "#invoke:TemplatePar|check|" | ||
} | } | ||
local r = form( false, options | local r = form( false, options ) | ||
if not r then | if not r then | ||
options = { mandatory = fill( frame.args.all ), | options = { mandatory = fill( frame.args.all ), | ||
optional = fill( frame.args.opt ), | optional = fill( frame.args.opt ), | ||
cat = frame.args.cat, | cat = frame.args.cat, | ||
low = frame.args.low, | low = frame.args.low, | ||
noError = frame.args.noError, -- OBSOLETE | |||
format = frame.args.format, | format = frame.args.format, | ||
template = frame.args.template | template = frame.args.template | ||
} | } | ||
r = form( true, options | r = form( true, options ) | ||
end | end | ||
return r or "" | return r or "" | ||
end -- | end -- .check() | ||
Zeile 1.425: | Zeile 1.181: | ||
-- TemplatePar.count() | -- TemplatePar.count() | ||
return tostring( TemplatePar.count() ) | return tostring( TemplatePar.count() ) | ||
end -- | end -- .count() | ||
Zeile 1.436: | Zeile 1.192: | ||
-- TemplatePar.countNotEmpty() | -- TemplatePar.countNotEmpty() | ||
return tostring( TemplatePar.countNotEmpty() ) | return tostring( TemplatePar.countNotEmpty() ) | ||
end -- | end -- .countNotEmpty() | ||
Zeile 1.442: | Zeile 1.198: | ||
function p.match( frame ) | function p.match( frame ) | ||
-- Combined analysis of parameters and their values | -- Combined analysis of parameters and their values | ||
-- Postcondition: | -- Postcondition: | ||
-- Return string with error message or "" | -- Return string with error message or "" | ||
-- Uses: | -- Uses: | ||
-- mw.text.trim() | -- mw.text.trim() | ||
-- mw.ustring.lower() | -- mw.ustring.lower() | ||
Zeile 1.459: | Zeile 1.212: | ||
local r = false | local r = false | ||
local options = { cat = frame.args.cat, | local options = { cat = frame.args.cat, | ||
low = frame.args.low, | low = frame.args.low, | ||
noError = frame.args.noError, -- OBSOLETE | |||
format = frame.args.format, | format = frame.args.format, | ||
template = frame.args.template | template = frame.args.template | ||
} | } | ||
local k, v, s | local k, v, s | ||
local params = { } | local params = { } | ||
for k, v in pairs( frame.args ) do | for k, v in pairs( frame.args ) do | ||
if type( k ) == "number" then | if type( k ) == "number" then | ||
Zeile 1.499: | Zeile 1.250: | ||
end -- for k, v | end -- for k, v | ||
options.optional = s | options.optional = s | ||
r = form( true, options | r = form( true, options ) | ||
end | end | ||
if not r then | if not r then | ||
Zeile 1.532: | Zeile 1.283: | ||
if lack then | if lack then | ||
if errMiss then | if errMiss then | ||
errMiss = | errMiss = errMiss .. ", '" .. k .. "'" | ||
else | else | ||
errMiss = | errMiss = "'" .. k .. "'" | ||
end | end | ||
elseif not errMiss then | elseif not errMiss then | ||
Zeile 1.555: | Zeile 1.305: | ||
end | end | ||
return r or "" | return r or "" | ||
end -- | end -- .match() | ||
Zeile 1.568: | Zeile 1.318: | ||
-- furnish() | -- furnish() | ||
return furnish( frame, "valid" ) | return furnish( frame, "valid" ) | ||
end -- | end -- .valid() | ||
Zeile 1.603: | Zeile 1.327: | ||
-- Return table with functions | -- Return table with functions | ||
return TemplatePar | return TemplatePar | ||
end -- | end -- .TemplatePar() | ||
return p | return p |