Jump to content

Module:User:Martijn Hoekstra/ForRomaine

From Wikipedia, the free encyclopedia

local p = {}
function p.extract(frame)
	stringy = frame.args[1]
	elem = frame.args[2]
	dosubelem = frame.args[3]
	tab =  p.rec(stringy, { subvalues = {} }, 1)
	if dosubelem then return tab.subvalues[elem] else return tab[elem] end
end

function p.check(frame)
	stringy = frame.args[1]
	return p.innercheck(stringy)
end

function p.innercheck(haystack)
	somecolon = string.find(haystack, ":")
	if not somecolon then return "Error" end
	allowed = { region = 1, scale = 1, subvalues = 1}
	allowed["type"] = 1
	tab = p.rec(haystack, { subvalues = {} }, 1)
	for key,value in pairs(tab) do
		if not allowed[key] then return "Error" end
	end
	return
end

function p.rec(haystack, accum, pos)
	poscol = string.find(haystack, ":", pos)
	key = string.sub(haystack, pos, poscol - 1)
	posunder = string.find(haystack, "_", pos)
	if posunder then
		posbracket = string.find(haystack, "%(", pos)
		if posbracket and posbracket < posunder then
			value = string.sub(haystack, poscol + 1, posbracket - 1)
			subvalue = string.sub(haystack, posbracket + 1, posunder - 2)
			accum[key] = value
			accum.subvalues[key] = subvalue
		else 
			value = string.sub(haystack, poscol + 1, posunder - 1)
			accum[key] = value
		end
		return p.rec(haystack, accum, posunder + 1)
	else
		value = string.sub(haystack, poscol + 1)
		accum[key] = value
		return accum
	end
end

return p