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 2015-02-14 | |||
-- | |||
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 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", | |||
noMSGnoCAT = "#invoke:TemplatePar weder Meldung noch 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 215: | Zeile 202: | ||
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 242: | Zeile 243: | ||
return r | return r | ||
end -- failure() | end -- failure() | ||
Zeile 406: | Zeile 287: | ||
-- mw.text.trim() | -- mw.text.trim() | ||
-- facility() | -- facility() | ||
-- | -- failsafe() | ||
-- containsCJK() | -- containsCJK() | ||
local r = false | local r = false | ||
Zeile 475: | Zeile 356: | ||
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 503: | Zeile 379: | ||
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 538: | Zeile 414: | ||
-- 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 442: | ||
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 486: | ||
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 652: | Zeile 526: | ||
local function finalize( submit, options ) | local function finalize( submit, options, frame ) | ||
-- Finalize message | -- Finalize message | ||
-- Precondition: | -- Precondition: | ||
Zeile 661: | Zeile 535: | ||
-- options.cat | -- options.cat | ||
-- options.template | -- options.template | ||
-- frame -- object, or false | |||
-- Postcondition: | -- Postcondition: | ||
-- Return string or false | -- Return string or false | ||
Zeile 667: | Zeile 542: | ||
local r = false | local r = false | ||
if submit then | if submit then | ||
local opt, s | local opt, s | ||
local lazy = false | |||
local show = false | |||
if type( options ) == "table" then | if type( options ) == "table" then | ||
opt = options | opt = options | ||
Zeile 677: | Zeile 551: | ||
s = opt.preview | s = opt.preview | ||
if type( s ) == "string" and | if type( s ) == "string" and | ||
s ~= "" and s ~= "0" and s ~= "-" then | |||
if lazy then | if lazy then | ||
show = "" | show = "" | ||
lazy = false | lazy = false | ||
end | end | ||
if not | if not frame then | ||
frame = mw.getCurrentFrame() | |||
end | end | ||
if | if frame:preprocess( "{{REVISIONID}}" ) == "" then | ||
if s == "1" then | if s == "1" then | ||
show = "*" | show = "*" | ||
Zeile 692: | Zeile 565: | ||
show = s | show = s | ||
end | end | ||
end | end | ||
end | end | ||
Zeile 709: | Zeile 581: | ||
local i | local i | ||
if not show or show == "*" then | if not show or show == "*" then | ||
show = "<span class=\"error\">@@@</span>" | |||
end | end | ||
i = show:find( "@@@", 1, true ) | i = show:find( "@@@", 1, true ) | ||
Zeile 742: | Zeile 593: | ||
r = show | r = show | ||
end | end | ||
end | end | ||
s = opt.cat | s = opt.cat | ||
if type( s ) == "string" then | if type( s ) == "string" then | ||
if opt.errNS then | if opt.errNS then | ||
local ns = mw.title.getCurrentTitle().namespace | local ns = mw.title.getCurrentTitle().namespace | ||
Zeile 756: | Zeile 603: | ||
local spaces = string.format( " %s ", opt.errNS ) | local spaces = string.format( " %s ", opt.errNS ) | ||
if spaces:match( space ) then | if spaces:match( space ) then | ||
opt.errNS = false | |||
end | end | ||
elseif st == "table" then | elseif st == "table" then | ||
for i = 1, #opt.errNS do | for i = 1, #opt.errNS do | ||
if opt.errNS[ i ] == ns then | if opt.errNS[ i ] == ns then | ||
opt.errNS = false | |||
break -- for i | break -- for i | ||
end | end | ||
end -- for i | end -- for i | ||
end | end | ||
end | |||
if opt.errNS then | |||
r = "" | |||
else | else | ||
if not r then | if not r then | ||
r = "" | r = "" | ||
Zeile 779: | Zeile 625: | ||
end | end | ||
end | end | ||
cats = mw.text.split( s, "%s*#%s*" ) | local i | ||
local cats = mw.text.split( s, "%s*#%s*" ) | |||
for i = 1, #cats do | for i = 1, #cats do | ||
s = mw.text.trim( cats[ i ] ) | s = mw.text.trim( cats[ i ] ) | ||
Zeile 957: | Zeile 804: | ||
-- options.mandatory | -- options.mandatory | ||
-- options.optional | -- options.optional | ||
-- frame -- object | -- frame -- object, or false | ||
-- 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 814: | ||
-- 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 995: | Zeile 836: | ||
end | end | ||
end | end | ||
return finalize( r, options ) | return finalize( r, options, frame ) | ||
end -- form() | end -- form() | ||
Zeile 1.066: | Zeile 907: | ||
-- false if valid or no answer permitted | -- false if valid or no answer permitted | ||
-- Uses: | -- Uses: | ||
-- mw.text.trim() | -- mw.text.trim() | ||
-- format() | -- format() | ||
-- failure() | -- failure() | ||
Zeile 1.073: | Zeile 914: | ||
local story = assignment.args[ access ] or "" | local story = assignment.args[ access ] or "" | ||
if type( access ) == "number" then | if type( access ) == "number" then | ||
story = mw.text.trim( story ) | story = mw.text.trim( story ) | ||
end | end | ||
if type( options ) ~= "table" then | if type( options ) ~= "table" then | ||
Zeile 1.145: | Zeile 986: | ||
end | end | ||
if r then | if r then | ||
r = finalize( r, options ) | r = finalize( r, options, frame ) | ||
else | else | ||
s = frame.args[ 1 ] or "" | s = frame.args[ 1 ] or "" | ||
Zeile 1.153: | Zeile 994: | ||
end | end | ||
if action == "valid" then | if action == "valid" then | ||
r = TemplatePar.valid( s, options ) | r = TemplatePar.valid( s, options, frame ) | ||
elseif action == "assert" then | elseif action == "assert" then | ||
r = TemplatePar.assert( s, "", options ) | r = TemplatePar.assert( s, "", options ) | ||
Zeile 1.263: | Zeile 1.104: | ||
TemplatePar.valid = function ( access, options, frame ) | |||
TemplatePar.valid = function ( access, options ) | |||
-- Check validity of one particular template parameter | -- Check validity of one particular template parameter | ||
-- Precondition: | -- Precondition: | ||
Zeile 1.305: | Zeile 1.110: | ||
-- string or number | -- string or number | ||
-- options -- table or nil; optional details | -- options -- table or nil; optional details | ||
-- frame -- object; #invoke environment | |||
-- Postcondition: | -- Postcondition: | ||
-- Return string with error message as configured; | -- Return string with error message as configured; | ||
-- 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() | ||
Zeile 1.335: | Zeile 1.140: | ||
params = TemplatePar.downcase( options ) | params = TemplatePar.downcase( options ) | ||
else | else | ||
params = frame:getParent() | |||
params = | |||
end | end | ||
r = formatted( params, access, options ) | r = formatted( params, access, options ) | ||
Zeile 1.377: | Zeile 1.179: | ||
-- furnish() | -- furnish() | ||
return furnish( frame, "assert" ) | return furnish( frame, "assert" ) | ||
end -- | end -- .assert() | ||
Zeile 1.414: | Zeile 1.216: | ||
end | end | ||
return r or "" | return r or "" | ||
end -- | end -- .check() | ||
Zeile 1.425: | Zeile 1.227: | ||
-- TemplatePar.count() | -- TemplatePar.count() | ||
return tostring( TemplatePar.count() ) | return tostring( TemplatePar.count() ) | ||
end -- | end -- .count() | ||
Zeile 1.436: | Zeile 1.238: | ||
-- TemplatePar.countNotEmpty() | -- TemplatePar.countNotEmpty() | ||
return tostring( TemplatePar.countNotEmpty() ) | return tostring( TemplatePar.countNotEmpty() ) | ||
end -- | end -- .countNotEmpty() | ||
Zeile 1.442: | Zeile 1.244: | ||
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.467: | Zeile 1.266: | ||
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.551: | Zeile 1.349: | ||
r = failure( "invalid", errValues, options ) | r = failure( "invalid", errValues, options ) | ||
end | end | ||
r = finalize( r, options ) | r = finalize( r, options, frame ) | ||
end | end | ||
end | end | ||
return r or "" | return r or "" | ||
end -- | end -- .match() | ||
Zeile 1.568: | Zeile 1.366: | ||
-- furnish() | -- furnish() | ||
return furnish( frame, "valid" ) | return furnish( frame, "valid" ) | ||
end -- | end -- .valid() | ||
Zeile 1.603: | Zeile 1.375: | ||
-- Return table with functions | -- Return table with functions | ||
return TemplatePar | return TemplatePar | ||
end -- | end -- .TemplatePar() | ||
return p | return p |