Module:User:Per utramque cavernam/Etymology

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


local export = {}


--[[	If language is an etymology language, iterates through parent languages
		until it finds a full language. ]]
function export.getFull(lang)
	while lang:getType() == "etymology language" do
		local parentCode = lang:getParentCode()
		local parent = require("Module:languages").getByCode(parentCode)
			or require("Module:etymology languages").getByCode(parentCode)
			or require("Module:families").getByCode(parentCode)
		
		lang = parent
--		mw.log(terminfo.lang:getCode() .. " " .. terminfo.lang:getType())
	end
	
	return lang
end


local function termError(terminfo)
	local type = terminfo.lang:getType()
	if type == "family" then
		if terminfo.term then
			require("Module:debug").track("etymology/family/has-term")
		end
		terminfo.term = "-"
	end
	return terminfo
end


local function createLink(terminfo, templateName)
	local link = ""
	
	if terminfo.term == "-" then
		--[=[
		[[Special:WhatLinksHere/Wiktionary:Tracking/cognate/no-term]]
		[[Special:WhatLinksHere/Wiktionary:Tracking/derived/no-term]]
		[[Special:WhatLinksHere/Wiktionary:Tracking/borrowed/no-term]]
		[[Special:WhatLinksHere/Wiktionary:Tracking/calque/no-term]]
		]=]
		require("Module:debug").track(templateName .. "/no-term")
	else
--		mw.log(terminfo.term)
		link = " " .. require("Module:links").full_link(terminfo, "term", true)
	end
	
	return link
end

function export.format_etyl(lang, source, sort_key, categories)
	local info = {}
	
	
	-- [[Special:WhatLinksHere/Wiktionary:Tracking/etymology/sortkey]]
	if sort_key then
		require("Module:debug").track("etymology/sortkey")
	end
	
	if not categories then
		categories = {}
	end
	
	if source:getCode() == "und" then
		info = {
			display = "undetermined",
			cat_name = "other languages",
		}
	else
		info.display = source:makeWikipediaLink()
		
		if source:getType() == "family" then
			info.cat_name = source:getCategoryName()
		else
			info.cat_name = source:getCanonicalName()
		end
	end
	
	-- Add the categories, but only if there is a current language
	
	if lang then
		local m_utilities = require("Module:utilities")
		
		table.insert(categories, lang:getCanonicalName() .. " terms derived from " .. info.cat_name)
		
		categories = m_utilities.format_categories(categories, lang, sort_key)
	else
		categories = ""
	end
	
	return "<span class=\"etyl\">" .. info.display .. categories .. "</span>"
end

-- Internal implementation of {{phono-semantic matching|...}} template
function export.psm(lang, terminfo, sort_key, nocap, notext)
	if nocap then
		require("Module:debug").track("psm/nocap")
	end
	
	local source = terminfo.lang
	
	terminfo.lang = export.getFull(terminfo.lang)

	terminfo = termError(terminfo)

	local text = ""
	local categories = {}

	if source:getType() == "family" then
		table.insert(categories, lang:getCanonicalName() .. " phono-semantic matchings ")
	else
		table.insert(categories, lang:getCanonicalName() .. " phono-semantic matchings ")
	end
	
	if not notext then
		text = "[[w:Phono-semantic matching|" .. (nocap and "p" or "P") .. "hono-semantic matching]] of "
	end
	
	local link = createLink(terminfo, "psm")
	
	return text .. export.format_etyl(lang, source, sort_key, categories) .. link
end


return export