Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Iteration: Difference between revisions

From Neodyland Wiki
m expansion
m parameter correction
Line 3: Line 3:


local p = {}
local p = {}


-- for Template:Ifim  
-- for Template:Ifim  
Line 85: Line 84:
local rtab = {};
local rtab = {};
local vtab = {};
local vtab = {};
local stab = {};
local wtab = {};
local ttab = {};
local itab = {};
local ptab = {};
local ptab = {};
local btab = {};
local btab = {};
Line 97: Line 96:
local pr5  = args.par5 or "";
local pr5  = args.par5 or "";
local pr6  = args.par6 or "";
local pr6  = args.par6 or "";
local sis = args.s or args.sist or args.sisterproject or "";
local wik = args.w or args.wiki or args.sisterproject or "";
local lng = args.l or args.lang or "";
local int = args.i or args.int or args.ind or args.inter or "";
local by0  = args.by             or "";
local by0  = args.by   or "";
local prf  = args.p or args.prf or args.prefix  or ""; -- 'list' item {{Comma}}
local prf  = args.p or args.prf or args.prefix  or ""; -- 'list' item {{Comma}}
if dir == "vert" and prf == "" then
if dir == "vert" and prf == "" then
Line 112: Line 111:
rtab [i] = args ["r" .. tostring( i )] or "-";
rtab [i] = args ["r" .. tostring( i )] or "-";
vtab [i] = args ["?" .. tostring( i )] or "-";
vtab [i] = args ["?" .. tostring( i )] or "-";
stab [i] = args ["s" .. tostring( i )] or "-";
wtab [i] = args ["w" .. tostring( i )] or "-";
ttab [i] = args ["t" .. tostring( i )] or "-";
itab [i] = args ["i" .. tostring( i )] or "-";
ptab [i] = args ["p" .. tostring( i )] or "-";
ptab [i] = args ["p" .. tostring( i )] or "-";
btab [i] = args ["b" .. tostring( i )] or args ["by" .. tostring( i )] or "-";
btab [i] = args ["b" .. tostring( i )] or args ["by" .. tostring( i )] or "-";
mnum  = i;
mnum  = i;
end
end
 
for i, v in ipairs(args) do
for i, v in ipairs(args) do
local nm = nam;  if ntab[i] ~= "-" then  nm = ntab[i] end
local nm = nam;  if ntab[i] ~= "-" then  nm = ntab[i] end
Line 127: Line 125:
local p5 = pr5;  if rtab[i] ~= "-" then  p5 = rtab[i] end
local p5 = pr5;  if rtab[i] ~= "-" then  p5 = rtab[i] end
local p6 = pr6;  if vtab[i] ~= "-" then  p6 = vtab[i] end
local p6 = pr6;  if vtab[i] ~= "-" then  p6 = vtab[i] end
local ps = sis;  if stab[i] ~= "-" then  ps = stab[i] end
local pw = wik;  if wtab[i] ~= "-" then  pw = wtab[i] end
local pt = lng;  if ttab[i] ~= "-" then  pt = ttab[i] end
local pi = int;  if itab[i] ~= "-" then  pi = itab[i] end
local pf = prf;  if ptab[i] ~= "-" then  pf = ptab[i] end
local pf = prf;  if ptab[i] ~= "-" then  pf = ptab[i] end
local by = by0;  if btab[i] ~= "-" then  by = btab[i] end
local by = by0;  if btab[i] ~= "-" then  by = btab[i] end
Line 150: Line 148:
end
end
end
end
table.insert(ctab, frame:expandTemplate{ title = "F", args = {vv, nm, ds, op, p4, p5, p6, l= ps, lang= pt, p= pf, by= by} });
table.insert(ctab, frame:expandTemplate{ title = "F", args = {vv, nm, ds, op, p4, p5, p6, l= pw, lang= pi, p= pf, by= by} });
end
end
end
end
Line 222: Line 220:
end
end


-- for Template:ColorString (Igen/cbox   -   param pairs)
-- for Template:ColorString (Igen/cbox   -   param pairs)
function p.colorbox (frame)  
function p.colorbox (frame)  
local args = mw.getCurrentFrame(): getParent().args;
local args = mw.getCurrentFrame(): getParent().args;

Revision as of 18:15, 8 February 2020

Module documentation[ view · edit · history · purge ]
This documentation is transcluded from Module:Iteration/doc.

0 The module uses frequently the template {{=}} for title = "=" 0

This module contains the functions for different templates, solving their iteration problem

The MediaWiki template coding does not support the repetitive processing of a parameter set, which leads often to a very primitive and long chain of parameter checks in the kind of:
{{#if:{{{1|}}} | perform an action with parameter 1 }}
{{#if:{{{2|}}} | perform an action with parameter 2 }}
{{#if:{{{3|}}} | perform an action with parameter 3 }}
{{#if:{{{4|}}} | perform an action with parameter 4 }}
•••
•••
{{#if:{{{33|}}} perform an action with parameter 33 }}
with the disadvantage that such a construct will fail as soon as there comes a 34th parm. I always thought that with Lua and its for loops this iterations should be possible, somehow. After long searching without any success and without an idea how to perform it, I asked at several forums and got finally from User:Trappist the monk the helping hint to solve it with a control structure like:
	local out = {}
	for i, v in ipairs (args) do
		table.insert (out, frame:expandTemplate{ title = title, args = v })
	end
	return table.concat (out)

The functions contained therein are from very simple, just one parameter to pass, to more complicated ones, where e.g. a pair of parameters needs kind of a flip-flop switch when always a tuple of e.g. an item and its correlated text needs to be passed.

When there are tuples, triples etc., the table.insert occurs, when the last element is processed; this performs problems when the last group is incomplete – it should be finished with empty values to trigger the table.insert. The check with table.maxn seems not to work correctly?

This problem is solved differently, for the function attribs with a primitive workaround.

Namespace lists

There are special lists for some namespaces:

  • Filelist - for items of the namespace 6 (File:)
  • Userlist - for items of the namespace 2 (User:)
  • Linklist - for items of the namespace 0 (main) — not yet
  • Templist - for items of the namespace 10 (Template:) — not yet
  • Cat-list - for items of the namespace 14 (Category:) — not yet
  • Mod-list - for items of the namespace 828 (Module:) — not yet

Filelist

File list templates

The three templates {{Ifim}}, {{Ifim1}} and {{Ifimt}} avoid to display the file itself as an |other version=.

There are currently five templates creating file lists with the function:filelist

  1. {{Other versions}} a vertical list (without initial)
  2. {{Filelist}} a horizontal list (without initial)
  3. {{Derivative versions}} a vertical list
  4. {{Derived from}} a vertical list
  5. {{File}} a horizontal list

All these templates are autoreferencing, and the support also the |gallery= option.

Other file lists

The function:svglang creates a list of just file_names and language_codes, for:

  1. {{Svg lang}}
  2. {{Lang gallery}}
  3. {{Multilingual SVG diagrams}}

Much more is created by the function:ownbased for the template:

  1. {{Own based}}

which will handle in the majority of cases just one file, but can serve an unlimited number.

This list can be controlled by the user to be displayed either horizontally or vertically.

  1. {{Attribs}}

The template works, whether there is a user assigned for a file, or one filename follows another.

  1. {{Emoji}}

The template displays a line of SVG emojis; Unicode characters/emojis can be displayed with the function emodis or emotab.

Variable iterations

A very simple iteration, usable for everything, is performed by function:iterate

used by e.g. {{Image extracted}}, {{Image translated}},

More usable is the function:iteration for transclusions of a template with:

  • a number of positional parameters which can change at each transclusion, and
  • a number of positional parameters which are the same for all transclusions.

See template {{Iterate}}. The function:loop is used by {{Repeat}}.

samefile function

When the name of one of the files is the same as the first one, it is enough to reference it with its extension after a dot, e.g. just ".jpeg". The most standard extensions can be abbreviated, e.g. ".j" will be sufficient for ".jpg" (only for lower case!).

The function incrhx increments a hexadecimal number (parm 1) by a decimal number (parm 2).

Valid numbers are assumed, no checking occurs!

When parameter 2 is preceded by a minus, the first number is decremented by the second one.

Userlist

  • Userlist: {{Userlist}} with many formatting parameters

Templist

Other iterations

  • Langlist:
  • Colorbox: {{ColorString}} and other color box templates
  • Legends : {{Legtab}} combines single {{Legend}}s
  • Tleparms: the metatemplate {{Tle}} displays the parameter usage of a template
  • Navigate: {{Navcat}} navigates through color categories
  • Showname: {{Showname}} helper template, for parameter tests
  • Emodis 0: {{Emoji}} displays (one or many) Unicode characters
The function emodis displays Unicode characters / emojis; it can care for newlines, while
The function emotab displays a table of files / emojis.
--  This module contains the functions for different templates, solving their iteration problems.


local p = {}

-- for Template:Ifim 
function p.ifim1 (frame) 
	local args = mw.getCurrentFrame(): getParent().args;
	local otab = {};
	local p1 = args.p1 or "";
	local p2 = args.p2 or "";
	local p3 = args.p3 or "";
	local p4 = args.p4 or "";
	local p5 = args.p5 or "";
	local fn = args.fn or "";
	for _, v in ipairs(args) do
		table.insert(otab, frame:expandTemplate{ title = "Ifim1", args = { v, p1, p2, p3, p4, p5, n= fn } });
	end
	return  table.concat (otab)
end

-- for Template:Ifimc 
function p.ifim2 (frame) 
	local args = mw.getCurrentFrame(): getParent().args;
	local otab = {};
	local p1 = args.p1 or "";
	local p2 = args.p2 or "";
	local p3 = args.p3 or "";
	local p4 = args.p4 or "";
	for _, v in ipairs(args) do
		table.insert(otab, frame:expandTemplate{ title = "Ifim2", args = { v, p1, p2, p3, p4, v } });
	end
	return  table.concat (otab)
end

-- for Template:Ifimt (param pairs)
function p.ifimt (frame) 
	local args = mw.getCurrentFrame(): getParent().args;
	local otab = {};
	local v1 = "";
	local p1 = args.p1 or "";
	local p2 = args.p2 or "";
	local p3 = args.p3 or "";
	local p4 = args.p4 or "";
	for _, v in ipairs(args) do
		if v1 == "" then   
			v1 = mw.text.trim( v );
		else
			table.insert(otab, frame:expandTemplate{ title = "Ifim2", args = { v1, p1, p2, p3, p4, v } });
			v1 = "";
		end
	end
	if v1 ~= "" then
		table.insert(otab, frame:expandTemplate{ title = "Ifim2", args = { v1, p1, p2, p3, p4, "" } });	-- last elem		
	end
	return  table.concat (otab)
end

-------------------------------------------------------

-- for Template:SVG lang
function p.svglang (frame) 
	local args = mw.getCurrentFrame(): getParent().args;
	local otab = {};
	local file = args.file or "";
	for _, v in ipairs(args) do
		table.insert(otab, frame:expandTemplate{ title = "Source thumb", args = { file, mw.text.trim(v) } });
	end
	return  table.concat (otab)
end


-- horizontal file list for:  Template:Filelist;  Template:File 
--   vertical file list for:  Template:Other versions;  Template:Derived from,  Template:Derivative versions 
function p.filelist (frame) 
--  local locp = frame     -- 1 local parm
	local dirp = frame.args-- global parms
	local args = mw.getCurrentFrame(): getParent().args;
	local ctab = {};
	local ntab = {};
	local dtab = {};
	local otab = {};
	local qtab = {};
	local rtab = {};
	local vtab = {};
	local wtab = {};
	local itab = {};
	local ptab = {};
	local btab = {};
	local mnum = 0; 
	local dir  = dirp[1] or "none"
	local nam  = args.n             or args.name    or ""; 
	local dis  = args.d or args.dis or args.display or ""; 
	local opt  = args.o or args.opt or args.option  or "";
	local pr4  = args.par4 or "";
	local pr5  = args.par5 or "";
	local pr6  = args.par6 or "";
	local wik  = args.w or args.wiki or args.sisterproject or "";
	local int  = args.i or args.int or args.ind or args.inter or "";
	local by0  = args.by   or "";
	local prf  = args.p or args.prf or args.prefix  or ""; -- 'list' item {{Comma}}
	if dir == "vert" and prf == "" then
		prf  = "<br><span style=color:#69F>✦ </span>";     -- 'list' item
	end

	for i, v in ipairs(args) do
		ntab [i] = args ["n" .. tostring( i )] or args ["l" .. tostring( i )] or "-";
		dtab [i] = args ["d" .. tostring( i )] or "-";
		otab [i] = args ["o" .. tostring( i )] or "-";
		qtab [i] = args ["q" .. tostring( i )] or "-";
		rtab [i] = args ["r" .. tostring( i )] or "-";
		vtab [i] = args ["?" .. tostring( i )] or "-";
		wtab [i] = args ["w" .. tostring( i )] or "-";
		itab [i] = args ["i" .. tostring( i )] or "-";
		ptab [i] = args ["p" .. tostring( i )] or "-";
		btab [i] = args ["b" .. tostring( i )] or args ["by" .. tostring( i )] or "-";
		mnum  = i;
	end

	for i, v in ipairs(args) do
		local nm = nam;  if ntab[i] ~= "-" then  nm = ntab[i] end
		local ds = dis;  if dtab[i] ~= "-" then  ds = dtab[i] end
		local op = opt;  if otab[i] ~= "-" then  op = otab[i] end
		local p4 = pr4;  if qtab[i] ~= "-" then  p4 = qtab[i] end
		local p5 = pr5;  if rtab[i] ~= "-" then  p5 = rtab[i] end
		local p6 = pr6;  if vtab[i] ~= "-" then  p6 = vtab[i] end
		local pw = wik;  if wtab[i] ~= "-" then  pw = wtab[i] end
		local pi = int;  if itab[i] ~= "-" then  pi = itab[i] end
		local pf = prf;  if ptab[i] ~= "-" then  pf = ptab[i] end
		local by = by0;  if btab[i] ~= "-" then  by = btab[i] end
		local vv = v;
		if vv == ""  and i > 1 then -- and ps .. pt ~= ""
			vv = args [1];
		end
		if vv ~= ""  then 
			if dir == "hori" then
				pf = "";
				if i == 1 then 
					pf = "&#32;"
				elseif i == mnum then
					table.insert(ctab, frame:expandTemplate{ title = "Conj-and" });
				else
					table.insert(ctab, frame:expandTemplate{ title = "Comma" });
				end
			else				--	elseif dir == "vert" then
				if i == 1 then 
					pf = "&#32;"
				end
			end
			table.insert(ctab, frame:expandTemplate{ title = "F", args = {vv, nm, ds, op, p4, p5, p6, l= pw, lang= pi, p= pf, by= by} });
		end
	end
 	return  table.concat (ctab)
end


-- for Template:Attribs  (param pairs)
function p.attribs (frame) 
	local args = mw.getCurrentFrame(): getParent().args;
	local otab = {};
	local ftab = {};
	local rtab = {}
	local rnum = 0;
	local tp   = args.type or "SVG";  -- needs check
	local un   = args.by or args.U or args.u or "";
	local tt   = args.t or "";
	local f    = args.f or tt;
	local vx   = "";
	local ux   = "";
	local ft   = "";
	for i, v in ipairs(args) do
		ftab [i] = args ["f" .. tostring( i )] or ".";
--                                   table.maxnum (args) does not work
--                                   therefore the "rtab" workaround
		table.insert(rtab, args[i]);
		rnum = i
	end
--	                                 plus one item when odd number
	if  rnum % 2 ==  1 then
 		table.insert( rtab, "" ); -- empty user name, to get a pair
	end
--	
	for i, v in ipairs(rtab) do
		if vx == "" then     -- the 2nd 'v' can be empty
			vx = mw.text.trim( v );
		else
			ux = mw.text.trim( v );
			if ux == "" then ux = un end; -- does not work otherwise (?)
			if ftab[i/2] == "." then 
				ft = f;
			else
				ft = ftab[i/2]  
			end
			table.insert(otab, frame:expandTemplate{ title = "Attrib", args = {vx, ux, "-", tp, "", ux, tt, ft } });
			vx = "";
		end
	end
	return  table.concat (otab)
end

-- for Template:Legend-table  (param pairs)
function p.legendt (frame) 
	local args = mw.getCurrentFrame(): getParent().args;
	local otab = {};
	local lg = args.lang or "";
	local v1 = ""
--                                    same problem when not a last pair
	for _, v in ipairs(args) do
		if v1 == "" then   
			v1 =  v;
		else
			table.insert(otab, frame:expandTemplate{ title = "Legend", args = { lang = lg, v1, v } });
			v1 = "";
		end	
	end
	if v1 ~= "" then
		table.insert(otab, frame:expandTemplate{ title = "Legend", args = { lang = lg, v1, "" } });	-- last elem
	end
	return  table.concat (otab)
end

-- for Template:ColorString (Igen/cbox   -   param pairs)
function p.colorbox (frame) 
	local args = mw.getCurrentFrame(): getParent().args;
	local otab = {};
	local lg = args.lang or "{{PAGELANGUAGE}}"; --
	local v1 = ""
	local p2 = ""
	for i, v in ipairs(args) do
		if i <= 2 then 
			p2 = v; 
		else	
			if v1 == "" then   
				v1 = mw.text.trim( v );
			else
				table.insert(otab, frame:expandTemplate{ title = "Igen/cbox", args = { v1, v, p2, lang = lg } });
				v1 = "";
			end
		end
	end
	if v1 ~= "" then
		table.insert(otab, frame:expandTemplate{ title = "Igen/cbox", args = { v1, "", p2, lang = lg } });	-- last elem		
	end
	return  table.concat (otab)
end


return p;
Cookies help us deliver our services. By using our services, you agree to our use of cookies.