Modul:TemplatePar: Unterschied zwischen den Versionen
update
wp>PerfektesChaos (Migration zur Umparametrisierung abgeschlossen; keine Einbindungen mehr) |
wp>PerfektesChaos (update) |
||
Zeile 1: | Zeile 1: | ||
--[=[ TemplatePar 2013-06- | --[=[ TemplatePar 2013-06-10 | ||
Template parameter utility | Template parameter utility | ||
* assert | |||
* check | * check | ||
* count | * count | ||
Zeile 108: | Zeile 109: | ||
end | end | ||
return r | return r | ||
end -- | end -- containsCJK() | ||
Zeile 545: | Zeile 546: | ||
local function format( | local function format( analyze, options ) | ||
-- Check validity of | -- Check validity of a value | ||
-- Precondition: | -- Precondition: | ||
-- | -- analyze -- string to be analyzed | ||
-- options -- table or nil; optional details | -- options -- table or nil; optional details | ||
-- options.pattern | -- options.pattern | ||
-- options.key | -- options.key | ||
-- options. | -- options.say | ||
-- options.min | -- options.min | ||
-- options.max | -- options.max | ||
Zeile 560: | Zeile 561: | ||
-- Uses: | -- Uses: | ||
-- > Patterns | -- > Patterns | ||
-- failure() | -- failure() | ||
-- mw.text.trim() | -- mw.text.trim() | ||
-- failsafe() | -- failsafe() | ||
-- containsCJK() | -- containsCJK() | ||
local r = false | local r = false | ||
local s | local s = false | ||
local | local show = nil | ||
local | local scan = false | ||
if type( options ) | if type( options.pattern ) == "string" then | ||
if options.key then | |||
r = failure( "dupRule", false, options ) | |||
else | |||
scan = options.pattern | |||
end | |||
else | else | ||
if type( options.key ) == "string" then | |||
s = mw.text.trim( options.key ) | |||
if type( options. | |||
else | else | ||
s = "+" | |||
end | |||
scan = Patterns[ s ] | |||
if type( scan ) == "string" then | |||
if s == "n" or s == "0,0" or s == "0.0" then | |||
if not analyze:match( "[0-9]" ) then | |||
scan = false | |||
if options.say then | |||
show = "'" .. options.say .. "'" | |||
end | end | ||
r = failure( "invalid", show, options ) | |||
end | end | ||
end | end | ||
else | |||
r = failure( "unknownRule", s, options ) | |||
end | end | ||
end | end | ||
if scan then | if scan then | ||
local legal, got = pcall( failsafe, | local legal, got = pcall( failsafe, analyze, scan ) | ||
if legal then | if legal then | ||
if not got then | if not got then | ||
if s == "aa" then | if s == "aa" then | ||
got = containsCJK( | got = containsCJK( analyze ) | ||
end | end | ||
if not got then | if not got then | ||
if options.say then | |||
r | show = "'" .. options.say .. "'" | ||
end | |||
r = failure( "invalid", show, options ) | |||
end | end | ||
end | end | ||
Zeile 639: | Zeile 627: | ||
end | end | ||
end | end | ||
if # | if #analyze < options.min and not r then | ||
show = " <" .. options.min | |||
if options.say then | |||
show = show .. " '" .. options.say .. "'" | |||
end | |||
r = failure( "tooShort", show, options ) | |||
end | end | ||
else | else | ||
Zeile 650: | Zeile 640: | ||
if options.max and not r then | if options.max and not r then | ||
if type( options.max ) == "number" then | if type( options.max ) == "number" then | ||
if # | if #analyze > options.max then | ||
show = " >" .. options.max | |||
if options.say then | |||
show = show .. " '" .. options.say .. "'" | |||
end | |||
r = failure( "tooLong", show, options ) | |||
end | end | ||
else | else | ||
Zeile 661: | Zeile 653: | ||
return r | return r | ||
end -- format() | end -- format() | ||
local function formatted( assignment, access, options ) | |||
-- Check validity of one particular parameter in a collection | |||
-- Precondition: | |||
-- assignment -- collection | |||
-- access -- id of parameter in collection | |||
-- options -- table or nil; optional details | |||
-- Postcondition: | |||
-- Return string with error message as configured; | |||
-- false if valid or no answer permitted | |||
-- Uses: | |||
-- > Patterns | |||
-- failure() | |||
-- mw.text.trim() | |||
-- format() | |||
-- failure() | |||
local r = false | |||
if type( assignment ) == "table" then | |||
local story = assignment.args[ access ] | |||
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 | |||
return r | |||
end -- formatted() | |||
TemplatePar.assert = function ( analyze, append, options ) | |||
-- Perform parameter analysis on a single string | |||
-- Precondition: | |||
-- analyze -- string to be analyzed | |||
-- append -- string: append error message, prepending <br /> | |||
-- false or nil: throw error with message | |||
-- options -- table; optional details | |||
-- Postcondition: | |||
-- Return string with error message as configured; | |||
-- false if valid | |||
-- Uses: | |||
-- format() | |||
local r = format( analyze, options ) | |||
if ( r ) then | |||
if ( type( append ) == "string" ) then | |||
if ( append ~= "" ) then | |||
r = append .. "<br />" .. r | |||
end | |||
else | |||
error( r, 0 ) | |||
end | |||
end | |||
return r | |||
end -- TemplatePar.assert() | |||
Zeile 738: | Zeile 790: | ||
TemplatePar.valid = function ( | TemplatePar.valid = function ( access, options ) | ||
-- Check validity of one particular template parameter | -- Check validity of one particular template parameter | ||
-- Precondition: | -- Precondition: | ||
-- | -- access -- id of parameter in template transclusion | ||
-- options -- table or nil; optional details | -- options -- table or nil; optional details | ||
-- Postcondition: | -- Postcondition: | ||
Zeile 748: | Zeile 800: | ||
-- Uses: | -- Uses: | ||
-- mw.text.trim() | -- mw.text.trim() | ||
-- | -- TemplatePar.downcase() | ||
-- mw.getCurrentFrame() | |||
-- frame:getParent() | |||
-- formatted() | |||
-- failure() | -- failure() | ||
-- finalize() | -- finalize() | ||
local r | local r | ||
if type( | if type( access ) == "string" then | ||
r = mw.text.trim( | r = mw.text.trim( access ) | ||
if #r == 0 then | if #r == 0 then | ||
r = false | r = false | ||
Zeile 759: | Zeile 814: | ||
end | end | ||
if r then | if r then | ||
r = | local params | ||
if type( options ) ~= "table" then | |||
options = { } | |||
end | |||
if options.low then | |||
params = TemplatePar.downcase( options ) | |||
else | |||
params = mw.getCurrentFrame():getParent() | |||
end | |||
r = formatted( params, access, options ) | |||
else | else | ||
r = failure( "noname", false, options ) | r = failure( "noname", false, options ) | ||
Zeile 905: | Zeile 969: | ||
else | else | ||
s = frame.args[ 1 ] or "" | s = frame.args[ 1 ] or "" | ||
r = tonumber( s ) | |||
if ( r ) then | |||
s = r | |||
end | |||
r = TemplatePar.valid( s, options ) | r = TemplatePar.valid( s, options ) | ||
end | end |