Module:Galeria

From "PTTLink Wiki"
Revision as of 13:33, 8 January 2022 by Mediawiki>Sarang (trimmed)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Galeria/doc

local p = {}

-- parses the size value
local function parseval ( value, parm2 ) 
	local par  = value or ''
	local req  = parm2 or ''
	if par ==  '' then
		return ''
	else
		if  mw.ustring.sub(par, -2) == 'px' then 
			par = mw.ustring.sub(par, 1, mw.ustring.len(par) -2)
		end
		local pos = mw.ustring.find(par, 'x');
		if req == 'p' then
			return par;
		elseif req == 'w' then
			if pos == nil then
				return par;
			elseif pos > 1 then
				return mw.ustring.sub(par, 1, pos -1);
			else
				return ''
			end
		elseif  req == 'h' then
			if pos == nil then
				return '';
			else
				return mw.ustring.sub(par, pos +1)
			end
		end
	end
end	--	function: parseval

-- checks whether the value is text, or the next file
local function iffil ( ftext, optn ) 
	local name  = mw.ustring.lower(ftext or '') ;		-- trimmed
	local fmat  = 'x';			-- assume it's text
	if mw.ustring.sub ( name or ' ', 1, 1 ) == "*"
	or mw.ustring.sub ( name or ' ', 1, 1 ) == "." then
		fmat = 'f'			--	short code for filename
	elseif name ~= "" then
 		local inx = mw.ustring.find( name .. "\n", "\n" );
		name = mw.ustring.sub ( name, 1, inx - 1 );
		if mw.ustring.sub (name, -5) == '.tiff' then 
			name = mw.ustring.sub (name, 1, #name-4)..'tif';
		end
		if mw.ustring.sub (name, -5) == '.jpeg' then 
			name = mw.ustring.sub (name, 1, #name-4)..'jpg';
		end
		if #name > 4 and
			mw.ustring.sub (name, -4, -4 ) == '.' then 
			local map = {svg=1, png=1, jpg=1, gif=1, tif=1, xcf=1, pdf=1};
			if 	map[mw.ustring.sub (name, -3)] then
				fmat = "f";                     -- it looks like a file name
			end
	 	end
	end
	if optn == 'F' then fmat = mw.ustring.sub (name, 1, #name-4) end
	return fmat
end	--	function: iffil

-- returns the file name without the .ext
local function namprt ( filnam )
	return iffil ( filnam, 'F')
end	--	function: namprt 

-- changes all occurences of "_" against " "
local function pagnam ( filnam )
	return mw.ustring.gsub( filnam or ' ', '_', ' ' ),_
end --  function: pagename 

-- prepares the display of the second param
local function disp2nd ( fnam, fmat )
	if iffil ( fnam ) == "f" then
		if fmat == '2' then
			return "|<div class=\"center\">{{F|" .. fnam .. "}}</div>"
		else		-- fmat==3
			return "|<div class=\"center\">" .. pagnam ( fnam ) .. "</div>"
		end
	else
		return "|<div class=\"center\">" .. fnam .. "</div>"
	end
end --  function: disp2nd



-- main function 
function p.gallery ( frame )
 	local ppar = mw.getCurrentFrame():getParent().args;
	local tagtab = {};
	local instab = {};
	local form   = ppar.f or ppar.format or "";
	local fmat   = ppar.f or ppar.format or "2";
	local mode   = ppar.m or ppar.mode or "";
	local insv   = "";
	local strg   = "";
	tagtab [1]   = "\n";
	
	for _, v in ipairs(ppar) do
		v = mw.text.trim (v);
		strg = strg .. "\006"  .. v                  -- replace pipe by ACK
	end
	strg = mw.ustring.gsub( strg, "[\n]" , "\006" ); -- replace CRLF by ACK
	
--------------------------------
	local par2 = ppar[2] or ""
	if fmat == "" then						--	when format NOT specified
		if par2 ~= "" then			--	when second parm:
 			if iffil ( par2 ) == "f" then
	 			fmat = "1"					--	default format="1"
		 	else
	 			fmat = "2"					--	default format="2"
		 	end
		else							--	no second parm			
			fmat = "1"						
		end
	end
--------------------------------
	for _, v in ipairs(mw.text.split(strg , "\006") ) do
		v = mw.text.trim (v);
		local scnd = ""
		if fmat == "1" or fmat == "2" or fmat == "3" then	-- param pairs
			if insv == "" then		--	1st value
				insv = v;
			else					--	2nd value:
				if iffil ( v ) == "f" then					-- it's a filename
					if pagnam ( mw.ustring.lower(insv) ) == pagnam ( mw.ustring.lower(v) ) then
						table.insert(instab, insv .. disp2nd (    v, fmat ) );
					else									-- it is the next file name
						table.insert(instab, insv .. disp2nd ( insv, fmat ) );
						scnd = v;
					end
				else				--	may be a short code
					if v == "" or v == "+" then 
						table.insert(instab, insv .. disp2nd ( insv, fmat ) );
					else
						table.insert(instab, insv .. disp2nd ( v, '3' ) );	-- just text
					end
				end
				insv = scnd;
			end

		else						-- fmat = 0, 1 
			table.insert(instab, v ); -- table.insert(instab, fmat fma2 );	
		end	-- if fmat
	end		-- for

	if insv ~= "" then		--	last value single?	
		if fmat == "2" or fmat == "3" then		-- make a pair
			table.insert(instab, insv .. disp2nd ( insv, fmat ) );
		else
			table.insert(instab, insv );
		end
	end
--
	for i, v in ipairs( instab ) do
		if v == "" or v == "+" then 
			tagtab [1] = tagtab [1] .. "\n"			-- same 
		else
			if  v == "*" then
				local titl = mw.title.getCurrentTitle();
				v = titl.text; 
			end	
			if mw.ustring.sub (v, 1, 1)  == "." then 
				local titl = mw.title.getCurrentTitle();
				local namp = namprt ( titl.text );
				if		v == "." 
				or		v == ".p" then	v = namp..".png"
				elseif	v == ".j" then	v = namp..".jpg"
				elseif	v == ".g" then	v = namp..".gif"
				elseif	v == ".s" then	v = namp..".svg"
				elseif	v == ".t" then	v = namp..".tif"
				else	v = namp..v
				end	
			end	
			tagtab [1] = tagtab [1] .. "\n" .. v	-- add parm
		end
	end
--
--
	if      mode == 'n' then mode = 'nolines'
	 elseif mode == 'p' then mode = 'packed'
	 elseif mode == 'o' then mode = 'packed-overlay'
	 elseif mode == 'h' then mode = 'packed-hover'
	 elseif mode == 's' then mode = 'slideshow'
	 elseif mode == 't' then mode = 'traditional'
	end
	local wide  = ppar.w or ppar.width  or ppar.widths or '';
	local high  = ppar.h or ppar.height or ppar.heights or '';
	local disp  = ppar.d or ppar.dis or ppar.disp or ppar.display or '';
	if wide .. high == '' and disp ~= '' then
		wide = parseval (disp, 'w')
		high = parseval (disp, 'h')
	end
	tagtab [2]   = mode;
	tagtab [3]   = ppar.c or ppar.caption or '';
	tagtab [4]   = ppar.p or ppar.perrow or '';
	tagtab [5]   = wide;
	tagtab [6]   = high;
	tagtab [7]   = ppar.s or ppar.style or '';
	tagtab [8]   = ppar.a or ppar.attr or ppar.class or '';
	tagtab [9]   = ppar.t or ppar.text or '';
	tagtab [10]  = fmat;
	if	fmat == ''  then
		tagtab [10] = '1';		-- default format
	end
	if	form..wide..mode == ''  then
		if ppar.m == nil and ppar.mode == nil then	-- mode empty ?
			tagtab [2] = 'nolines';		-- only when completely missing
		end
	end
	return mw.getCurrentFrame():expandTemplate{ title = "G/layout", args = tagtab };
end	-- function gallery



-- main function gallang
function p.gallang ( frame )
	local gpar = frame.args;
	local temp = '';
	if gpar.sw == "y" then temp = "y" end	-- and fmat = '0'
	local ppar = mw.getCurrentFrame():getParent().args;-- parent parms
	local file = ppar.file or mw.title.getCurrentTitle().text or "";
	local fmat = ppar.f or ppar.format or "";
	local mode = ppar.m or ppar.mode or "";
	local wide = ppar.w or ppar.width or "";
	local high = ppar.h or ppar.height or "";
	if ppar.sw == "y" then 
		  temp = "y" 
		  fmat = "0"
	end
	local tagt = {};
	tagt [1]   = "\n";
	
	for _, v in ipairs(ppar) do
		v = mw.text.trim (v);
		if v ~= '' and file ~= '' then	-- add parm
			if temp == "y" 
				then tagt [1] = tagt [1] .. file .. "|lang="..v.."|''Key:'' "..v.."\n"
				else tagt [1] = tagt [1] .. file .. "|lang="..v.."|{{LangCapt|"..v.."}}\n"
			end
		end
	end
-- exists param "file" and at least one lang ?

	if      mode == 'n' then mode = 'nolines'
	 elseif mode == 'p' then mode = 'packed'
	 elseif mode == 'o' then mode = 'packed-overlay'
	 elseif mode == 'h' then mode = 'packed-hover'
	 elseif mode == 's' then mode = 'slideshow'
	 elseif mode == 't' then mode = 'traditional'
	end
	local wide  = ppar.w or ppar.width  or ppar.widths or '';
	local high  = ppar.h or ppar.height or ppar.heights or '';
	local disp  = ppar.d or ppar.dis or ppar.disp or ppar.display or '';
	if wide .. high == '' and disp ~= '' then
		wide = parseval (disp, 'w')
		high = parseval (disp, 'h')
	end
	tagt [2] = mode;
	tagt [3] = ppar.c or ppar.caption or '';
	tagt [4] = ppar.p or ppar.perrow or '';
	tagt [5] = wide;
	tagt [6] = high;
	tagt [7] = ppar.s or ppar.style or '';
	tagt [8] = ppar.a or ppar.attr or ppar.class or '';
	tagt [9] = ppar.t or ppar.text or '';
	tagt [10]= fmat;
	if	fmat == ''  then
		tagt [10] = '1';		-- default format
	end

	if tagt [1] == '\n' then 
		return		-- empty
	else
		return mw.getCurrentFrame():expandTemplate{ title = "G/layout", args = tagt };
	end
end	-- function gallang


-- convert size to width and height
function p.converse (frame) 
	local gpar = frame.args
	return parseval ( gpar[1], gpar [2] );
end

-- remove final "px"
function p.getp (frame) 
	local gpar = frame.args
	return parseval ( gpar[1], "p");
end
function p.getw (frame) 
	local gpar = frame.args
	return parseval ( gpar[1], "w");
end
function p.geth (frame) 
	local gpar = frame.args
	return parseval ( gpar[1], "h");
end

-- change understrokes
function p.pagename (frame ) 
	local gpar = frame.args
	return mw.ustring.gsub( gpar[1] or ' ', '_', ' ' ),_
end

-- check whether it can be a filename
function p.iffile ( frame ) 
	local gpar = frame.args 
	return iffil ( gpar[1], gpar[2] );
end
	
return p;