Modul:TemplatePar: Unterschied zwischen den Versionen
gem. Spezial:Diff/137026344/137027480
K (1 Version) |
wp>Mabschaaf |
||
Zeile 1: | Zeile 1: | ||
--[=[ TemplatePar | --[=[ TemplatePar 2014-12-22 | ||
Template parameter utility | Template parameter utility | ||
* assert | * assert | ||
Zeile 16: | Zeile 16: | ||
-- Module globals | -- Module globals | ||
local TemplatePar = { } | local TemplatePar = { } | ||
local | local MessagePrefix = "lua-module-TemplatePar-" | ||
local | local L10nDef = {} | ||
L10nDef.en = { | |||
badPattern = "#invoke:TemplatePar pattern syntax error", | badPattern = "#invoke:TemplatePar pattern syntax error", | ||
dupOpt = "#invoke:TemplatePar repeated optional parameter", | dupOpt = "#invoke:TemplatePar repeated optional parameter", | ||
Zeile 26: | Zeile 26: | ||
invalidPar = "#invoke:TemplatePar invalid parameter", | invalidPar = "#invoke:TemplatePar invalid parameter", | ||
minmax = "#invoke:TemplatePar min > max", | minmax = "#invoke:TemplatePar min > max", | ||
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", | 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", | |||
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", | ||
Zeile 35: | Zeile 37: | ||
unknownRule = "#invoke:TemplatePar unknown rule" | unknownRule = "#invoke:TemplatePar unknown rule" | ||
} | } | ||
L10nDef.de = { | |||
badPattern = "#invoke:TemplatePar Syntaxfehler des pattern", | badPattern = "#invoke:TemplatePar Syntaxfehler des pattern", | ||
dupOpt = "#invoke:TemplatePar Optionsparameter wiederholt", | dupOpt = "#invoke:TemplatePar Optionsparameter wiederholt", | ||
Zeile 46: | Zeile 48: | ||
noErrorCat = "#invoke:TemplatePar noError und keine Kategorie", | noErrorCat = "#invoke:TemplatePar noError und keine Kategorie", | ||
noname = "#invoke:TemplatePar Parameter nicht angegeben", | noname = "#invoke:TemplatePar Parameter nicht angegeben", | ||
notFound = "Fehler bei Vorlage * Seite fehlt", | |||
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", | ||
Zeile 80: | Zeile 83: | ||
[ "abc+" ] = "^[a-z]+$", | [ "abc+" ] = "^[a-z]+$", | ||
[ "aBc+" ] = "^[a-z]+[A-Z][A-Za-z]*$", | [ "aBc+" ] = "^[a-z]+[A-Z][A-Za-z]*$", | ||
[ "w" ] = "^%S*$", | |||
[ "w+" ] = "^%S+$", | |||
[ "base64" ] = "^[A-Za-z0-9%+/]*$", | [ "base64" ] = "^[A-Za-z0-9%+/]*$", | ||
[ "base64+" ] = "^[A-Za-z0-9%+/]+$", | [ "base64+" ] = "^[A-Za-z0-9%+/]+$", | ||
Zeile 113: | Zeile 118: | ||
return r | return r | ||
end -- containsCJK() | end -- containsCJK() | ||
local function facility( accept, attempt ) | |||
-- Check string as possible file name or other source page | |||
-- Precondition: | |||
-- accept -- string; requirement | |||
-- file | |||
-- file+ | |||
-- file: | |||
-- file:+ | |||
-- image | |||
-- image+ | |||
-- image: | |||
-- image:+ | |||
-- attempt -- string; to be tested | |||
-- Postcondition: | |||
-- Return error keyword, or false | |||
-- Uses: | |||
-- Module:FileMedia | |||
-- FileMedia.isType() | |||
local r | |||
if attempt and attempt ~= "" then | |||
local lucky, FileMedia = pcall( require, "Module:FileMedia" ) | |||
if type( FileMedia ) == "table" then | |||
FileMedia = FileMedia.FileMedia() | |||
local s, live = accept:match( "^([a-z]+)(:?)%+?$" ) | |||
if live then | |||
if FileMedia.isType( attempt, s ) then | |||
if FileMedia.isFile( attempt ) then | |||
r = false | |||
else | |||
r = "notFound" | |||
end | |||
else | |||
r = "invalid" | |||
end | |||
elseif FileMedia.isType( attempt, s ) then | |||
r = false | |||
else | |||
r = "invalid" | |||
end | |||
else | |||
r = "missing" | |||
end | |||
elseif accept:match( "%+$" ) then | |||
r = "empty" | |||
else | |||
r = false | |||
end | |||
return r | |||
end -- facility() | |||
Zeile 123: | Zeile 180: | ||
-- Return some message string | -- Return some message string | ||
-- Uses: | -- Uses: | ||
-- > | -- > MessagePrefix | ||
-- > | -- > L10nDef | ||
-- mw.language.getContentLanguage() | -- mw.language.getContentLanguage() | ||
-- mw.message.new() | -- mw.message.new() | ||
local c = mw.language.getContentLanguage():getCode() | local c = mw.language.getContentLanguage():getCode() | ||
local m = mw.message.new( | local m = mw.message.new( MessagePrefix .. say ) | ||
local r = false | local r = false | ||
if m:isBlank() then | if m:isBlank() then | ||
local l10n = | local l10n = L10nDef[ c ] | ||
if not l10n then | if not l10n then | ||
l10n = | l10n = L10nDef[ "en" ] | ||
end | end | ||
r = l10n[ say ] | r = l10n[ say ] | ||
Zeile 229: | Zeile 286: | ||
-- failure() | -- failure() | ||
-- mw.text.trim() | -- mw.text.trim() | ||
-- facility() | |||
-- failsafe() | -- failsafe() | ||
-- containsCJK() | -- containsCJK() | ||
Zeile 293: | Zeile 351: | ||
r = "undefined" | r = "undefined" | ||
end | end | ||
elseif s:match( "^image%+?:?$" ) or | |||
s:match( "^file%+?:?$" ) then | |||
r = facility( s, analyze ) | |||
n = true | |||
elseif s:match( "langW?%+?" ) then | |||
n = "lang" | |||
-- lang lang+ | |||
-- langW langW+ | |||
end | end | ||
if not n and not r then | if not n and not r then | ||
Zeile 464: | 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 | -- options.noError -- OBSOLETE | ||
-- options.format | |||
-- options.cat | -- options.cat | ||
-- options.template | -- options.template | ||
Zeile 472: | 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 = options | opt = options | ||
if opt.noError then -- OBSOLETE | |||
lazy = true | |||
lapsus = true | |||
end | |||
show = opt.format | |||
lazy = lazy or | |||
( show == "" or show == "0" or show == "-" ) | |||
else | else | ||
opt = { } | opt = { } | ||
end | end | ||
if | if lazy then | ||
if not opt.cat then | if not opt.cat then | ||
r = submit .. " " .. factory( "noErrorCat" ) | r = submit .. " " .. factory( "noErrorCat" ) -- OBSOLETE | ||
end | end | ||
else | else | ||
r = submit | r = submit | ||
end | end | ||
if r then | if r and not lazy then | ||
if not show or show == "*" then | |||
show = "<span class=\"error\">@@@</span>" | |||
end | |||
r = mw.ustring.gsub( show, "@@@", r ) | |||
end | end | ||
s = opt.cat | s = opt.cat | ||
Zeile 499: | Zeile 580: | ||
end | end | ||
end | end | ||
r = | local i | ||
local cats = mw.text.split( s, "%s*#%s*" ) | |||
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 -- 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 809: | Zeile 906: | ||
"max", | "max", | ||
"min", | "min", | ||
"noError", | "noError", -- OBSOLETE | ||
"format", | |||
"template" }, | "template" }, | ||
template = "#invoke:TemplatePar|".. action .. "|" | template = "#invoke:TemplatePar|".. action .. "|" | ||
Zeile 818: | Zeile 916: | ||
options = { cat = frame.args.cat, | options = { cat = frame.args.cat, | ||
low = frame.args.low, | low = frame.args.low, | ||
noError = frame.args.noError, | noError = frame.args.noError, -- OBSOLETE | ||
format = frame.args.format, | |||
template = frame.args.template | template = frame.args.template | ||
} | } | ||
Zeile 1.053: | Zeile 1.152: | ||
"cat", | "cat", | ||
"low", | "low", | ||
"noError", | "noError", -- OBSOLETE | ||
"format", | |||
"template" }, | "template" }, | ||
template = "#invoke:TemplatePar|check|" | template = "#invoke:TemplatePar|check|" | ||
Zeile 1.063: | Zeile 1.163: | ||
cat = frame.args.cat, | cat = frame.args.cat, | ||
low = frame.args.low, | low = frame.args.low, | ||
noError = frame.args.noError, | noError = frame.args.noError, -- OBSOLETE | ||
format = frame.args.format, | |||
template = frame.args.template | template = frame.args.template | ||
} | } | ||
Zeile 1.110: | Zeile 1.211: | ||
-- finalize() | -- finalize() | ||
local r = false | local r = false | ||
local options = { cat | local options = { cat = frame.args.cat, | ||
low | low = frame.args.low, | ||
noError = frame.args. | noError = frame.args.noError, -- OBSOLETE | ||
template | format = frame.args.format, | ||
template = frame.args.template | |||
} | } | ||
local k, v, s | local k, v, s |