This module is used by the template {{માથું}} to create headwords for entries.


local m_headword = require("Module:માથુંશબ્દ")

local export = {}

function export.head_t(frame)
	local m_utilities = require("Module:ઉપયોગીતા")
	
	local args = frame:getParent().args
	PAGENAME = mw.title.getCurrentTitle().text
	SUBPAGENAME = mw.title.getCurrentTitle().subpageText
	NAMESPACE = mw.title.getCurrentTitle().nsText
	
	-- Get language and script information
	local lang = args[1] or (NAMESPACE == "ઢાંચો" and "und") or error("Language code has not been specified. Please pass parameter 1 to the template.")
	lang = require("Module:ભાષાઓ").getLanguageByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	local sc = args["લિપિ"] or ""; if sc == "" then sc = nil end
	
	-- Gather basic parameters
	local sort_key = args["ક્રમ"]; if sort_key == "" then sort_key = nil end
	local pos = args[2]; if pos == "" then pos = nil end
	local cat = args["શ્રેણી"]; if cat == "" then cat = nil end
	local cat2 = args["શ્રેણી૨"]; if cat2 == "" then cat2 = nil end
	local cat3 = args["શ્રેણી૩"]; if cat3 == "" then cat3 = nil end
	local tr = args["લિપ્ય"]; if tr == "" then tr = nil end
	
	-- Gather headwords
	local heads = {}
	local head = args["માથું"]; if head == "" then head = nil end
	local i = 2
	
	while head do
		table.insert(heads, head)
		head = args["માથું" .. i]; if head == "" then head = nil end
		i = i + 1
	end
	
	-- Gather gender and number specifications
	-- Iterate over all gn parameters (g2, g3 and so on) until one is empty
	local genders = {}
	local g = args["લિંગ"]; if g == "" then g = nil end
	local i = 2
	
	while g do
		table.insert(genders, g)
		g = args["લિંગ" .. i]; if g == "" then g = nil end
		i = i + 1
	end
	
	-- Gather inflected forms
	local inflections = {}
	
	local i = 1
	local label = args[i * 2 + 1]
	local accel = args["રૂપ" .. i .. "પ્રવેગ"]; if accel == "" then accel = nil end
	local nolink = args["રૂપ" .. i .. "નાકડી"]; if nolink == "" then nolink = nil end
	local parts = {label = label, accel = accel, nolink = nolink}
	
	while label do
		local term = args[i * 2 + 2]; if term == "" then term = nil end
		local alt = args["રૂપ" .. i .. "બીજું"]; if alt == "" then alt = nil end
		local sc = args["રૂપ" .. i .. "લિપિ"]; if sc == "" then sc = nil end
		local id = args["રૂપ" .. i .. "ઓળખ"]; if id == "" then id = nil end
		local gender = args["રૂપ" .. i .. "લિંગ"]; if gender == "" then gender = nil end
		
		if term then
			table.insert(parts, {term = term, alt = alt, sc = sc, id = id, genders = {gender}})
		end
		
		i = i + 1
		label = args[i * 2 + 1]
		accel = args["રૂપ" .. i .. "પ્રવેગ"]; if accel == "" then accel = nil end
		nolink = args["રૂપ" .. i .. "નાકડી"]; if nolink == "" then nolink = nil end
		
		-- If the next label is not "or" then insert the previous one and create a new one.
		if label ~= "કે" then
			-- Only insert if the previous label is not empty.
			if (parts.label or "") ~= "" then
				table.insert(inflections, parts)
			end
			
			parts = {label = label, accel = accel, nolink = nolink}
		end
	end
	
	-- Get/set categories
	local categories = {}
	
	if cat then
		table.insert(categories, lang:getCanonicalName() .. " " .. cat)
	elseif pos then
		-- Make the plural form of the part of speech
		if pos:find("x$") then
			pos = pos .. "ઓ"
		elseif pos ~= "punctuation" or pos ~= "phrasebook" then
			pos = pos .. "ો"
		end
		
		table.insert(categories, lang:getCanonicalName() .. " " .. pos)
	end
	
	if cat2 then
		table.insert(categories, lang:getCanonicalName() .. " " .. cat2)
	end
	
	if cat3 then
		table.insert(categories, lang:getCanonicalName() .. " " .. cat3)
	end
	
	return
		m_headword.full_headword(lang, sc, heads, tr, genders, inflections, categories, sort_key) ..
		m_utilities.format_categories(categories, lang, sort_key)
end

return export