Documentation for this module may be created at Մոդուլ:compound/templates/doc

local m_compound = require("Module:compound")

local export = {}

local function get_part(args, i, offset)
	offset = offset or 0
	
	local term = args[i + offset]; if term == "" then term = nil end
	local alt = args["alt" .. i]; if alt == "" then alt = nil end
	local id = args["id" .. i]; if id == "" then id = nil end
	local lang = args["lang" .. i]; if lang == "" then lang = nil end
	local sc = args["sc" .. i]; if sc == "" then sc = nil end
	
	local tr = args["tr" .. i]; if tr == "" then tr = nil end
	local gloss = args["t" .. i] or args["gloss" .. i]; if gloss == "" then gloss = nil end
	local pos = args["pos" .. i]; if pos == "" then pos = nil end
	local lit = args["lit" .. i]; if lit == "" then lit = nil end
	
	lang = lang and (require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")) or nil
		sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	if not term and not alt and not tr then
		return nil
	-- Embedded wikilinks should not be allowed in parts of a compound;
	-- this would just confuse things a lot.
	elseif term and term:find("[[", nil, true) then
		error("Embedded wikilinks should not be used in the parts of a compound.")
	else
		return {term = term, alt = alt, id = id, lang = lang, sc = sc, annotations = {tr = tr, gloss = gloss, pos = pos, lit = lit}}
	end
end

local function get_parts(args, i, offset)
	local parts = {}
	
	while true do
		local part = get_part(args, i, offset)
		
		if not part then
			break
		end
		
		table.insert(parts, part)
		
		i = i + 1
	end
	
	return parts
end

function export.affix(frame)
	local args = frame:getParent().args
	
	local lang = args[1]; if lang == "" then lang = nil end
	local sc = args["sc"] or ""; if sc == "" then sc = nil end
	local pos = args["pos"]; if pos == "" then pos = nil end
	local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
	local nocat = args["nocat"]; if not nocat or nocat == "" then nocat = false else nocat = true end
	
	if not lang then
		if mw.title.getCurrentTitle().nsText == "Կաղապար" then
			lang = "und"
		else
			error("Language code has not been specified. Please pass parameter 1 to the template.")
		end
	end
	
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local parts = get_parts(args, 1, 1)
	
	-- Just to make sure someone didn't use the template in a silly way
	if #parts < 2 then
		if mw.title.getCurrentTitle().nsText == "Կաղապար" then
			parts = { {term = "նախածանց-"}, {term = "հիմք"}, {term = "-վերջածանց"} }
		else
			error("You must provide at least two parts.")
		end
	end
	
	return m_compound.show_affixes(lang, sc, parts, pos, sort_key, nocat)
end

function export.compound(frame)
	local args = frame:getParent().args
	
	local lang = args["lang"]; if lang == "" then lang = nil end
	local sc = args["sc"] or ""; if sc == "" then sc = nil end
	local pos = args["pos"]; if pos == "" then pos = nil end
	local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
	local nocat = args["nocat"]; if not nocat or nocat == "" then nocat = false else nocat = true end
	
	if not lang then
		lang = "en"
	end
	
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local parts = get_parts(args, 1)
	
	-- Just to make sure someone didn't use the template in a silly way
	if #parts < 2 then
		if mw.title.getCurrentTitle().nsText == "Կաղապար" then
			parts = { {term = "առաջին"}, {term = "երկրորդ"} }
		else
			error("You must provide at least two parts of a compound.")
		end
	end
	
	return m_compound.show_compound(lang, sc, parts, pos, sort_key, nocat)
end

function export.interfix_compound(frame)
	local args = frame:getParent().args
	
	local lang = args[1]; if lang == "" then lang = nil end
	local sc = args["sc"] or ""; if sc == "" then sc = nil end
	local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
	local nocat = args["nocat"]; if not nocat or nocat == "" then nocat = false else nocat = true end
	local pos = args["pos"]; if pos == "" then pos = nil end
	
	if not lang then
		if mw.title.getCurrentTitle().nsText == "Կաղապար" then
			lang = "und"
		else
			error("Language code has not been specified. Please pass parameter 1 to the template.")
		end
	end
	
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local parts = get_parts(args, 1, 1)
	local base1 = parts[1]
	local interfix = parts[2]
	local base2 = parts[3]
	
	-- Just to make sure someone didn't use the template in a silly way
	if not (base1 and interfix and base1) then
		if mw.title.getCurrentTitle().nsText == "Կաղապար" then
			base1 = {term = "հիմք1"}
			interfix = {term = "միջածանց"}
			base2 = {term = "հիմք2"}
		else
			error("You must provide a base term, an interfix and a second base term.")
		end
	end
	
	return m_compound.show_interfix_compound(lang, sc, base1, interfix, base2, pos, sort_key, nocat)
end

function export.circumfix(frame)
	local args = frame:getParent().args
	
	local lang = args["lang"]; if lang == "" then lang = nil end
	local sc = args["sc"] or ""; if sc == "" then sc = nil end
	local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
	local nocat = args["nocat"]; if not nocat or nocat == "" then nocat = false else nocat = true end
	local pos = args["pos"]; if pos == "" then pos = nil end
	
	if not lang then
		lang = "en"
	end
	
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local parts = get_parts(args, 1)
	local prefix = parts[1]
	local base = parts[2]
	local suffix = parts[3]
	
	-- Just to make sure someone didn't use the template in a silly way
	if not (prefix and base and suffix) then
		if mw.title.getCurrentTitle().nsText == "Կաղապար" then
			prefix = {term = "շուրջըածանց", alt = "նախածանց"}
			base = {term = "base"}
			suffix = {term = "շուրջըածանց", alt = "վերջածանց"}
		else
			error("You must specify a prefix part, a base term and a suffix part.")
		end
	end
	
	return m_compound.show_circumfix(lang, sc, prefix, base, suffix, pos, sort_key, nocat)
end

function export.confix(frame)
	local args = frame:getParent().args
	
	local lang = args["lang"]; if lang == "" then lang = nil end
	local sc = args["sc"] or ""; if sc == "" then sc = nil end
	local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
	local nocat = args["nocat"]; if not nocat or nocat == "" then nocat = false else nocat = true end
	local pos = args["pos"]; if pos == "" then pos = nil end
	
	if not lang then
		lang = "en"
	end
	
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local parts = get_parts(args, 1)
	local prefix = parts[1]
	local base = #parts >= 3 and parts[2] or nil
	local suffix = #parts >= 3 and parts[3] or parts[2]
	
	-- Just to make sure someone didn't use the template in a silly way
	if not (prefix and suffix) then
		if mw.title.getCurrentTitle().nsText == "Կաղապար" then
			prefix = {term = "նախածանց"}
			suffix = {term = "վերջածանց"}
		else
			error("You must specify a prefix part, an optional base term and a suffix part.")
		end
	end
	
	return m_compound.show_confix(lang, sc, prefix, base, suffix, pos, sort_key, nocat)
end

function export.infix(frame)
	local args = frame:getParent().args
	
	local lang = args["lang"]; if lang == "" then lang = nil end
	local sc = args["sc"] or ""; if sc == "" then sc = nil end
	local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
	local nocat = args["nocat"]; if not nocat or nocat == "" then nocat = false else nocat = true end
	local pos = args["pos"]; if pos == "" then pos = nil end
	
	if not lang then
		lang = "en"
	end
	
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local parts = get_parts(args, 1)
	local base = parts[1]
	local infix = parts[2]
	
	-- Just to make sure someone didn't use the template in a silly way
	if not (base and infix) then
		if mw.title.getCurrentTitle().nsText == "Կաղապար" then
			base = {term = "հիմք"}
			infix = {term = "մեջածանց"}
		else
			error("You must provide a base term and an infix.")
		end
	end
	
	return m_compound.show_infix(lang, sc, base, infix, pos, sort_key, nocat)
end

function export.prefix(frame)
	local args = frame:getParent().args
	
	local lang = args["lang"]; if lang == "" then lang = nil end
	local sc = args["sc"] or ""; if sc == "" then sc = nil end
	local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
	local nocat = args["nocat"]; if not nocat or nocat == "" then nocat = false else nocat = true end
	local pos = args["pos"]; if pos == "" then pos = nil end
	
	if not lang then
		lang = "en"
	end
	
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local prefixes = get_parts(args, 1)
	local base = nil
	
	if #prefixes >= 2 then
		base = prefixes[#prefixes]
		prefixes[#prefixes] = nil
	end
	
	-- Just to make sure someone didn't use the template in a silly way
	if #prefixes == 0 then
		if mw.title.getCurrentTitle().nsText == "Կաղապար" then
			base = {term = "base"}
			prefixes = { {term = "prefix"} }
		else
			error("You must provide at least one prefix.")
		end
	end
	
	return m_compound.show_prefixes(lang, sc, prefixes, base, pos, sort_key, nocat)
end

function export.suffix(frame)
	local args = frame:getParent().args
	
	local lang = args["lang"]; if lang == "" then lang = nil end
	local sc = args["sc"] or ""; if sc == "" then sc = nil end
	local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
	local nocat = args["nocat"]; if not nocat or nocat == "" then nocat = false else nocat = true end
	local pos = args["pos"]; if pos == "" then pos = nil end
	
	if not lang then
		lang = "en"
	end
	
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	local base = get_part(args, 1)
	local suffixes = get_parts(args, 2)
	
	-- Just to make sure someone didn't use the template in a silly way
	if #suffixes == 0 then
		if mw.title.getCurrentTitle().nsText == "Կաղապար" then
			base = {term = "հիմք"}
			suffixes = { {term = "վերջածանց"} }
		else
			error("You must provide at least one suffix.")
		end
	end
	
	return m_compound.show_suffixes(lang, sc, base, suffixes, pos, sort_key, nocat)
end

function export.derivsee(frame)
	local args = frame:getParent().args
	
	local derivtype = frame.args["derivtype"]
	local lang = args[1] or (mw.title.getCurrentTitle().nsText == "Կաղապար" and "und") or error("Language code has not been specified. Please pass parameter 1 to the template.")
	local term = args[2] or args["head"]; if term == "" then term = nil end
	local sc = args["sc"]; if sc == "" then sc = nil end
	local pos = args["pos"]; if pos == "" then pos = nil end
	lang = require("Module:languages").getByCode(lang)
	sc = (sc and require("Module:scripts").getByCode(sc) or nil)
	
	pos = pos or "word"
	
	-- Pluralize the part of speech name
	if pos:find("[sx]$") then
		pos = pos .. "es"
	else
		pos = pos .. "s"
	end
	
	if not term then
		if lang:getType() == "reconstructed" then
			term = "*" .. mw.title.getCurrentTitle().subpageText
		elseif lang:getType() == "appendix-constructed" then
			term = mw.title.getCurrentTitle().subpageText
		elseif mw.title.getCurrentTitle().nsText == "Appendix" then
			term = "*" .. mw.title.getCurrentTitle().subpageText
		else
			term = mw.title.getCurrentTitle().subpageText
		end
	end
	
	local category = nil
	
	if derivtype == "compound" then
		category = lang:getCanonicalName() .. " compounds with " .. term
	else
		category = lang:getCanonicalName() .. " " .. pos .. " " .. derivtype .. "ed with " .. term
	end
	
	return frame:expandTemplate{title = "deriv", args = {category, sc = sc and sc:getCode() or nil}}
end

return export