Module:Galeria

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

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

The format parameter

[edit]

When none is specified, the module tries to find the best fitting default format. If another display format is wanted it can be specified;
but a wrong format parameter which does not fit to the file name parameters gives troubles.

  • 0: no captions, every unnamed parameter specifies a valid file name
  • 1: 'showfilename' captions, every unnamed parameter specifies a valid file name
  • 2: parameter pairs of file name and caption; when the second value is missing the file name is displayed, with link
  • 3: as format 2, but file name captions are displayed without link

Format 0 and 1 may not contain caption parameters; format 2 and 3 can work with a mixture of file names and caption texts.

The file parameters

[edit]

Each file to be displayed can be spezified by one, or by two parameters. The chain of files starts of course with a file name; the following parameter can be the caption for the current file, or it specifies the next file name. When such a second parameter is the same as the first one, possibly only differing in case and space/understroke, it is used as a caption for the first parameter. Otherwise every second parameter seeming to be a valid file name is considered to be the next file name, and the first parameter is treated as an unpaired, single file name.

An easy solution is the specification of empty parameters for captions: it signals that the file name should also be used as the caption.

How it works

[edit]

This module gets all parameters from the parent template of G, creates the attributes and values, and passes them to the gallery tag.

The mainly invoker is template:{{G}}.
The template G itself does nothing than to invoke this module, it does not specify any own parameters.

The module analyzes these parameters. In special, the unnamed parameter(s) can contain an unlimited number of items.

First of all, the contents of all unnamed parameters are concatenated to one string:

  1. concatenates all pipe-separated items and separates them by an ACK
  2. changes also the newline separators to ACK separators.

Then when the parameter for 'format' is not specified, but

a second parameter exists (after a pipe), and
the first parameter is preceded by a newline, and
the second parameter does not specify a file name,
then the format parameter is defaulted to the value "2".

Using the ACK instead of the slash "/" tolerates items containing slashes, as URL web addresses.
For the format 2, pairs of items are built, with possible defaulting of the description item.

Finally the total amount of items is passed to the Template:{{G/layout}}, together with the other parameters.
Because the ‎<gallery>...‎</gallery> tag does not support the suppression of the showfilename option,
two transclusions of the gallery tag are necessary - one with, and the other one without showfilename.

Helper functions

[edit]

The function "converse" gets a size parameter (possible format examples: 100, x260, 200x240 or 150px, x280px, 220x250px)
and returns the widths and/or the heights values; the local function "parseval" is used to parse the values.

The function "filename" returns the {{PAGENAME}} without the extension; also " Wellcome X99999999" will be removed.

The function "pagename" can change all understrokes to spaces.

The function "iffile" performs a rough check whether a string can be a valid file name. Depending on a second parameter, it returns either a flag or the file name without extension.

Other main function

[edit]

The function 'gallang" is invoked from the template {{Lgallery}} to diplay multilanguage SVG files.

See also

[edit]

Code

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';
			fmat = 'f';
		end
		if mw.ustring.sub (name, -5) == '.jpeg' then 
			name = mw.ustring.sub (name, 1, #name-4)..'jpg';
			fmat = 'f';
		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 )	--	depending on parameter 'f'
	if iffil ( fnam ) == "f" then
		if		fmat == '2'  then
			return "|<div class=\"center\">{{F|" .. fnam .. "}}</div>"
		elseif	fmat == '4'  then 
			return "|<div class=\"center\">{{F|" .. fnam .."|"..namprt(fnam).."}}</div>"
		elseif	fmat == '5'  then 
			return "|<div class=\"center\">" .. namprt ( fnam ) .. "</div>"
		else 
			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" or fmat == "4" or fmat == "5" 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

--  function filename without extension (and the "Wellcome ..." text)
function p.filename ( frame )
	local fnam = mw.title.getCurrentTitle().text;
	if mw.ustring.sub( fnam, -4 ) == '.jpg'
		then fnam = mw.ustring.sub( fnam, 1, #fnam-4 );
		local posf = mw.ustring.find( fnam, ' Wellcome' );
		if posf
			then fnam = mw.ustring.sub( fnam, 1, posf-1 );
		end
	elseif mw.ustring.sub( fnam, -4, -4 ) == '.'
		then fnam = mw.ustring.sub( fnam, 1, #fnam-4 );
	elseif mw.ustring.sub( fnam, -5, -5 ) == '.'
		then fnam = mw.ustring.sub( fnam, 1, #fnam-5 );
	end
	return fnam;
end 
	
return p;