Module:etymtree

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
The following documentation is located at Module:etymtree/documentation. [edit]
Useful links: subpage listtransclusionstestcases
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") 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 search_for = "|" .. branch_lang:getCode() .. "|" .. branch_term
	local branch = tree:sub(tree:find(search_for, 1, true) or error("Branch not found."), -1)
 
	local ind = string.rep("%*", branch:match( "\n%*+"):len() - 2)
	local point = 1
 
	while point do
		point = branch:find( "\n", point + 1)
 
		if not branch:find( "^\n%*" .. ind, point) then
			break
		end
	end
 
	if point then
		branch = branch:sub( 1, point )
	end
 
	branch = branch:gsub( "^[^\n]+\n", "\n"):gsub( "\n" .. ind, "\n")
 
	if not branch:find("\n%*[%*:#]") then
		point = 0
		local g = branch:gmatch("\n%*")
 
		while g() do
			point = point + 1
		end
 
		if point > 1 then
			branch = "{{top2}}" .. branch:gsub("\n%*", function(a) 
				point = point - 2 if point < -1 and point >= -3 then return "\n{{mid2}}" .. a end
			end ) .. "{{bottom}}"
		end
	end
 
	return "<div style=\"float: right;\">{{edit|" .. tree_page .. "}}</div>" .. branch
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