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 ", "" },
{ "", true },
{ "", "" },
{ "]>",
"" } };
local i = 1;
local r = area;
local k, m, n;
if not TemplUtl.Delimiters then
local c, sD, sP;
TemplUtl.Delimiters = { };
for j = 1, delimiters do
table.insert( TemplUtl.Delimiters, { } );
for ji = 1, 2 do
sD = delimiters[ j ][ ji ];
sP = "";
for js = 1, sD, 1 do
c = sD:byte( js, js );
if c >= 65 and c 1 then
i = k - 1;
s = r:sub( 1, i );
else
s = "";
end
j, m = r:find( TemplUtl.Delimiters[ n ][ 2 ],
k + 1,
TemplUtl.Delimiters[ n ][ 3 ] );
if m then
r = s .. r:sub( m + 1 );
else
r = s;
break; -- while true
end
else
break; -- while true
end
end -- while true
return r;
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|item|~|@ or false
-- Postcondition:
-- Returns string -- with queried version/item, also if problem
-- false -- if appropriate
-- 2020-08-17
local since = atleast
local last = ( since == "~" )
local linked = ( since == "@" )
local link = ( since "item" )
local r
if last or link or linked or since "wikidata" then
local item = Failsafe.item
since = false
if type( item ) == "number" and item > 0 then
local suited = string.format( "Q%d", item )
if link then
r = suited
else
local entity = mw.wikibase.getEntity( suited )
if type( entity ) == "table" then
local seek = Failsafe.serialProperty or "P348"
local vsn = entity:formatPropertyValues( seek )
if type( vsn ) "table" and
type( vsn.value ) "string" and
vsn.value ~= "" then
if last and vsn.value == Failsafe.serial then
r = false
elseif linked then
if mw.title.getCurrentTitle().prefixedText
== mw.wikibase.getSitelink( suited ) then
r = false
else
r = suited
end
else
r = vsn.value
end
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()

-- Export
local p = { };

function p.faculty( frame )
return TemplUtl.faculty( frame.args[ 1 ],
frame.args[ 2 ] ) and "1"
or "";
end -- p.faculty

function p.failure( frame )
local scream = mw.text.trim( frame.args[ 1 ] or "" );
local loud = frame.args[ 2 ];
local select = frame.args.class;
if scream == "" then
scream = "?????????";
end
if loud then
loud = TemplUtl.faculty( loud, nil );
end
return TemplUtl.failure( scream, loud, select, frame );
end -- p.failure

function p.fake( frame )
TemplUtl.fake( frame.args[ 1 ] or "", frame );
return "";
end -- p.fake

function p.fakes( frame )
local list = ( frame.args.list == "1" );
local r = TemplUtl.fakes( frame.args,
frame,
frame.args.prefix,
list );
return r or "";
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( "&123;&123;%s&125;&125;", r );
end
return r or "";
end -- p.from

function p.isRedirect()
return mw.title.getCurrentTitle().isRedirect and "1" or "";
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 )
-- Versioning interface
local s = type( frame )
local since
if s == "table" then
since = frame.args[ 1 ]
elseif s == "string" then
since = frame
end
if since then
since = mw.text.trim( since )
if since == "" then
since = false
end
end
return Failsafe.failsafe( since ) or ""
end -- p.failsafe

p.TemplUtl = function ()
return TemplUtl;
end -- p.TemplUtl()

return p;