Bearbeiten von „Modul:TemplUtl“
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: | ||
local TemplUtl = { suite = "TemplUtl", | local TemplUtl = { suite = "TemplUtl", | ||
serial = " | serial = "2017-04-24" }; | ||
Zeile 39: | Zeile 9: | ||
-- accept -- string; trimmed title | -- accept -- string; trimmed title | ||
-- Postcondition: | -- Postcondition: | ||
-- | -- Returns string with pattern | ||
local start = mw.ustring.sub( accept, 1, 1 ); | local start = mw.ustring.sub( accept, 1, 1 ); | ||
local r; | local r; | ||
Zeile 63: | Zeile 33: | ||
local framing = function ( frame ) | local framing = function ( frame ) | ||
if not TemplUtl.frame then | if not TemplUtl.frame then | ||
if | if frame then | ||
TemplUtl.frame = frame; | TemplUtl.frame = frame; | ||
else | else | ||
Zeile 69: | Zeile 39: | ||
end | end | ||
end | end | ||
end -- framing() | end -- framing() | ||
TemplUtl.faculty = function ( analyze, another ) | TemplUtl.faculty = function ( analyze, another, frame ) | ||
-- Test template arg for boolean | -- Test template arg for boolean | ||
-- analyze -- string, boolean, number or nil | -- analyze -- string, boolean, number or nil | ||
-- another -- fallback: string, boolean, or nil | -- another -- fallback: string, boolean, or nil | ||
-- frame -- object, or nil | |||
-- Returns boolean | -- Returns boolean | ||
local s = type( analyze ); | local s = type( analyze ); | ||
Zeile 84: | Zeile 54: | ||
r = mw.text.trim( analyze ); | r = mw.text.trim( analyze ); | ||
if r == "" then | if r == "" then | ||
r = TemplUtl.faculty( another, nil ); | r = TemplUtl.faculty( another, nil, frame ); | ||
elseif r | elseif r == "1" then | ||
r = true; | r = true; | ||
elseif r | elseif r == "0" then | ||
r = false; | r = false; | ||
else | else | ||
r = r:lower(); | r = r:lower(); | ||
if r == "y" or | if r == "y" or r == "yes" or r == "true" then | ||
r = true; | r = true; | ||
elseif r == "n" or | elseif r == "n" or r == "no" or r == "false" then | ||
r = false; | r = false; | ||
else | else | ||
if not TemplUtl. | 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(); | |||
TemplUtl. | |||
end | end | ||
entry = TemplUtl.L10N[ TemplUtl.lang ]; | |||
if type( entry ) == "table" then | |||
s = entry[ r ]; | |||
if type( s ) == "boolean" then | |||
r = s; | |||
end | |||
end | end | ||
else | |||
TemplUtl.L10N = true; | |||
end | end | ||
if type( r ) ~= "boolean" then | if type( r ) ~= "boolean" then | ||
if type( another ) ~= "nil" then | |||
r = TemplUtl.faculty( another ); | |||
else | |||
r = true; | r = true; | ||
end | end | ||
end | end | ||
Zeile 157: | Zeile 110: | ||
TemplUtl.failure = function ( alert, always | TemplUtl.failure = function ( alert, always ) | ||
-- Format error message, mostly hidden | -- Format error message, mostly hidden | ||
-- alert | -- alert -- string | ||
-- always | -- always -- do not hide: boolean, or nil | ||
-- Returns string | -- 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" ) | local err = mw.html.create( "span" ) | ||
:addClass( "error" ) | :addClass( "error" ) | ||
:attr( "id", sign ) | |||
:wikitext( alert ); | :wikitext( alert ); | ||
local btn = mw.html.create( "span" ); | |||
local top = mw.html.create( "div" ); | |||
-- TODO: LTR | |||
btn:css( { ["background"] = "#FFFF00", | |||
["border"] = "#FF0000 3px 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", | |||
["line-height"] = "0", | |||
["max-height"] = "0", | |||
["max-width"] = "0", | |||
["visibility"] = "hidden" } ); | |||
end | end | ||
mw.addWarning( tostring( top ) ); | |||
return tostring( err ); | return tostring( err ); | ||
end -- TemplUtl.failure() | end -- TemplUtl.failure() | ||
Zeile 260: | Zeile 151: | ||
-- address -- string; what to inspect, URL presumed | -- address -- string; what to inspect, URL presumed | ||
-- Postcondition: | -- Postcondition: | ||
-- | -- Returns true, if URL beginning | ||
local start, r = address:match( "^%s*((%a*:?)//)" ); | local start, r = address:match( "^%s*((%a*:?)//)" ); | ||
if start then | if start then | ||
Zeile 290: | Zeile 181: | ||
-- after -- true, if only to search for "}}" | -- after -- true, if only to search for "}}" | ||
-- Postcondition: | -- Postcondition: | ||
-- | -- Returns | ||
-- -- number; byte position in area | -- -- number; byte position in area | ||
-- -- before "|" or "}}", may be at end | -- -- before "|" or "}}", may be at end | ||
Zeile 427: | Zeile 318: | ||
-- at -- optional number; byte position in area of "{{" | -- at -- optional number; byte position in area of "{{" | ||
-- Postcondition: | -- Postcondition: | ||
-- | -- Returns | ||
-- -- table | -- -- table | ||
-- [0] -- template, page, parser function name | -- [0] -- template, page, parser function name | ||
Zeile 494: | Zeile 385: | ||
-- ask -- string; parameter name | -- ask -- string; parameter name | ||
-- Postcondition: | -- Postcondition: | ||
-- | -- Returns string with trimmed parameter value, or nil | ||
-- Does not return value if template inside | -- Does not return value if template inside | ||
local r; | local r; | ||
Zeile 525: | Zeile 416: | ||
-- no colon (:) | -- no colon (:) | ||
-- Postcondition: | -- Postcondition: | ||
-- | -- Returns ustring position of "{{" in area, or false | ||
-- Requires: | -- Requires: | ||
-- fiatTitleRegExp() | -- fiatTitleRegExp() | ||
Zeile 591: | Zeile 482: | ||
-- 3 4 5 6 | -- 3 4 5 6 | ||
-- more like 2 | -- more like 2 | ||
Zeile 608: | Zeile 488: | ||
-- Remove syntax elements that hide effective syntax only | -- Remove syntax elements that hide effective syntax only | ||
-- Precondition: | -- Precondition: | ||
-- area -- string; | -- area -- string; wikitext to be reduced | ||
-- Postcondition: | -- Postcondition: | ||
-- | -- Returns cleared wikitext | ||
local delimiters = { { "<%s*NOWIKI%s*>", "<%s*/%s*NOWIKI%s*>" }, | local delimiters = { { "<%s*NOWIKI%s*>", "<%s*/%s*NOWIKI%s*>" }, | ||
{ "<!--", "-->", true }, | { "<!--", "-->", true }, | ||
Zeile 674: | Zeile 554: | ||
return r; | return r; | ||
end -- TemplUtl.flat() | end -- TemplUtl.flat() | ||
Zeile 737: | Zeile 562: | ||
function p.faculty( frame ) | function p.faculty( frame ) | ||
return TemplUtl.faculty( frame.args[ 1 ], | return TemplUtl.faculty( frame.args[ 1 ], | ||
frame.args[ 2 ] ) and "1" | frame.args[ 2 ], | ||
frame ) and "1" | |||
or ""; | or ""; | ||
end -- p.faculty | end -- p.faculty() | ||
function p.failure( frame ) | function p.failure( frame ) | ||
local scream = mw.text.trim( frame.args[ 1 ] or "" ); | local scream = mw.text.trim( frame.args[ 1 ] or "" ); | ||
local loud = frame.args[ 2 ]; | local loud = frame.args[ 2 ]; | ||
if scream == "?????????" then | |||
if scream = | scream = ""; | ||
end | end | ||
if loud then | if loud then | ||
loud = TemplUtl.faculty( loud, nil | loud = TemplUtl.faculty( loud, nil, frame ); | ||
end | end | ||
return | return TemplUtl.failure( scream, loud ); | ||
end -- p. | end -- p.failure() | ||
function p. | function p.failsafe() | ||
return TemplUtl.serial; | |||
end -- p.failsafe() | |||
return | |||
end -- | |||
p.failsafe | |||
p.TemplUtl = function () | p.TemplUtl = function () |