Module:Igenmod
Jump to navigation
Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules
This module contains the functions needed by Image generation, and other templates and modules
* {{#Invoke:Igenmod|wrkdate}} returns the <s>"creation date"</s> of a file, the value given to the date parameter, yyyy-mm-dd * {{#Invoke:Igenmod|invdate}} returns the <s>"creation date"</s> inverted: a string 20301231 - yyyymmdd * {{#Invoke:Igenmod|exttext}} returns the file extension uppercased * {{#Invoke:Igenmod|extcode}} returns either "s" for SVG, "n" for anything else * {{#Invoke:Igenmod|default}} returns either "v" for SVG, "n" for anything else * {{#Invoke:Igenmod|unspec }} returns nothing for SVG, "n" for anything else * {{#Invoke:Igenmod|categ }} returns either "SVG" for SVG, "PNG" for anything else * {{#Invoke:Igenmod|split }} transcludes the template of parm2 (def: cbox) with the "/"-split values of parm1 * {{#Invoke:Igenmod|c_split}} transcludes the template of parm1 with every value of the "/"-splitting of parm2 * {{#Invoke:Igenmod|g_split}} "/"-splitting of parm1, can handle an erroneous "?" in location parms * {{#Invoke:Igenmod|q_split}} "/"-splitting of parm1, handles named parameters (for Igen) with "~"-splitting * {{#Invoke:Igenmod|t_split}} "/"-splitting of parm1, transcludes the 1st split of parm1 with all the other values * {{#Invoke:Igenmod|subst }} returns the input text with eventually the first character substitited * {{#Invoke:Igenmod|gsubst }} returns the input text with all occurrences of "/" replaced by "|" * {{#Invoke:Igenmod|catsort}} returns gsubstet linefeeds replaced by categories * {{#Invoke:Igenmod|dotrepl}} returns the input text with eventually the last character substitited * {{#Invoke:Igenmod|remprf }} returns the input text with eventually a special prefix removed elseif mw.ustring.sub (gpar[1], 1, 1) == '_' then return ' ' .. mw.ustring.sub (gpar[1], 2 ); -- leading space * {{#Invoke:Igenmod|inkscape}}special categorization for NEW Inkscape files - to find new violations ------------ functions in alpha: * {{#Invoke:Igenmod|sortkey}} * {{#Invoke:Igenmod|getdate}} * {{#Invoke:Igenmod|sysdate}} * {{#Invoke:Igenmod|invtest}} * {{#Invoke:Igenmod|cattest}} * {{#Invoke:Igenmod|newfile|10}} * {{#Invoke:Igenmod|compare}}
Code
local p = {}
-----------------------------------------------------------------------
-- get workdate via "Module:File"
function p.wrkdate ( )
local str = mw.getCurrentFrame():preprocess('{{REVISIONTIMESTAMP}}')
return str:sub(1,4) .. '-' .. str:sub(5,6) .. '-' .. str:sub(7,8)
end -- end function wrkDate
-- invert the workdate (for a descending sortkey)
function p.invdate ()
local invt = ""
local wdat = p.wrkdate () or "2020-00-00"
invt = string.format ( "%.4u" , 2030 - tonumber ( string.sub ( wdat, 1, 4 )) )
invt = invt .. string.format ( "%.2u" , 12 - tonumber ( string.sub ( wdat, 6, 7 )) )
invt = invt .. string.format ( "%.2u" , 31 - tonumber ( string.sub ( wdat, 9, 10 )) )
return invt
end -- end function invDate
-- get extension text (uppercased)
function p.exttext ( )
local page = mw.title.getCurrentTitle().text;
local ptab = mw.text.split(page , '.', true)
return mw.ustring.upper( table.concat(ptab, '', #ptab ) )
end -- end function extText
-- get extension code: "s" for SVG, "n" for anything else
function p.extcode ( )
if p.exttext () == "SVG"
then return "s"
else return "n"
end
end -- end function extCode
-- get default tag: "v" for SVG, "n" for anything else
function p.default ( )
if p.exttext () == "SVG"
then return "v"
else return "n"
end
end -- end function default
-- get unspec tag: "" for SVG, "n" for anything else
function p.unspec ( )
if p.exttext () ~= "SVG"
then return "n"
end
end -- end function unspec
-- replace a final dot
function p.dotrepl ( frame )
local lpar = frame.args
local strg = mw.text.trim( lpar[1] or '' )
local repl = mw.text.trim( lpar[2] or '' )
if mw.ustring.sub(strg, #strg) == "." then
strg = mw.ustring.sub(strg, 1, #strg-1) .. repl
end
return strg
end -- function dotrepl
-- invert the workdate, for a descending sortkey
function p.sortkey ()
local invt = ""
local wdat = p.wrkdate () or "2020-00-00"
invt = string.format ( "%.4u" , 2030 - tonumber ( string.sub ( wdat, 1, 4 )) )
invt = invt .. string.format ( "%.2u" , 12 - tonumber ( string.sub ( wdat, 6, 7 )) )
invt = invt .. string.format ( "%.2u" , 31 - tonumber ( string.sub ( wdat, 9, 10 )) )
return invt
end -- end function sortKey
-- get categ:sub: "SVG" for SVG, "PNG" for anything else
function p.categ ( )
if p.exttext () == "SVG"
then return "SVG"
else return "PNG"
end
end -- end function categ
-- function: changes #/*/:/; to \35/\42/\58/\59
function p.subst ( frame )
local gpar = frame.args
local char = mw.text.trim ( gpar[1] )
if mw.ustring.sub ( char, 1, 1 ) == '#' then
return '#' .. mw.ustring.sub( char, 2, #char );
elseif mw.ustring.sub ( char, 1, 1 ) == '*' then
return '*' .. mw.ustring.sub( char, 2, #char );
elseif mw.ustring.sub ( char, 1, 1 ) == ':' then
return ':' .. mw.ustring.sub( char, 2, #char );
elseif mw.ustring.sub ( char, 1, 1 ) == ';' then
return ';' .. mw.ustring.sub( char, 2, #char );
else
return char;
end
end -- end function subst
-- replace slash by pipe
function p.gsubst (frame)
local frmp = frame.args
return mw.ustring.gsub( frmp[1], "/" , "|" ),_; -- replace slash by pipe
end -- function gsubst
function p.catsort (frame)
local frmp = frame.args
local strg = frmp [1] or " "
strg = mw.ustring.gsub( strg, "/" , "|" ),_; -- replace slash by pipe
strg = mw.ustring.gsub( strg, "[\n]" , "]]\n[[Category:" ); -- replace lfeed by Cat
return strg
end -- function catsort
-- function textsplit (for Colorbox and COAInf.)
function p.split ( frame )
local lpar = frame.args
local parstr = mw.text.trim( lpar[1] ) or 'xx/yy/zz';
local tmplte = mw.text.trim( lpar[2] or 'Igen/cbx' ); -- or 'Igen/coa'
local argtab = mw.text.split( parstr, "/" );
-- for (trim loop) ?
return mw.getCurrentFrame(): expandTemplate { title = tmplte, args = argtab };
end
-- function cmdsplit
function p.c_split ( frame )
local lpar = frame.args
local tmplte = mw.text.trim( lpar[1] or '!' );
local parstr = mw.text.trim( lpar[2] ) or 'xx/yy/zz';
local argtab = mw.text.split( parstr, '/', true );
local indx = 1;
local splt = {};
local ppar = {}
for i, v in ipairs(lpar) do
if v ~= nil and lpar [i+2] ~= nil then
splt = mw.text.split( lpar [i+2], '/', true )
if splt [2] == nil then
indx = indx + 1
ppar [indx] = splt [1]
else
splt [1] = mw.text.trim( splt[1] or 'a' )
splt [2] = mw.text.trim( splt[2] or 'b' )
local xxx = splt [1]
ppar [splt [1]] = splt [2]
-- indx = indx + 1
-- ppar [indx] = splt [2] .. splt [1]
end
end
end
local cmdtab = {}
for v = 1, #argtab do
ppar [1] = mw.text.trim( argtab [v] )
table.insert( cmdtab, frame:expandTemplate{ title = tmplte, args = ppar } );
end
return table.concat (cmdtab)
end -- function c_split
-- function textsplit (for geo locations, with wrong "?")
function p.g_split ( frame )
local lpar = frame.args
local parstr = mw.ustring.gsub( mw.text.trim( lpar[1] ), '?', '″', 2 ) or 'xx/yy/zz';
local tmplte = mw.text.trim( lpar[2] or 'Location' ); -- or 'Object location'
local argtab = mw.text.split( parstr, "/" );
-- for (trim loop) ?
return mw.getCurrentFrame(): expandTemplate { title = tmplte, args = argtab };
end -- function g_split
-- function textsplit (for parameter names, with equal signs)
function p.q_split ( frame )
local lpar = frame.args
local parstr = mw.text.trim( lpar[1] ) or ' ';
local tmplte = mw.text.trim( lpar[2] ) or 'Igen'; --
local outtab = {};
local parind = 0;
local argtab = mw.text.split( parstr, "/" ); -- becomes "|"
local fpos = nil;
for i, v in ipairs(argtab) do
-- if v ~= '' then
v = mw.ustring.gsub( v, "¦" , "/" ); -- repl brokenbar by slash ,_
fpos = mw.ustring.find( v, '~' ); -- "~" becomes "="
if fpos == nil then
if v ~= '+' then
parind = parind + 1;
outtab [ parind ] = v; -- positional
end
else
if '+' ~= mw.ustring.sub ( v, fpos+1 ) then
outtab [ mw.ustring.sub ( v, 1, fpos-1 ) ] = mw.ustring.sub ( v, fpos+1 );
end
end
-- end
end
if mw.ustring.sub (tmplte,1,4) == 'Igen' then -- Showname
fpos = mw.ustring.find( outtab [1], ':' ); -- shortcut:
if fpos ~= nil then -- "T;xx:yy" > "T", "s=xx", "w=yy"
outtab['w'] = mw.ustring.sub ( outtab [1], fpos+1 );
outtab[1] = mw.ustring.sub ( outtab [1],1, fpos-1 );
end
fpos = mw.ustring.find( outtab [1], ';' ); -- shortcut:
if fpos ~= nil then -- "Tool;d" > "Tool", "s=d"
outtab['s'] = mw.ustring.sub ( outtab [1], fpos+1 );
outtab[1] = mw.ustring.sub ( outtab [1],1, fpos-1 );
end
-- if mw.ustring.sub (tmplte,5) ~= '/coa' then
table.insert( outtab, '+' ); -- re_insert the "+"
-- end
end
return mw.getCurrentFrame(): expandTemplate { title = tmplte, args = outtab };
end -- function q_split
-- function t_split: 1 parm "templte/p1/p2/.."
function p.t_split ( frame )
local lpar = frame.args
local parstr = mw.text.trim( lpar[1] ) or 'xx/yy/zz';
local argtab = mw.text.split( parstr, "/" );
local tmplte = mw.text.trim( table.remove ( argtab, 1 ) );
-- for (trim loop) ?
return mw.getCurrentFrame(): expandTemplate { title = tmplte, args = argtab };
end -- function t_split
-- global function with 1 parm: can remove special prefixes
-- the pairs [], <>, (), {}
-- doubles as <<, .., ((, && but of course ''not'' [[ and {{
function p.remprf ( frame )
local lpar = frame.args;
if mw.ustring.sub (lpar[1], 1, 2) == '[]'
or mw.ustring.sub (lpar[1], 1, 2) == '<>'
or mw.ustring.sub (lpar[1], 1, 2) == '()'
or mw.ustring.sub (lpar[1], 1, 2) == '{}'
or mw.ustring.sub (lpar[1], 1, 1) == mw.ustring.sub (lpar[1], 2, 2)
and mw.ustring.sub (lpar[1], 1, 2) ~= '[['
and mw.ustring.sub (lpar[1], 1, 2) ~= '{{' then
return mw.ustring.sub (lpar[1], 3 ) -- remove that prefix
else
return lpar[1]
end
end -- function remprf
-- global function with 1 parm: remove any 2-byte prefix
function p.rem2bp ( frame )
local lpar = frame.args;
return mw.ustring.sub (lpar[1], 3 )
end -- function rem2bp
-- check whether it is a new Inkscape file
function p.inkscape ( frame )
local otab = {}
if p.exttext () ~= "SVG" then
table.insert(otab, frame:expandTemplate{ title = "=", args = {" "} } );
else
local cat = "Unspec New SVG created with Inkscape"
local key = "{{padleft:{{#expr:{{CURRENTYEAR}} -{{REVISIONYEAR}}}}|2|0}}"
key = key .."{{padleft:{{#expr:{{CURRENTMONTH2}}-{{REVISIONMONTH}}}}|2|0}}"
key = key .."{{padleft:{{#expr:{{CURRENTDAY2}} -{{REVISIONDAY2}}}}|2|0}}"
table.insert(otab, frame:expandTemplate{ title = "Igen/cat", args = {cat, key } });
end
return table.concat (otab);
end -- end function inkscape
----------------------- TEST ------------------------------------------
-- check whether it is a new Inkscape file
function p.newscape ( frame )
local otab = {}
if p.exttext () ~= "SVG" then
table.insert(otab, frame:expandTemplate{ title = "=", args = {" "} } );
else
local wdat = p.wrkdate () or "2020-00-00"
local sdat = mw.getCurrentFrame():callParserFunction( "#time", { 'Y-m-d', datestr} ) or "2020-08"
if mw.ustring.sub (wdat, 1, 7) == mw.ustring.sub (sdat, 1, 7) then
local cat = "Unspec new SVG created with Inkscape"
local key = string.format ( "%.4u" , 2030 - tonumber ( string.sub ( wdat, 1, 4 )) )
key = key .. string.format ( "%.2u" , 12 - tonumber ( string.sub ( wdat, 6, 7 )) )
key = key .. string.format ( "%.2u" , 31 - tonumber ( string.sub ( wdat, 9,10 )) )
table.insert(otab, frame:expandTemplate{ title = "Igen/cat", args = {cat, key } });
-- local msg = " Template [[Template:Inkscape|{{Inkscape}}]] without W3C-specification is deprecated ! "
-- table.insert(otab, frame:expandTemplate{ title = "error", args = {msg} } );
else
table.insert(otab, frame:expandTemplate{ title = "=", args = {" "} } );
-- table.insert(otab, frame:expandTemplate{ title = "=", args = {wdat..sdat} } ); -- test output
end
end
return table.concat (otab);
end -- end function newscape
-- function: invert date
function p.invtest ( frame )
local gpar = frame.args
local wdat = mw.text.trim ( gpar[1] ) or '2020-00-00'
-- local leng = tonumber ( gpar[2] ) or 7
local sdat = mw.getCurrentFrame():callParserFunction( "#time", { 'Y-m-d', datestr} ) or "2020-08-30"
local key = string.format ( "%.4u" , 2030 - tonumber ( string.sub ( wdat, 1, 4 )) )
key = key .. string.format ( "%.2u" , 12 - tonumber ( string.sub ( wdat, 6, 7 )) )
key = key .. string.format ( "%.2u" , 31 - tonumber ( string.sub ( wdat, 9,10 )) )
return key
end -- end function invTest
-- check whether it is a new file
function p.newfile ( frame )
if p.exttext () ~= "SVG" then
return "n"
else
local leng = frame [1] or 7;
local wdat = p.wrkdate () or "2020-00-00"
local sdat = mw.getCurrentFrame():callParserFunction( "#time", { 'Y-m-d', datestr} )
local leng = tonumber ( frame.args[1] or 7 )
if mw.ustring.sub (wdat, 1, leng) ~= mw.ustring.sub (sdat, 1, leng)
then return "s"
else return "newcat"
end
end
end -- end function newFile
function p.cattest (args)
local categ = mw.ext.cattools
local category = args[1] or "Category:Igen/top subtemplates"
local page = args[2] or "Template:Igen/top:d"
if categ.hasPage( category, page )
then return page .. " ist in category " .. category .. " enthalten"
else return page .. " gibt es nicht in " .. category .. " !"
end
end
function p.sysdate ( )
return os.date ( )
end
function p.compare ( )
local wdat = p.wrkdate () or "2020-00-00"
local sdat = mw.getCurrentFrame():callParserFunction( "#time", { 'Y-m-d', datestr} )
return "W="..wdat ..", S="..sdat.."."
end
-----------------------------------------------------------------------
return p