Difference between revisions of "Module:ApplyPattern"

From Holdfast: Nations At War
Jump to navigation Jump to search
m
Line 19: Line 19:
 
 
 
for key,entry in pairs(split(list,sep)) do
 
for key,entry in pairs(split(list,sep)) do
result[#result+1] = "{{" .. string.gsub(pattern, '%%pattern%%', trim(entry)).. "}}"
+
result[#result+1] = string.gsub(pattern, '%%pattern%%', trim(entry))
 
end
 
end
 
 
 
     return table.concat(result,"")  
 
     return table.concat(result,"")  
 
end
 
end
 +
 +
function p.applyTemplate(frame)
 +
local list = frame.args[1]
 +
local tempalte = frame.args[2]
 +
local result = {}
 +
local sep = ','
 +
 +
for key,entry in pairs(split(list,sep)) do
 +
result[#result+1] = string.gsub(pattern, '%%pattern%%', trim(entry))
 +
end
 +
 +
    return table.concat(result,"")
 +
end
 +
 
return p
 
return p

Revision as of 16:58, 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 trim(s)
   return (s:gsub("^%s*(.-)%s*$", "%1"))
end

function p.apply(frame)
	local list = frame.args[1]
	local pattern = frame.args[2]
	local result = {}
	local sep = ','
	
	for key,entry in pairs(split(list,sep)) do
		result[#result+1] = string.gsub(pattern, '%%pattern%%', trim(entry))
	end
	
    return table.concat(result,"") 
end

function p.applyTemplate(frame)
	local list = frame.args[1]
	local tempalte = frame.args[2]
	local result = {}
	local sep = ','
	
	for key,entry in pairs(split(list,sep)) do
		result[#result+1] = string.gsub(pattern, '%%pattern%%', trim(entry))
	end
	
    return table.concat(result,"") 
end

return p