Module:Etymtree

local export = {}

-- Please add comments to explain what the code does/is for! (August 2013)

function export.getTree(branch_lang, branch_term, root_lang, root_term)
	if not branch_term then
		branch_term = mw.title.getCurrentTitle().subpageText
	
		if branch_lang:getType() == "reconstructed" or (branch_lang:getType() ~= "appendix-constructed" and (mw.title.getCurrentTitle().nsText == "Appendix" or mw.title.getCurrentTitle().nsText == "Reconstruction")) then
			branch_term = "*" .. branch_term
		end
	end
	
	root_lang = root_lang or branch_lang
	root_term = root_term or branch_term
	
	local tree_page = "Template:etymtree/" .. root_lang:getCode() .. "/" .. root_term
	local tree = mw.title.new(tree_page):getContent()
	
	if not tree then
		error("Invalid root")
	end
	
	local search_for = "|" .. branch_lang:getCode() .. "|" .. branch_term
	
	local found = false
	
	local indent_level, skip_level
	
	local result = {}
	
	for line in mw.text.gsplit(tree, "\n") do
		if not found then
			if mw.ustring.find(line, search_for, 1, true) then
				indent_level = mw.ustring.match(line, "^[%*:#]+")
				indent_level = indent_level:len()
				found = true
			end
		else
			local current_level = mw.ustring.match(line, "^[%*:#]+")
			current_level = current_level:len()
			if current_level <= indent_level then
				break
			end
			
			if not skip_level or current_level <= skip_level then
				skip_level = false
				
				local lang,term = mw.ustring.match(line, "|([^|=}]+)|(%*[^|=}]+)")
				
				if not lang then
					lang,term = mw.ustring.match(line, "|(la)|(%*?[^|=}]+)")
				end
				
				if lang and lang == "itc-pro" then
					lang = nil
				end
				
				if lang then
					lang = require("Module:languages").getByCode(lang)
					term = lang:makeEntryName(term)
					lang = lang:getCanonicalName()
					local page_name
					if mw.ustring.match(term, "^%*") then
						page_name = 'Reconstruction:'..lang..'/'..mw.ustring.sub(term,2)
					else
						page_name = term
					end
					
					if mw.title.new(page_name).exists then
						table.insert(result, mw.ustring.sub(line, indent_level+1).." {{see desc}}")
						skip_level = current_level
					else
						table.insert(result, mw.ustring.sub(line, indent_level+1))
					end
				else
					table.insert(result, mw.ustring.sub(line, indent_level+1))
				end
			end
		end
	end
	
	if not found then
		error("Branch not found.")
	end
	
	if #result > 15 and branch_lang:getType() ~= "reconstructed" and not (branch_lang:getType() ~= "appendix-constructed" and mw.title.getCurrentTitle().nsText == "Appendix") then
		local index = math.floor((#result+1)/2) + 1
		local left, right = index, index
		
		while left > 0 and right <= #result do
			if mw.ustring.match(result[left], "^[%*:#][^%*:#]") then
				index = left
				break
			elseif mw.ustring.match(result[right], "^[%*:#][^%*:#]") then
				index = right
				break
			end
			right = right + 1
			left = left - 1
		end
			
		table.insert(result, 1, "{{top2}}")
		table.insert(result, index+1, "{{mid2}}")
		table.insert(result, "{{bottom}}")
	end
	
	return "<div style=\"float: right;\">{{edit|" .. tree_page .. "}}</div>\n" .. table.concat(result,"\n")
end

function export.etym(branch_lang, branch_term, root_lang, root_term)
	if not branch_term then
		branch_term = mw.title.getCurrentTitle().subpageText
	
		if branch_lang:getType() == "reconstructed" or (branch_lang:getType() ~= "appendix-constructed" and mw.title.getCurrentTitle().nsText == "Appendix") then
			branch_term = "*" .. branch_term
		end
	end

	root_lang = root_lang or branch_lang
	root_term = root_term or branch_term
	
	local tree_page = "Template:etymtree/" .. root_lang:getCode() .. "/" .. root_term
	local tree = "\n" .. mw.title.new(tree_page):getContent() .. "\n"
	
	local point = tree:find("|" .. branch_lang:getCode() .. "|" .. branch_term, 1, true) or error("Etymology chain not found.")
	point = tree:find("\n", point, true)
	local branch = tree:sub(1, point)
	
	local function a()
		branch = branch:gsub( "(\n%*+)%**[^\n%*][^\n]+%1([^%*])", "%1%2")
		return branch
	end
	
	while a() ~= a() do
		-- nothing
	end
	
	branch = branch:gsub("\n[^\n]+\n?$","") -- don't display the word itself in the etym
	local chain = mw.text.gsplit(branch, "\n%*+%s*")
	local returnvalue = "."
	
	for b in chain do
		if b:find( ":" ) then
			local c = b:gsub("^[^:]+%:%s*%{%{", "")
				:gsub("^l%|([^%|]+)%|%*", "%1|" .. branch_lang:getCode() .. "}} {{m|%1|*")
				:gsub("^l%|([^%|]+)%|", "%1|" .. branch_lang:getCode() .. "}} {{m|%1|")
			returnvalue = ", from {{etyl|" .. c .. returnvalue
		end
	end
	
	return returnvalue:gsub("^, f", "F") .. "{{edit|" .. tree_page .. "}}"
end

function export.main(frame)
	local args = frame:getParent().args
	
	local branch_lang = args[1] or nil
	local root_lang = args[2] or nil
	local root_term = args[3] or nil
	local branch_term = args["branch_term"] or nil
	
	if mw.title.getCurrentTitle().nsText == "Template" then
		root_lang = "ine-pro"
		root_term = "*wódr̥"
		branch_lang = "ang"
		branch_term = "wæter"
	end
	
	branch_lang = require("Module:languages").getByCode(branch_lang) or require("Module:languages").err(branch_lang, 1, "branch language code")
	root_lang = root_lang and (require("Module:languages").getByCode(root_lang) or require("Module:languages").err(root_lang, 2) ) or nil
	
	local ret = export.getTree(branch_lang, branch_term, root_lang, root_term)
	return frame:preprocess(ret)
end

function export.etymmain(frame)
	local args = frame:getParent().args
	
	local branch_lang = args[1] or nil
	local root_lang = args[2] or nil
	local root_term = args[3] or nil
	local branch_term = args["branch_term"] or nil
	
	if mw.title.getCurrentTitle().nsText == "Template" then
		root_lang = "ine-pro"
		root_term = "*wódr̥"
		branch_lang = "en"
		branch_term = "water"
	end
	
	branch_lang = require("Module:languages").getByCode(branch_lang) or require("Module:languages").err(branch_lang, 1, "branch language code")
	root_lang = root_lang and (require("Module:languages").getByCode(root_lang) or require("Module:languages").err(root_lang, 2)) or nil
	
	local ret = export.etym(branch_lang, branch_term, root_lang, root_term)
	return frame:preprocess(ret)
end

return export