Modul:TemplUtl: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
2019-11-12
K (Änderte den Schutzstatus für „Modul:TemplUtl“ ([Bearbeiten=Nur Sichter] (unbeschränkt) [Verschieben=Nur Administratoren] (unbeschränkt))) |
(2019-11-12) |
||
Zeile 1: | Zeile 1: | ||
local TemplUtl = { suite = "TemplUtl", | local TemplUtl = { suite = "TemplUtl", | ||
serial = "2019- | serial = "2019-11-12", | ||
item = 52364930 }; | item = 52364930 }; | ||
local Failsafe = TemplUtl; | |||
local fallible = function ( adjust, ahead ) | |||
-- Check for leading character disturbing syntax | |||
-- Precondition: | |||
-- adjust -- string; trimmed wikitext | |||
-- ahead -- true, if leading syntax shall start on new line | |||
-- Postcondition: | |||
-- Returns string, modified if necessary | |||
local r = adjust; | |||
local c = r:byte( 1, 1 ); | |||
if c <= 59 and | |||
( c==35 or c==42 or c==58 or c==59 ) then | |||
if ahead then | |||
r = "\n" .. r; | |||
else | |||
r = mw.text.nowiki( r:sub( 1, 1 ) ) .. r:sub( 2 ); | |||
end | |||
elseif ahead then | |||
local c2 = r:byte( 2, 1 ); | |||
if ( c==123 and c2==124 ) or | |||
( c==124 and c2==45 ) then | |||
r = "\n" .. r; | |||
end | |||
end | |||
return r; | |||
end -- fallible() | |||
Zeile 10: | Zeile 39: | ||
-- 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 92: | ||
else | else | ||
r = r:lower(); | r = r:lower(); | ||
if r == "y" or | if r == "y" or | ||
r == "yes" or | |||
r == "true" or | |||
r == "on" then | |||
r = true; | r = true; | ||
elseif r == "n" or | elseif r == "n" or | ||
r == "no" or | |||
r == "false" or | |||
r == "off" then | |||
r = false; | r = false; | ||
else | else | ||
Zeile 109: | Zeile 144: | ||
return r; | return r; | ||
end -- TemplUtl.faculty() | end -- TemplUtl.faculty() | ||
Zeile 220: | Zeile 218: | ||
-- answer -- true, or nil, for list creation | -- answer -- true, or nil, for list creation | ||
-- Postcondition: | -- Postcondition: | ||
-- | -- Returns string, if answer requested | ||
local e = framing( frame ); | local e = framing( frame ); | ||
local f = function ( a ) | local f = function ( a ) | ||
Zeile 252: | Zeile 250: | ||
-- 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 282: | Zeile 280: | ||
-- 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 419: | Zeile 417: | ||
-- 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 486: | Zeile 484: | ||
-- 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 517: | Zeile 515: | ||
-- no colon (:) | -- no colon (:) | ||
-- Postcondition: | -- Postcondition: | ||
-- | -- Returns ustring position of "{{" in area, or false | ||
-- Requires: | -- Requires: | ||
-- fiatTitleRegExp() | -- fiatTitleRegExp() | ||
Zeile 583: | Zeile 581: | ||
-- 3 4 5 6 | -- 3 4 5 6 | ||
-- more like 2 | -- more like 2 | ||
TemplUtl.firstbreak = function ( adjust ) | |||
-- Precede leading character with newline if specific syntax | |||
-- Precondition: | |||
-- adjust -- string; trimmed wikitext | |||
-- Postcondition: | |||
-- Returns string, modified if necessary | |||
return fallible( adjust, true ); | |||
end -- TemplUtl.firstbreak() | |||
Zeile 589: | Zeile 598: | ||
-- Remove syntax elements that hide effective syntax only | -- Remove syntax elements that hide effective syntax only | ||
-- Precondition: | -- Precondition: | ||
-- area -- string; wikitext to be reduced | -- area -- string; unparsed 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 655: | Zeile 664: | ||
return r; | return r; | ||
end -- TemplUtl.flat() | end -- TemplUtl.flat() | ||
TemplUtl.nowiki1 = function ( adjust ) | |||
-- HTML-escape leading character if disturbing syntax | |||
-- Precondition: | |||
-- adjust -- string; trimmed wikitext | |||
-- Postcondition: | |||
-- Returns string, modified if necessary | |||
return fallible( adjust, false ); | |||
end -- TemplUtl.nowiki1() | |||
Failsafe.failsafe = function ( atleast ) | |||
-- Retrieve versioning and check for compliance | |||
-- Precondition: | |||
-- atleast -- string, with required version or "wikidata" or "~" | |||
-- or false | |||
-- Postcondition: | |||
-- Returns string -- with queried version, also if problem | |||
-- false -- if appropriate | |||
local last = ( atleast == "~" ) | |||
local since = atleast | |||
local r | |||
if last or since == "wikidata" then | |||
local item = Failsafe.item | |||
since = false | |||
if type( item ) == "number" and item > 0 then | |||
local entity = mw.wikibase.getEntity( string.format( "Q%d", | |||
item ) ) | |||
if type( entity ) == "table" then | |||
local vsn = entity:formatPropertyValues( "P348" ) | |||
if type( vsn ) == "table" and | |||
type( vsn.value ) == "string" and | |||
vsn.value ~= "" then | |||
if last and vsn.value == Failsafe.serial then | |||
r = false | |||
else | |||
r = vsn.value | |||
end | |||
end | |||
end | |||
end | |||
end | |||
if type( r ) == "nil" then | |||
if not since or since <= Failsafe.serial then | |||
r = Failsafe.serial | |||
else | |||
r = false | |||
end | |||
end | |||
return r | |||
end -- Failsafe.failsafe() | |||
Zeile 693: | Zeile 756: | ||
return r or ""; | return r or ""; | ||
end -- p.fakes | end -- p.fakes | ||
function p.firstbreak( frame ) | |||
local r = ( frame.args[ 1 ] ); | |||
if r then | |||
r = mw.text.trim( r ); | |||
if r ~= "" then | |||
r = TemplUtl.firstbreak( r ); | |||
end | |||
end | |||
return r or ""; | |||
end -- p.firstbreak | |||
function p.from( frame ) | |||
local r = frame:getParent():getTitle(); | |||
if r then | |||
r = string.format( "{{%s}}", r ); | |||
end | |||
return r or ""; | |||
end -- p.from | |||
function p.isRedirect() | function p.isRedirect() | ||
return mw.title.getCurrentTitle().isRedirect and "1" or ""; | return mw.title.getCurrentTitle().isRedirect and "1" or ""; | ||
end -- p.isRedirect | end -- p.isRedirect | ||
function p.nowiki1( frame ) | |||
local r = ( frame.args[ 1 ] ); | |||
if r then | |||
r = mw.text.trim( r ); | |||
if r ~= "" then | |||
r = TemplUtl.nowiki1( r ); | |||
end | |||
end | |||
return r or ""; | |||
end -- p.nowiki1 | |||
p.failsafe = function ( frame ) | p.failsafe = function ( frame ) | ||
Zeile 713: | Zeile 806: | ||
end | end | ||
end | end | ||
return | return Failsafe.failsafe( since ) or "" | ||
end -- p.failsafe | end -- p.failsafe | ||
p.TemplUtl = function () | p.TemplUtl = function () |