Module:User:Crom daba/sh-headword

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This is a private module sandbox of Crom daba, for his own experimentation. Items in this module may be added and removed at Crom daba's discretion; do not rely on this module's stability.


local export = {}

local m_translit = require("Module:sh-translit")
local m_headword = require("Module:headword")
local m_links = require("Module:links")
local m_sc = require("Module:scripts")
local lang = require("Module:languages").getByCode("sh")
local lang_canon = lang:getCanonicalName()

function map(f, xs)
  local s = {}
  for k,v in pairs(xs) do s[k] = f(v) end
  return s
end

local function sh_yat(yat)
	local s
	if 	   yat == 'i'   			 then s = "Ikavian"
	elseif yat == 'e'   			 then s = "Ekavian"
	elseif yat == 'ije' 			 then s = "Ijekavian"
	elseif yat == 'e+ije' or 'ije+e' then s = "Ekavian and Ijekavian"
	end
	return "<small> (''" .. s .. "'') </small>"
end
local function head (sc,heads, genders, inflections, categories, sort_key, force_cat_output, yat) 
	
	if #heads == 0 then
		local WORDBREAKCHARS = "([%p%s]+)"
		local EXCLUDECHARS = "([^-־׳״'.·*]+)" -- workaround for excluding characters from the above
		local contains_words = false
		mw.ustring.gsub(default_head, WORDBREAKCHARS, function(b) 
						contains_words = contains_words or mw.ustring.match(b, "^" .. EXCLUDECHARS .. "$"); 
						end)
		
		if contains_words then
			local function workaround_to_exclude_chars(s)
				return mw.ustring.gsub(s, EXCLUDECHARS, "]]%1[[") end
		
		local default_head = "[[" .. mw.ustring.gsub(default_head, WORDBREAKCHARS, workaround_to_exclude_chars) .. "]]"
		default_head = mw.ustring.gsub(default_head, "%[%[%]%]", "") -- remove any empty links (which could have been created above at the beginning or end of the string)
		data.heads = {default_head}
		end
	end
	
	local label, altSc
	if sc:getCode() == "Latn" then 
		label = "Cyrillic spelling"
		altSc = m_sc.getByCode("Cyrl")
	else
		label = "Latin spelling"
		altSc = m_sc.getByCode("Latn")
	end
	
	link_translit = function(head) 
		return m_links.full_link( {term = m_translit.tr(head, sc:getCode()),lang=lang, sc=altSc}) 
	end
	local translits = table.concat( map(link_translit, heads), " ''or'' ")
	
	remove_ticks = function(head) return mw.ustring.gsub(head, '`', "") end
	heads = map(remove_ticks, heads)
	
	inflections = inflections or {}
	table.insert(inflections, {label=label, translits})
	return m_headword.full_headword(lang, sc, heads, {}, genders or {"?"}, inflections, categories or {}, sort_key, force_cat_output) .. (yat and sh_yat(yat) or "")
end


local params = {
	["sort"] = {},
	
	-- PoS is the first argument here
	[1] = {required = true},
	["cat2"] = {},
	["cat3"] = {},
	["cat4"] = {},
	["yat"] = {},
	
	["head"] = {list = true, allow_holes = true, default = ""},
	["g"] = {list = true},
	
	[2] = {list = true, allow_holes = true},
	
	["f=accel"]   = {list = true, allow_holes = true},
	["f=request"] = {list = true, allow_holes = true},
	["f=alt"]     = {list = true, allow_holes = true},
	["f=sc"]      = {list = true, allow_holes = true},
	["f=id"]      = {list = true, allow_holes = true},
	["f=tr"]      = {list = true, allow_holes = true},
	["f=g"]       = {list = true, allow_holes = true},
	["f=qual"]    = {list = true, allow_holes = true},
	["f=nolink"]  = {list = true, allow_holes = true, type = "boolean"},
	["f=lang"]    = {list = true, allow_holes = true},
}

--this is mostly a reworking of head_t from headword/templates
function export.head_t(frame)
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	-- Gather basic parameters
	local sort_key = args["sort"]
	local pos = args[1]
	local cat2 = args["cat2"]
	local cat3 = args["cat3"]
	local cat4 = args["cat4"]
	local heads = args["head"]
	local genders = args["g"]
	local yat = args["yat"]
	
	local accels = args["faccel"]
	local requests = args["frequest"]
	local alts = args["falt"]
	local gs = args["fg"]
	local ids = args["fid"]
	local langs = args["flang"]
	local nolinks = args["fnolink"]
	local quals = args["fqual"]
	local scs = args["fsc"]
	local trs = args["ftr"]
	
	--assuming that all heads are in the same script
	local sc = m_sc.findBestScript(heads[1],lang)

	-- Gather inflected forms
	local inflections = {}
	
	--ignore obsolete script parameters
	local start_from = 1
	if args[2][1] == 'r' or args[2][1] == 'c' then start_from = 2 end
		
	-- Go over all the inflection parameters
	for i = start_from, math.ceil(args[2].maxindex / 2) do
		local infl_part = {
			label    = args[2][i * 2 - 1],
			accel    = accels[i-1],
			request  = requests[i-1],
			}
		
		local form = {
			term       =  args[2][i * 2],
			alt        =  alts[i-1],
			genders    = {gs[i-1]},
			id         =  ids[i-1],
			lang       =  lang,
			nolink     =  nolinks[i-1],
			qualifiers = {quals[i-1]},
			sc         =  sc
			}
		
		-- If no term or alt is given, then the label is shown alone.
		if form.term or form.alt then
			table.insert(infl_part, form)
		end
		
		if infl_part.label == "or" then
			-- Append to the previous inflection part, if one exists
			if #infl_part > 0 and inflections[1] then
				table.insert(inflections[#inflections], form)
			end
		elseif infl_part.label then
			-- Add a new inflection part
			table.insert(inflections, infl_part)
		end
	end

	-- Get/set categories
	local categories = {}
	local tracking_categories = {}
	
	if pos then
		table.insert(categories, lang_canon .. " " .. pos)
	end
	
	if cat2 then
		table.insert(categories, lang_canon .. " " .. cat2)
	end
	
	if cat3 then
		table.insert(categories, lang_canon .. " " .. cat3)
	end
	
	if cat4 then
		table.insert(categories, lang_canon .. " " .. cat4)
	end
	
	if #categories == 0 and mw.title.getCurrentTitle().nsText == "Template" then
		categories = {"Undetermined nouns"}
	end

	return
		head (sc, heads, genders, inflections, categories, sort_key, force_cat_output, yat) ..
		require("Module:utilities").format_categories(tracking_categories, lang, sort_key)
end


return export