Modul:etymtree

Vun Wiktionary
Dissen Vörlaag hef dokumentatsie neudig.
Maak de dokumentatsie veur dissen modul ovver t doel en gebroek op de dokumentatsieziede.

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
                local idx = 1
		branch_term = mw.title.getCurrentTitle().subpageText
                while idx do
		   idx = branch_term:find( "/", idx) 
                   if idx then
                      branch_term = branch_term:sub(idx + 1)
                      idx = 1
                   end
	        end
	

		if branch_lang:getType() == "reconstructed" or (branch_lang:getType() ~= "appendix-constructed" and mw.title.getCurrentTitle().nsText == "Anhang") then
			branch_term = "*" .. branch_term
		end
	end
	
	root_lang = root_lang or branch_lang
	root_term = root_term or branch_term
	
	local tree_page = "Vörlaag: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("Boom nich funnen. Begrepp:" .. tree_page .. branch_lang:getCode() .. branch_term), -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 == "Anhang") then
			branch_term = "*" .. branch_term
		end
	end

	root_lang = root_lang or branch_lang
	root_term = root_term or branch_term
	
	local tree_page = "Vörlaag: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") .. "{{ännern|" .. tree_page .. "}}"
end

function export.main(frame)
	local args = frame:getParent().args
	
	local branch_lang = args[1] or (mw.title.getCurrentTitle().nsText == "Vörlaag" and "osx") or nil
	local root_lang = args[2] or (mw.title.getCurrentTitle().nsText == "Vörlaag" and "ine-pro") or branch_lang  or nil
	local root_term = args[3] or (mw.title.getCurrentTitle().nsText == "Vörlaag" and "*wódr̥") or nil
	local branch_term = args["branch_term"] or (mw.title.getCurrentTitle().nsText == "Vörlaag" and "watar") or nil
	
	branch_lang = require("Modul: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("Modul: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 == "Vörlaag" and "osx") or nil
	local root_lang = args[2] or (mw.title.getCurrentTitle().nsText == "Vörlaag" and "ine-pro") or branch_lang or nil
	local root_term = args[3] or (mw.title.getCurrentTitle().nsText == "Vörlaag" and "*wódr̥̥̥") or nil
	local branch_term = args["branch_term"] or (mw.title.getCurrentTitle().nsText == "Vörlaag" and "watar") 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