Modul:TemplUtl: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
2017-04-23
(2016-02-28) |
(2017-04-23) |
||
Zeile 1: | Zeile 1: | ||
local TemplUtl = { suite = "TemplUtl", | local TemplUtl = { suite = "TemplUtl", | ||
serial = " | serial = "2017-04-23" }; | ||
Zeile 23: | Zeile 22: | ||
if r:match( " " ) then | if r:match( " " ) then | ||
r = r:gsub( "%", "%%" ) | r = r:gsub( "%", "%%" ) | ||
:gsub( "[^.?+*()$] | :gsub( "[%-^.?+*()$]", "%$1" ) | ||
:gsub( "_", " " ) | :gsub( "_", " " ) | ||
:gsub( "%s+", "[%s_]+" ); | :gsub( "%s+", "[%s_]+" ); | ||
Zeile 29: | Zeile 28: | ||
return r; | return r; | ||
end -- fiatTitleRegExp() | end -- fiatTitleRegExp() | ||
local framing = function ( frame ) | |||
if not TemplUtl.frame then | |||
if frame then | |||
TemplUtl.frame = frame; | |||
else | |||
TemplUtl.frame = mw.getCurrentFrame(); | |||
end | |||
end | |||
end -- framing() | |||
TemplUtl.faculty = function ( analyze, another, frame ) | |||
-- Test template arg for boolean | |||
-- analyze -- string, boolean, number or nil | |||
-- another -- fallback: string, boolean, or nil | |||
-- frame -- object, or nil | |||
-- Returns boolean | |||
local s = type( analyze ); | |||
local r; | |||
if s == "string" then | |||
r = mw.text.trim( analyze ); | |||
if r == "" then | |||
r = TemplUtl.faculty( another, nil, frame ); | |||
elseif r == "1" then | |||
r = true; | |||
elseif r == "0" then | |||
r = false; | |||
else | |||
r = r:lower(); | |||
if r == "y" or r == "yes" or r == "true" then | |||
r = true; | |||
elseif r == "n" or r == "no" or r == "false" then | |||
r = false; | |||
else | |||
if not TemplUtl.L10N then | |||
local lucky; | |||
s = string.format( "Module:%s/local", | |||
TemplUtl.suite ); | |||
lucky, TemplUtl.L10N = pcall( mw.loadData, s ); | |||
end | |||
if type( TemplUtl.L10N ) == "table" then | |||
local entry; | |||
if not TemplUtl.lang then | |||
-- TODO: page language | |||
TemplUtl.lang = | |||
mw.language.getContentLanguage():getCode(); | |||
end | |||
entry = TemplUtl.L10N[ TemplUtl.lang ]; | |||
if type( entry ) == "table" then | |||
s = entry[ r ]; | |||
if type( s ) == "boolean" then | |||
r = s; | |||
end | |||
end | |||
else | |||
TemplUtl.L10N = true; | |||
end | |||
if type( r ) ~= "boolean" then | |||
if type( another ) ~= "nil" then | |||
r = TemplUtl.faculty( another ); | |||
else | |||
r = true; | |||
end | |||
end | |||
end | |||
end | |||
elseif s == "boolean" then | |||
r = analyze; | |||
elseif s == "number" then | |||
r = ( analyze ~= 0 ); | |||
else | |||
r = false; | |||
end | |||
return r; | |||
end -- TemplUtl.faculty() | |||
TemplUtl.failure = function ( alert, always ) | |||
-- Format error message, mostly hidden | |||
-- alert -- string | |||
-- always -- do not hide: boolean, or nil | |||
-- Returns string | |||
local max = 1000000000; | |||
local id = math.floor( os.clock() * max ); | |||
local sign = string.format( "error_%d", id ); | |||
local err = mw.html.create( "span" ) | |||
:addClass( "error" ) | |||
:attr( "id", sign ) | |||
:wikitext( alert ); | |||
local btn = mw.html.create( "span" ); | |||
local top = mw.html.create( "div" ); | |||
-- TODO: LTR | |||
btn:css( { ["border"] = "#808080 1px solid", | |||
["font-weight"] = "bold", | |||
["padding"] = "2px", | |||
["text-decoration"] = "none" } ) | |||
:wikitext( ">>>" ) ; | |||
sign = string.format( "[[#%s|%s]]", sign, tostring( btn ) ); | |||
top:wikitext( sign, " ", alert ); | |||
if not always then | |||
err:css( { ["display"] = "inline-block", | |||
["max-width"] = "0", | |||
["visibility"] = "hidden" } ); | |||
end | |||
mw.addWarning( tostring( top ) ); | |||
return tostring( err ); | |||
end -- TemplUtl.failure() | |||
Zeile 385: | Zeile 496: | ||
local r = area; | local r = area; | ||
local k, m, n; | local k, m, n; | ||
if not Delimiters then | if not TemplUtl.Delimiters then | ||
local c, sD, sP; | local c, sD, sP; | ||
Delimiters = { }; | TemplUtl.Delimiters = { }; | ||
for j = 1, #delimiters do | for j = 1, #delimiters do | ||
table.insert( Delimiters, { } ); | table.insert( TemplUtl.Delimiters, { } ); | ||
for ji = 1, 2 do | for ji = 1, 2 do | ||
sD = delimiters[ j ][ ji ]; | sD = delimiters[ j ][ ji ]; | ||
Zeile 402: | Zeile 513: | ||
end | end | ||
end -- for js | end -- for js | ||
table.insert( Delimiters[ j ], sP ); | table.insert( TemplUtl.Delimiters[ j ], sP ); | ||
end -- for ji | end -- for ji | ||
end -- for j | end -- for j | ||
Zeile 409: | Zeile 520: | ||
k = false; | k = false; | ||
for j = 1, #delimiters do | for j = 1, #delimiters do | ||
m = r:find( Delimiters[ j ][ 1 ], | m = r:find( TemplUtl.Delimiters[ j ][ 1 ], | ||
i, | i, | ||
Delimiters[ j ][ 3 ] ); | TemplUtl.Delimiters[ j ][ 3 ] ); | ||
if m and ( not k or m < k ) then | if m and ( not k or m < k ) then | ||
k = m; | k = m; | ||
Zeile 425: | Zeile 536: | ||
s = ""; | s = ""; | ||
end | end | ||
j, m = r:find( Delimiters[ n ][ 2 ], | j, m = r:find( TemplUtl.Delimiters[ n ][ 2 ], | ||
k + 1, | k + 1, | ||
Delimiters[ n ][ 3 ] ); | TemplUtl.Delimiters[ n ][ 3 ] ); | ||
if m then | if m then | ||
r = s .. r:sub( m + 1 ); | r = s .. r:sub( m + 1 ); | ||
Zeile 445: | Zeile 556: | ||
-- Export | -- Export | ||
local p = { }; | local p = { }; | ||
function p.faculty( frame ) | |||
return TemplUtl.faculty( frame.args[ 1 ], | |||
frame.args[ 2 ], | |||
frame ) and "1" | |||
or ""; | |||
end -- p.faculty() | |||
function p.failure( frame ) | |||
local scream = mw.text.trim( frame.args[ 1 ] or "" ); | |||
local loud = frame.args[ 2 ]; | |||
if scream == "?????????" then | |||
scream = ""; | |||
end | |||
if loud then | |||
loud = TemplUtl.faculty( loud, nil, frame ); | |||
end | |||
return TemplUtl.failure( scream, loud ); | |||
end -- p.failure() | |||
function p.failsafe() | function p.failsafe() | ||
return TemplUtl.serial | return TemplUtl.serial; | ||
end | end -- p.failsafe() | ||
p.TemplUtl = function () | p.TemplUtl = function () | ||
return TemplUtl; | return TemplUtl; | ||
end | end -- p.TemplUtl() | ||
return p; | return p; |