Difference between revisions of "Module:ApplyPattern"
Jump to navigation
Jump to search
m |
m |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
+ | |||
+ | function split(instr,sep) | ||
+ | local sep, fields = sep or ":", {} | ||
+ | local pattern = string.format("([^%s]+)", sep) | ||
+ | string.gsub(instr, pattern, function(c) fields[#fields+1] = c end) | ||
+ | return fields | ||
+ | end | ||
+ | |||
function p.apply(frame) | function p.apply(frame) | ||
local list = frame.args[1] | local list = frame.args[1] | ||
Line 6: | Line 14: | ||
local sep = ',' | local sep = ',' | ||
− | for entry in | + | for entry in split(list,sep) do |
result = result .. string.gsub(pattern, '%%pattern%%',entry) | result = result .. string.gsub(pattern, '%%pattern%%',entry) | ||
end | end |
Revision as of 14:55, 17 August 2019
Custom module to apply either some wiki markup or a template to each item in a comma delimited list. Look at Template:Class for examples.
local p = {} function split(instr,sep) local sep, fields = sep or ":", {} local pattern = string.format("([^%s]+)", sep) string.gsub(instr, pattern, function(c) fields[#fields+1] = c end) return fields end function p.apply(frame) local list = frame.args[1] local pattern = frame.args[2] local result = "" local sep = ',' for entry in split(list,sep) do result = result .. string.gsub(pattern, '%%pattern%%',entry) end return result end return p