Module:Igenmod

From Multilingual Bookbinding Dictionary
Jump to navigation Jump to search

This module contains some of the functions needed by Image generation

* {{#Invoke:Igenmod|wrkdate}}
* {{#Invoke:Igenmod|exttext}}
* {{#Invoke:Igenmod|extcode}}
* {{#Invoke:Igenmod|default}}
* {{#Invoke:Igenmod|unspec}}
* {{#Invoke:Igenmod|categ }}
* {{#Invoke:Igenmod|sortkey}}
* {{#Invoke:Igenmod|split|aa/bb/cc}}
* {{#Invoke:Igenmod|subst|testtext}}
* {{#Invoke:Igenmod|subst|###ttext}}
* {{#Invoke:Igenmod|subst|***ttext}}
* {{#Invoke:Igenmod|subst|:::ttext}}
* {{#Invoke:Igenmod|subst|;;;ttext}}
------------ in alpha: 
* {{#Invoke:Igenmod|getdate}}
* {{#Invoke:Igenmod|invdate}}
* {{#Invoke:Igenmod|sysdate}}
* {{#Invoke:Igenmod|invtest}}
* {{#Invoke:Igenmod|cattest}}
* {{#Invoke:Igenmod|newfile|10}}
* {{#Invoke:Igenmod|inkscape}}
* {{#Invoke:Igenmod|compare}}

local p = {}
-----------------------------------------------------------------------

-- get workdate via "Module:File" 
function p.wrkdate ( )
	local fmod = require( "Module:File" )
	local fileObj = fmod.File( mw.title.getCurrentTitle().text )
	return fileObj.dateWorkCreated()
end -- end function wrkDate


-- 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

-- 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


-- 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


-- 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 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 Inkscape


-- 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 '&#59;' .. mw.ustring.sub( char, 2, #char );
	else
		return char;
	end
end -- end function subst


-- 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


-- 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

----------------------- TEST ------------------------------------------

-- 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


-- get workdate (sometimes does not work properly)
function p.getdate ( )
	local parse = require("Module:HTMLParser").parse
	local fname = mw.title.getCurrentTitle().text
	local fulln = mw.title.new(fname, 6).prefixedText
	local b,exp = pcall (mw.getCurrentFrame():expandTemplate{title = ':' .. fulln, {}  } )
	if not b then
 		return '2000-00-00'
	end
	local root  = parse ('<html>' .. exp:gsub('<nowiki>.*?</nowiki>', '') ..  '</html>')
	local tdEl  = root('#fileinfotpl_date')
	for td in pairs(tdEl) do
		local timeElem = td.parent('time')
		for te in pairs(timeElem) do
			return te.attributes['datetime']
		end
	end
end -- end  function getDate


-- 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