Difference between revisions of "Module:Foreach"
Jump to navigation
Jump to search
Rixithechao (talk | contribs) m |
Rixithechao (talk | contribs) (okay awesome it works, let's make the delimiter more human-readable) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
-- Modified from https://www.mediawiki.org/wiki/Module:Foreach | -- Modified from https://www.mediawiki.org/wiki/Module:Foreach | ||
-- adds a third argument that toggles whether the for loop parses the table from a single argument string | |||
local p = {} | local p = {} | ||
local function parseArgFour(arg) | |||
local tbl = {} | |||
for match in (arg..", "):gmatch("(.-)"..", ") do | |||
table.insert(tbl, match); | |||
end | |||
return tbl | |||
end | |||
function p.w(frame) | function p.w(frame) | ||
Line 9: | Line 20: | ||
local tplname = pframe.args[1] | local tplname = pframe.args[1] | ||
local delim = pframe.args[2] | local delim = pframe.args[2] | ||
local listType = pframe.args[3] | |||
local list = pframe.args | |||
local listStartN = 3 | |||
if listType == "arg" then | |||
list = parseArgFour(pframe.args[4]) | |||
listStartN = 0 | |||
end | |||
r = '' | r = '' | ||
for n,v in ipairs( | for n,v in ipairs(list) do | ||
if n > | if n > listStartN then | ||
r = r .. delim | r = r .. delim | ||
end | end | ||
if n > | if n > listStartN then | ||
r = r .. frame:expandTemplate{title = tplname, args = {i=n- | r = r .. frame:expandTemplate{title = tplname, args = {i=n-listStartN, [1]=v}} | ||
end | end | ||
end | end | ||
Line 25: | Line 44: | ||
-- to be called from a module | -- to be called from a module | ||
-- at the top: p={foreach = require "Module:foreach"} | -- at the top: p={foreach = require "Module:foreach"} | ||
-- example: p.foreach.m({tplname,', ',item1,item3,item3},frame) | -- example: p.foreach.m({tplname,', ','',item1,item3,item3},frame) | ||
r = '' | r = '' | ||
tplname=t[1] | tplname=t[1] | ||
delim=t[2] | delim=t[2] | ||
for n,v in ipairs( | listType=t[3] | ||
if n > | |||
list = t | |||
listStartN = 3 | |||
if listType == "arg" then | |||
list = parseArgFour(t[4]) | |||
listStartN = 0 | |||
end | |||
for n,v in ipairs(list) do | |||
if n > listStartN then | |||
r = r .. delim | |||
end | |||
if n > listStartN then | |||
r = r .. frame:expandTemplate{title = tplname, args = {i=n-listStartN, [1]=v}} | |||
end | |||
end | end | ||
return r | return r |
Latest revision as of 22:03, 16 June 2021
Documentation for this module may be created at Module:Foreach/doc
-- Modified from https://www.mediawiki.org/wiki/Module:Foreach -- adds a third argument that toggles whether the for loop parses the table from a single argument string local p = {} local function parseArgFour(arg) local tbl = {} for match in (arg..", "):gmatch("(.-)"..", ") do table.insert(tbl, match); end return tbl end function p.w(frame) -- called from Template:Fe local pframe = frame:getParent() local tplname = pframe.args[1] local delim = pframe.args[2] local listType = pframe.args[3] local list = pframe.args local listStartN = 3 if listType == "arg" then list = parseArgFour(pframe.args[4]) listStartN = 0 end r = '' for n,v in ipairs(list) do if n > listStartN then r = r .. delim end if n > listStartN then r = r .. frame:expandTemplate{title = tplname, args = {i=n-listStartN, [1]=v}} end end return r end function p.m(t,frame) -- to be called from a module -- at the top: p={foreach = require "Module:foreach"} -- example: p.foreach.m({tplname,', ','',item1,item3,item3},frame) r = '' tplname=t[1] delim=t[2] listType=t[3] list = t listStartN = 3 if listType == "arg" then list = parseArgFour(t[4]) listStartN = 0 end for n,v in ipairs(list) do if n > listStartN then r = r .. delim end if n > listStartN then r = r .. frame:expandTemplate{title = tplname, args = {i=n-listStartN, [1]=v}} end end return r end return p