Module:Sarang/sandbox

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Code

local p = {}


-- "File:DEK Deutsche Einheitskurzschrift - Verkehrsschrift - ?????.svg"
function p.dekname ( frame )
	local args = frame.args;
	return mw.ustring.sub ( args[1], 54, #args[1]-4 );  
end

-- function: filename 
function p.f_name ( frame )
	local gpar = frame.args
	gpar[1] = gpar[1] or mw.title.getCurrentTitle().text;
	gpar[2] = gpar[2] or '';	--	"+" value 
	gpar[3] = gpar[3] or '';	--	"-" value  
	local l_strip = 0;
	local r_strip = 0;
	if	mw.ustring.lower(mw.ustring.sub (gpar[1],-4,-4)) == '.' then
		r_strip = 4;			-- .jpg, .png, .svg
	end

	if	mw.ustring.sub (gpar[2],1,1) == "+" then
		  l_strip = tonumber(mw.ustring.sub (gpar[2],2))
	end
	if	mw.ustring.sub (gpar[3],1,1) == "+" then
		  l_strip = tonumber(mw.ustring.sub (gpar[3],2))
	end
	if	mw.ustring.sub (gpar[2],1,1) == "-" then
		  r_strip = r_strip+tonumber(mw.ustring.sub (gpar[2],2))
	end
	if	mw.ustring.sub (gpar[3],1,1) == "-" then
		  r_strip =  r_strip+tonumber(mw.ustring.sub (gpar[3],2))
	end
	return 	mw.ustring.sub (gpar[1], 1+l_strip, #gpar[1]-r_strip)
end

--
function p.led_segm ( frame )
	local args = frame.args;
	local part = mw.ustring.sub ( args[1], 11, #args[1]-4 );  -- "File:7-segment ???.svg"
	local patt = '';
	if mw.ustring.find( part, 'a') then patt = patt .. '1' else patt = patt .. '0'; end;
	if mw.ustring.find( part, 'b') then patt = patt .. '1' else patt = patt .. '0'; end;
	if mw.ustring.find( part, 'c') then patt = patt .. '1' else patt = patt .. '0'; end;
	if mw.ustring.find( part, 'd') then patt = patt .. '1' else patt = patt .. '0'; end;
	if mw.ustring.find( part, 'e') then patt = patt .. '1' else patt = patt .. '0'; end;
	if mw.ustring.find( part, 'f') then patt = patt .. '1' else patt = patt .. '0'; end;
	if mw.ustring.find( part, 'g') then patt = patt .. '1' else patt = patt .. '0'; end;
	return patt;
end

-- defaultsorr
function p.defsrt ( frame )
	local gprm = frame.args	-- global parms
	if mw.ustring.sub (gprm[1],1,4) == "DEU " then
		local ptab = mw.text.split(gprm[1] , ' ', true)
		return ptab[2]
	else 
		return gprm
	end
end


----------------- Module: Igenmod ----------------------------------------------

-- 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 name without extension
function p.filenam ( )
	local page = mw.title.getCurrentTitle().text;
	local ptab = mw.text.split(page , '.', true)
	return	table.concat(ptab, '.', 1, #ptab-1 )
end -- end function extText

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


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