Civilization Wiki
Advertisement
Civilization Wiki

Documentation for this module may be created at Module:TemplateUtils/doc

local p = {}

-------------------------------
------- ExplodeForeach --------
-------------------------------
-- Arguments are as follow :
--1 = template name
--2 = a string to process
--3 = that string's separators
--4 = additional unnamed template param
--delim = the delimiter between the resulting templates 
function p.ExplodeForeach(frame)
	local args = frame:getParent().args
	if args[2] == nil then return "" end

	local template = args[1]
	local inputs = mw.text.split( args[2], args[3])
	local delimiter = args.delim
	local param = args[4]
	
	for i = 1, #inputs do
		if not (inputs[i] == "") then -- No empty strings will be processed
			inputs[i] = string.gsub(inputs[i], "^%s", "") -- Remove leading whitespaces
			local templateArgs;
			if param == nil then
				templateArgs = { inputs[i] }
			else 
				templateArgs = { inputs[i], param }
			end
			inputs[i] = frame:expandTemplate{ title = template, args = templateArgs }
		end
	end
	
	return table.concat( inputs, delimiter)
end

-----------------------------------
------- ExplodeIthElement ---------
---- Similar to ExplodeForeach ----
-- but returns only i-th element --
-----------------------------------
-- Arguments are as follow :
--1 = index
--2 = template name
--3 = a string to process
--4 = that string's separators
function p.ExplodeIthElement(frame)
	local args = frame:getParent().args
	if args[3] == nil then return "" end

    local index = tonumber(args[1])
	local template = args[2]
	local inputs = mw.text.split(args[3], args[4])

	if index >= 1 and index <= #inputs then
		inputs[index] = string.gsub(inputs[index], "^%s", "") -- Remove leading whitespaces
		inputs[index] = frame:expandTemplate{ title = template, args = { inputs[index] } }
	end

	return inputs[index]
end

function p.Link(s, suffix)
	return "[["..s.." ("..suffix..")|"..s.."]]"
end
 
function p.RecreateTable(inputTable)
	local outputTable = {}
	for k,v in pairs(inputTable) do
		outputTable[k] = v
	end
	return outputTable
end
 
function p.LinkAll(t, suffix)
	t = p.RecreateTable(t)
	for i = 1, #t do
		t[i] = p.Link(t[i], suffix)
	end
	return table.concat(t,"<br/>")
end
 
function p.PreProcess(frame,s)
	return (frame and frame.preprocess and frame:preprocess(s)) or s
end

function p.UnpackArgs(args)
	return unpack(p.RecreateTable(args))
end

function p.Error(text)
	return string.format("<div class='error'>Error: %s</div>",text)
end

function p.CleanPageName(frame)
    local str = string.gsub(frame.args[1], " %(.*","")
    return str
end
 
return p