A modult a Modul:term etymology/templates/doc lapon tudod dokumentálni

local export = {}

local m_languages = require("Module:languages")

local gsub = mw.ustring.gsub

local template_mappings = {
	derived = "der",
	inherited = "inh",
	borrowing = "bor",
	loan = "bor",
	calque = "cal",
	calq = "cal",
	affix = "af",
	confix = "con",
	prefix = "pre",
	compound = "com",
	["blend of"] = "blend",
	suffix = "suf",
}

local function process_etymology(lang, entryLang, source, term, args)
	local etymology = require("Module:term etymology").getEtymology(entryLang, term)
	
	-- rename templates to shorter versions
	etymology = gsub(
		etymology,
		"{{([^|]*)|",
		function(template_name)
			return "{{" .. (template_mappings[template_name] or template_name) .. "|"
		end
	)
	
	etymology = gsub(etymology, "{{bor([^}]*)|notext=[^|}]*([^}]*)}}", "{{bor%1%2}}")
	etymology = gsub(etymology, "{{bor([^}]*)|nocap=[^|}]*([^}]*)}}", "{{bor%1%2}}")
	etymology = gsub(etymology, "{{bor|", "from {{der|")
	
	local deriv_tls = {'der', 'inh'}
	-- If not inherited, replace {{inh}} with {{der}}
	if args["noinh"] then
		etymology = gsub(etymology, "{{inh|", "{{der|")
		deriv_tls = {'der'} --remove 'inh'
	elseif mw.ustring.find(etymology, "{{der|") then --find all {{inh}} after the first {{der}} and replace them
		local pre, post = mw.ustring.match(etymology, "(.-)({{der|.*)")
		etymology = pre .. gsub(post, "{{inh|", "{{der|")
	end
	
	-- Replace source language in nested parent etymologies with source language of child entry
	for _, template in ipairs(deriv_tls) do
	  etymology = gsub(etymology, "{{(" .. template .. ")|[%a%-]*|", "{{%1|" .. source .. "|")
	end
	
	-- etyl works backwards, annoyingly
	-- This should omit {{etyl|foo|-}}
	etymology = gsub(etymology, "{{etyl|([%a%-]*)|[%a]+%-?[%a]*}}", "{{etyl|%1|" .. source .. "}}")
	
	-- Add nocat=1 to all morphology templates
	local morph_tls = {'af', 'blend', 'circumfix', 'con', 'com', 'infix', 'pre', 'suf', 'transfix'}
	for _, template in ipairs(morph_tls) do
	  etymology = gsub(etymology, "{{(" .. template .. ")|([^}]*)}}", "{{%1|%2|nocat=1}}")
	end

	return etymology
end

function export.show(frame)
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "und"},
		[3] = {required = true},
		["noinh"] = {type = "boolean"},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local source, lang, term = args[1], args[2], args[3]
	
	lang = m_languages.getByCode(lang)
		or require("Module:etymology languages").getByCode(lang)
		or m_languages.err(lang, 1)
		
	local entryLang = require("Module:etymology").getNonEtymological(lang)
	
	if lang:getCode() ~= entryLang:getCode() then
		-- [[Special:WhatLinksHere/Template:tracking/descendant/etymological]]
		require("Module:debug").track("descendant/etymological")
		require("Module:debug").track("descendant/etymological/" .. lang:getCode())
	end
	
	return frame:preprocess(process_etymology(lang, entryLang, source, term, args))
end

return export