Module:etymtree

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
Text-x-generic with pencil.svg This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

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 = 'Appendix:'..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 (mw.title.getCurrentTitle().nsText == "Template" and "ang") or nil
	local root_lang = args[2] or (mw.title.getCurrentTitle().nsText == "Template" and "ine-pro") or nil
	local root_term = args[3] or (mw.title.getCurrentTitle().nsText == "Template" and "*wódr̥") or nil
	local branch_term = args["branch_term"] or (mw.title.getCurrentTitle().nsText == "Template" and "wæter") or nil
	
	branch_lang = require("Module:languages").getByCode(branch_lang or error("Parameter 1 (branch language code) has not been specified.")) or error("The language code \"" .. branch_lang .. "\" is not valid.")
	root_lang = root_lang and (require("Module:languages").getByCode(root_lang) or error("The language code \"" .. root_lang .. "\" is not valid.")) 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 (mw.title.getCurrentTitle().nsText == "Template" and "en") or nil
	local root_lang = args[2] or (mw.title.getCurrentTitle().nsText == "Template" and "ine-pro") or nil
	local root_term = args[3] or (mw.title.getCurrentTitle().nsText == "Template" and "*wódr̥") or nil
	local branch_term = args["branch_term"] or (mw.title.getCurrentTitle().nsText == "Template" and "water") or nil
	
	branch_lang = require("Module:languages").getByCode(branch_lang or error("Parameter 1 (branch language code) has not been specified.")) or error("The language code \"" .. branch_lang .. "\" is not valid.")
	root_lang = root_lang and (require("Module:languages").getByCode(root_lang) or error("The language code \"" .. root_lang .. "\" is not valid.")) or nil
	
	local ret = export.etym(branch_lang, branch_term, root_lang, root_term)
	return frame:preprocess(ret)
end

return export