Module:DataPreprocessor

From Conflict of Nations Wiki
Jump to navigation Jump to search

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

--------------------------------------------------------------------------------
-- Preproccesses a string meant to be called with {{PAGENAME}}
-- Expects an argument of the form "Country Name (Scenario Name)""
-- Outputs two arguments in a table: "Country Name" and "Scenario Name"
--------------------------------------------------------------------------------
local returnValue = {}

function returnValue.process(frame)
	local pagename = frame:preprocess(frame.args[1])
	if (frame.args[1] == nil) then
		error("Expected {{PAGENAME}} in frame.args[1]")
		end
	local indexOpenParen = nil
	local indexCloseParen = nil
	local currentIndexOffset = 0
	local currentSubstring = pagename
	local pagenameSplitByCloseParen = mw.text.split(pagename, ")", true)
	if (#pagenameSplitByCloseParen == 1) then
		error("Malformed pagename while looking for scenario name - no closing parentheses found")
		end
	local pagenamePenultimateCloseParenSubstringSplitByOpenParen = mw.text.split(pagenameSplitByCloseParen[#pagenameSplitByCloseParen - 1], "(", true)
	if (#pagenamePenultimateCloseParenSubstringSplitByOpenParen == 1) then
		error("Malformed pagename while looking for scenario name - no opening parentheses found before last closing parentheses (and after the second to last closing parentheses, if it exists)")
		end
	if (#pagenamePenultimateCloseParenSubstringSplitByOpenParen > 2) then
		error("Malformed pagename while looking for scenario name - too many opening parentheses found before last closing parentheses (and after the second to last closing parentheses, if it exists)")
		end
	local source = pagenamePenultimateCloseParenSubstringSplitByOpenParen[2]
	local entry = pagename
	return {source, entry}
end

return returnValue