모듈:Temflake

Gnlow (토론 | 기여)님의 2022년 8월 13일 (토) 02:47 판 (WIP)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

이 모듈에 대한 설명문서는 모듈:Temflake/설명문서에서 만들 수 있습니다

p = {}

function shallow_copy(t)
  local t2 = {}
  for k,v in pairs(t) do
    t2[k] = v
  end
  return t2
end

function p.parse(str)
	local ctx = {}
	ctx.indent = -2
	ctx.depth = -2
	ctx.path = {}
	ctx.obj = {}
	result = {}
	for line in str:gmatch("[^\r\n]+") do
		indent, bar, text, colon = line:match("^(%s*)(-?)%s*([^:]+)(:?)%s*$")
		
		if ctx.indent > indent:len() then
			if indent:len() < ctx.depth then
				result = {[ctx.path[#ctx.path]] = result}
				ctx.depth = ctx.depth - 2
			else
				result[ctx.path[#ctx.path]] = ctx.obj
				ctx.depth = indent:len()
				ctx.obj = {}
			end
			table.remove(ctx.path)
			
		end
		
		if colon ~= "" then
			table.insert(ctx.path, text)
		elseif bar ~= "" then
			table.insert(ctx.obj, text)
		end
		
		ctx.indent = indent:len()
		mw.logObject(ctx)
	end
	
	for i = #ctx.path, 1, -1 do
		result[ctx.path[i]] = ctx.obj
		ctx.obj = {}
		table.remove(ctx.path)
		mw.logObject(ctx)
	end
	
	return result
end

return p