Mô đun:TemplUtl

local TemplUtl = { suite  = "TemplUtl",
serial = "2021-01-01",
item   = 52364930 };
--=
Utilities to support template programming.
**
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 >>" );
sign = string.format( "%s",  sign,  tostring( btn ) );
top:wikitext( sign, " ", alert );
mw.addWarning( tostring( top:attr( "role", "alert" ) ) );
elseif not always then
err:css( { "display"     = "none" } );
--      err:css( { "display"     = "inline-block",
--                 "line-height" = "0",
--                 "max-height"  = "0",
--                 "max-width"   = "0",
--                 "visibility"  = "hidden" } );
end
return tostring( err );
end -- TemplUtl.failure()
TemplUtl.fake = function ( access )
-- Simulation of template transclusion
-- Precondition:
--    access  -- string; page name (template)
if type( access ) == "string" then
local s = mw.text.trim( access );
if s ~= "" then
local t = mw.title.new( s, 10 );
if not mw.title.equals( mw.title.getCurrentTitle(), t )  and
t.exists then
t:getContent();
end
end
end
end -- TemplUtl.fake()
TemplUtl.fakes = function ( array, frame, ahead, answer )
-- Simulation of template transclusions
-- Precondition:
--    array   -- table, with template title strings
--    frame   -- object, or nil
--    ahead   -- string, or nil, with common prefix
--    answer  -- true, or nil, for list creation
-- Postcondition:
--    Returns string, if answer requested
local e = framing( frame );
local f = function ( a )
e:expandTemplate{ title = a };
end
local s = ahead or "";
local r;
for k, v in pairs( array ) do
if type( k ) == "number" and
type( v ) == "string" then
v = s .. mw.text.trim( v );
pcall( f, v );
if answer then
if r then
r = r .. "\n";
else
r = "";
end
r = string.format( "%s* %s", r, v, v );
end
end
end -- for k, v
return r;
end -- TemplUtl.fakes()
TemplUtl.feasible = function ( address )
-- Does this describe an URL beginning?
-- Precondition:
--    address  -- string; what to inspect, URL presumed
-- Postcondition:
--    Returns true, if URL beginning
local start, r = address:match( "^%s*((%a*:?)//)" );
if start then
if r == "" then
r = true;
elseif r:sub( -1, -1 ) == ":" then
local schemes = ":ftp:ftps:http:https:";
r = ":" .. r:lower();
if schemes:find( r, 1, true ) then
r = true;
else
r = false;
end
else
r = false;
end
end
return r;
end -- TemplUtl.feasible()
TemplUtl.feed = function ( area, ahead, at, after )
-- Detect next free "|" or "}}"
-- Precondition:
--     area   -- string; template transclusion
--     ahead  -- string; opening element, or false
--     at     -- number; byte position in area where to start
--     after  -- true, if only to search for "}}"
-- Postcondition:
--    Returns
--          -- number; byte position in area
--             -- before "|" or "}}", may be at end
--             -- to continue search; ahead has been closed
--          -- true, if to be continued at number
local j    = at;
local loop = true;
local c, k, r, s, seek;
if after then
seek = "{}<";
else
seek = "%%|{}<:";
end
while loop do
j = area:find( seek, j );
if j then
c = area:byte( j, j );
if c == 123 then    -- {
k = j + 1;
if area:byte( k, k ) == 123 then
k = k + 1;
if area:byte( k, k ) == 123 then
j, loop = TemplUtl.feed( area, "