Module:Sarang/Parlist

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
--Experimental module for some functions

-- now "Module:Parmlist":
--	gets one parameter in the structure aaa/bbb/ccc (also with html-reserved characters!)
--  returns [to the template] the parameters in the structure aaa|bbb|ccc

-- now "Module:Color2dec":
--functions hexval, hexdec

--end

local p = {}


--

-- global=args[1], local=frame
function p.tincture ( frame )
	local args = frame.args
	local inp = ''
	if  args then
        inp   =  args[1] or 'glo'
	elseif  frame then
         inp  =  frame   or 'loc'
--	else inp  = 'Tinct wo'
	end
	local low  =  ( string.lower ( inp ) );
	if  mw.ustring.sub ( inp, 1, 1 ) == '#' then
		if  #low ==  4  then
			return inp;
		elseif   mw.ustring.sub ( low, 2, 2 ) ==  mw.ustring.sub ( low, 3, 3 )
			and  mw.ustring.sub ( low, 4, 4 ) ==  mw.ustring.sub ( low, 5, 5 )
			and  mw.ustring.sub ( low, 6, 6 ) ==  mw.ustring.sub ( low, 7, 7 )  then
			local  low  =  ( string.upper ( inp ) );
			return '#'  .. mw.ustring.sub ( low, 2, 2 ) 
						.. mw.ustring.sub ( low, 4, 4 ) 
						.. mw.ustring.sub ( low, 6, 6 );
		else			
			return inp;
		end	
	end
	local out  =  '';
	if     low == 'argent'    then  out = '#FFF'
	elseif low == 'argent-d'  then  out = '#E7E7E7'
	elseif low == 'azure'     then  out = '#0F47AF'
	elseif low == 'carnation' then  out = '#F2A772'
	elseif low == 'céleste'   then  out = '#89C5E3'
	elseif low == 'celeste'   then  out = '#89C5E3'
	elseif low == 'cendrée'   then  out = '#999'
	elseif low == 'gules'     then  out = '#DA121A'
	elseif low == 'or'        then  out = '#FCDD09'
	elseif low == 'purpure'   then  out = '#9116A1'
	elseif low == 'sable'     then  out = '#000'
	elseif low == 'tawny'     then  out = '#9D5333'
	elseif low == 'vert'      then  out = '#078930'
-- colours less common:	
	elseif low == 'brunâtre'  then  out = '#3F1FFF'
	elseif low == 'sanguine'  then  out = '#BC3F4A'
	elseif low == 'murrey'    then  out = '#C54B8C'
	elseif low == 'orange_t'  then  out = '#FC7A11'
	else   
		local convc = require( "Module:convCSS" )
		out  =  convc.main ( inp )
	end
	return out;
end

-- local=frame/global=args[1]
function p.decode ( frame )
	local args = frame.args
	local str = ''
	if  args then
       str   =  args[1] or 'glo'
	elseif  frame then
       str   =  frame or 'loc'
	else str = 'decode wo'
	end
    if  mw.ustring.sub ( str, 1, 1 ) == '#' then 
         return '#' .. mw.ustring.sub( str, 2, #str );
    elseif mw.ustring.sub ( str, 1, 1 ) == '*' then 
         return '*' .. mw.ustring.sub( str, 2, #str );
    elseif mw.ustring.sub ( str, 1, 1 ) == ':' then 
         return ':' .. mw.ustring.sub( str, 2, #str );
    elseif mw.ustring.sub ( str, 1, 1 ) == ';' then 
         return '&#59;' .. mw.ustring.sub( str, 2, #str );
    else
         return str;
    end
end

-- alias test
function p.subst  ( frame )
	return p.decode ( frame )
end
-- local=frame/global=args[1]
function p.inptest ( frame )
	local args = frame.args
	if  args then
        return  'g_args1: ' .. args[1] 
	elseif  frame then
        return  'l_frame: ' .. frame
	end
end
--
function p.inpcall ( frame )
	local args = frame.args
	local parm = p.inptest ( args[1] );
	return  parm
end
--
function p.testinp ( frame_args, arg_list )
	local  testfunc = require ( "Module:String" )
	local  testparm = testfunc._getParameters ( frame_args, arg_list )
	return testparm; 
end  
--
function p.oneparm ( inp_parm )
	local hexcod = inp_parm 
	local decval = {}
	if #inp_parm == 7 then
		decval [1] = hexdec( string.byte (hexcod, 2)) * 16 + hexdec (string.byte (hexcod, 3))
		decval [2] = hexdec( string.byte (hexcod, 4)) * 16 + hexdec (string.byte (hexcod, 5))
		decval [3] = hexdec( string.byte (hexcod, 6)) * 16 + hexdec (string.byte (hexcod, 7))
		return table.concat( decval, '|' )
	else
		return table.concat( inp_parm, '|' )
--		return inp_parm 
	end
end  
 -- if no argument provided than issue msg
function p.testone(frame)
    local args = frame.args
    if args[1] == nil then
        return 'parm missing'
    end
    return (args[1])
end
-- 
function p.tsthex ( frame )
    local args = frame.args
	local hexcod = args[1] or '#000000'; 
	local septor = args[2] or '_'; 
	local decval = {};
	decval = convert ( hexcod ); 
	return table.concat( decval, septor  )
end  
--
function p.subst ( frame )
	local args = frame.args;
    local str   =  args[1]; 
    if     mw.ustring.sub ( str, 1, 1 ) == '#' then 
         return '#' .. mw.ustring.sub( str, 2, #str );
    elseif mw.ustring.sub ( str, 1, 1 ) == '*' then 
         return '*' .. mw.ustring.sub( str, 2, #str );
    elseif mw.ustring.sub ( str, 1, 1 ) == ':' then 
         return ':' .. mw.ustring.sub( str, 2, #str );
    elseif mw.ustring.sub ( str, 1, 1 ) == ';' then 
         return '&#59;' .. mw.ustring.sub( str, 2, #str );
    else
         return str;
    end
end


--
local function hexdec ( arg )
	local dig  =  ( string.upper ( string.char ( arg ) ) );
	local dec  =  0;
	if     dig == 'F' then
		dec = 0xF
	elseif dig == 'E' then
		dec = 0xE
	elseif dig == 'D' then
		dec = 0xD
	elseif dig == 'C' then
		dec = 0xC
	elseif dig == 'B' then
		dec = 0xB
	elseif dig == 'A' then
		dec = 0xA
	else
		dec = tonumber (dig)
	end
	return dec;
end  

-- 
local function convert ( arg )
	local hex  =  arg or '#00ff00';
    if  mw.ustring.byte ( hex, 1 ) ~= '#' then 
        error ('value "' .. hex .. '" cannot be converted to decimal')
	end  
	local dec  =  {};
	if #hex == 7 then
		dec [1] = hexdec ( string.byte (hex, 2)) * 16 + hexdec (string.byte (hex, 3))
		dec [2] = hexdec ( string.byte (hex, 4)) * 16 + hexdec (string.byte (hex, 5))
		dec [3] = hexdec ( string.byte (hex, 6)) * 16 + hexdec (string.byte (hex, 7))
	else
		dec [1] = hexdec ( string.byte (hex, 2)) * 16 + hexdec (string.byte (hex, 2))
		dec [2] = hexdec ( string.byte (hex, 3)) * 16 + hexdec (string.byte (hex, 3))
		dec [3] = hexdec ( string.byte (hex, 4)) * 16 + hexdec (string.byte (hex, 4))
	end  
	return dec;
end


-- 
function p.hexval ( frame )
    local args = frame.args
	local hexcode = args[1] or '#000000'; 
	local decval = {};
	decval = convert ( hexcode ); 
	return decval;
end

--
function p.location ( frame )
    local args = frame.args;
	local lstr = mw.ustring.gsub( args[1], "([°′″ ,])" , "/" );
	local ltab = { };
	for _, v in ipairs(mw.text.split(lstr , "/") ) do
		if v ~= "" then 
			table.insert(ltab, v )
		end
	end
  	return mw.getCurrentFrame():expandTemplate{ title = 'location', args = ltab };
end
--
function p.location0 ( frame )
    local args = frame.args;
	local lstr = mw.ustring.gsub( args[1], "([°′″ ])" , "/" );
	local ltab = { };
	for _, v in ipairs(mw.text.split(lstr , "/") ) do
		table.insert(ltab, v )
	end
  	return mw.getCurrentFrame():expandTemplate{ title = 'Igen/tst', args = ltab };
end
--
function p.location1 ( frame )
    local args = frame.args;
	local loctab = { '"' .. mw.ustring.gsub( args[1], "([°′″ ])" , '", "' ) .. '"' };
  	return mw.getCurrentFrame():expandTemplate{ title = 'Igen/tst', args = loctab };
end

function p.location2 ( frame )
	local args = frame.args;
	return mw.getCurrentFrame():expandTemplate{ title = 'Igen/tst', 
  		args = { mw.ustring.gsub( args[1], "([°′″ ])" , "|" ) } };
end

   
--
function p.tbcgen ( frame )
    local args = frame.args;
    local arg1 = args[1] or '#000000';
	local tbctab = {};
	local hexcod = '';
	hexcod = p.tincture ( arg1 );
	decval = convert ( hexcod ); 
	tbctab [1]   = '0';
	tbctab [2]   = decval [1];
	tbctab [3]   = decval [2];
	tbctab [4]   = decval [3];
	tbctab [5]   = p.decode ( args[2] or 'couleur');
	return mw.getCurrentFrame():expandTemplate{ title = 'Tbc', args =  tbctab };
end
	
return p;