Module:Galeria

From Multilingual Bookbinding Dictionary
Jump to navigation Jump to search

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 "g" was not recognized as a supported language code."g" was not recognized as a supported language code. "g" was not recognized as a supported language code..
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 "g/layout" was not recognized as a supported language code."g/layout" was not recognized as a supported language code. "g/layout" was not recognized as a supported language code., together with the other parameters.
Because the 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.


A small 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.

See also



local p = {}

--
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 p.gallery ( frame )
 	local args = mw.getCurrentFrame():getParent().args;
	local tagtab = {};
	local instab = {};
	local fmat   = args.f or args.format or "";
	local mode   = args.m or args.mode or "";
	local insv   = "";
	local strg   = "";
	tagtab [1]   = "\n";
	
	for _, v in ipairs(args) do
		strg = strg .. "\006"  .. v                  -- replace pipe by ACK
	end
	strg = mw.ustring.gsub( strg, "[\n]" , "\006" ); -- replace CRLF by ACK
	
	local arg2 = args[2] or ".svg"
	if fmat == "" then                                -- when format NOT specified
		if args[2] ~= "" then
 			if mw.ustring.sub (args[1], 1, 1 ) == "\n" then
				local inx = mw.ustring.find( arg2 .. "\n", "\n" );
				arg2 = mw.ustring.sub ( arg2, 1, inx - 1 );
				inx = string.lower(string.gsub(arg2,"^.*%.([^%.]*)$","%1"));
				local map = {svg=1, png=1, jpg=1, jpeg=1, gif=1, tif=1, tiff=1, xcf=1, pdf=1 }
	 			if not map[inx] then
	 				fmat = "2"                       -- default format="2"
		 	 	end
		 	end
		end
	end

	for _, v in ipairs(mw.text.split(strg , "\006") ) do
		if fmat == "2" then 
			if insv == "" then 
				insv = v;
			else 
				if v == "" then 
					v = "{{F|{{PAGENAME:" .. insv .. "}}}}";
				end
				table.insert(instab, insv .. "|<center>" .. v .. "</center>" );
				insv = ""
			end
		else
			table.insert(instab, v )	
		end
	end
	for i, v in ipairs( instab ) do
		if v == "" or v == "+" then 
			tagtab [1] = tagtab [1] .. "\n"
		else
			tagtab [1] = tagtab [1] .. "\n" .. v
		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  = args.w or args.width  or args.widths or '';
	local high  = args.h or args.height or args.heights or '';
	local disp  = args.d or args.dis or args.disp or args.display or '';
	if wide .. high == '' and disp ~= '' then
		wide = parseval (disp, 'w')
		high = parseval (disp, 'h')
	end
	tagtab [2]   = mode;
	tagtab [3]   = args.c or args.caption or '';
	tagtab [4]   = args.p or args.perrow or '';
	tagtab [5]   = wide;
	tagtab [6]   = high;
	tagtab [7]   = args.s or args.style or '';
	tagtab [8]   = args.a or args.attr or args.class or '';
	tagtab [9]   = args.t or args.text or '';
	tagtab [10]  = fmat;
	if fmat == ''  then
		tagtab [10] = '1';
		if mode == ''  then 
			tagtab [2] = 'nolines';
		end
	end
	return mw.getCurrentFrame():expandTemplate{ title = "G/layout", args = tagtab };
end


-- 
function p.converse (frame) 
	local arg = frame.args
	out = parseval ( arg [1], arg [2] )
	return out;
end

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

return p;