Przejdź do zawartości

Moduł:etymologia

Z Wikisłownika – wolnego słownika wielojęzycznego

Moduł wykorzystywany przez szablon {{etymologia}}.


local p = {}

local linki = require("Module:linki")
local usunDiakrytyki = require("Module:usuń_diakrytyki")

----------------------------------------------------------------
-- Namespace
----------------------------------------------------------------

local function getTitle()
	return mw.title.getCurrentTitle()
end

local function allowCategories()
	local t = getTitle()
	return t.namespace == 0 or t.text:match("^Rekonstrukcja:/")
end

----------------------------------------------------------------
-- Dane językowe
----------------------------------------------------------------

local function getLangData(iso)
	if not iso or iso == "" then return nil end
	local data = mw.loadData("Module:lang/dane")
	return data[iso]
end

local function getMianownikLM(iso)
	local d = getLangData(iso)
	return (d and d["mianownik lm"]) or iso
end

local function getDopelniacz(iso)
	local d = getLangData(iso)
	return (d and d["dopełniacz"]) or iso
end

----------------------------------------------------------------
-- Wielkość liter
----------------------------------------------------------------

local function ucfirst(s)
	return mw.ustring.upper(mw.ustring.sub(s,1,1)) .. mw.ustring.sub(s,2)
end

local function lcfirst(s)
	return mw.ustring.lower(mw.ustring.sub(s,1,1)) .. mw.ustring.sub(s,2)
end

----------------------------------------------------------------
-- Kategorie (stare typy)
----------------------------------------------------------------

local function buildCategories(args, typ)
	local langTo   = args[1]
	local langFrom = args[2]

	local L1 = getMianownikLM(langTo)
	local L2_dop = getDopelniacz(langFrom)
	local L2_mian = getMianownikLM(langFrom)

	local cats = {}

	if typ == "zapożyczenie" or typ == "zapożyczenie sztuczne" then
		table.insert(cats, "Terminy " .. L1 .. " zapożyczone z " .. L2_dop)
		table.insert(cats, "Terminy " .. L1 .. " pochodzące z " .. L2_dop)
	end

	if typ == "zapożyczenie sztuczne" then
		table.insert(cats, "Terminy " .. L1 .. " sztucznie zapożyczone z " .. L2_dop)
	end

	if typ == "pochodne" then
		table.insert(cats, "Terminy " .. L1 .. " pochodzące z " .. L2_dop)
	end

	if typ == "kalka" then
		table.insert(cats, "Terminy " .. L1 .. " pochodzące z " .. L2_dop)
		table.insert(cats, "Terminy " .. L1 .. " kalkujące " .. L2_mian)
	end

	return cats
end

----------------------------------------------------------------
-- Główna logika (stare typy)
----------------------------------------------------------------

local function makeEtymology(frame, args, typ)
	local langFrom = args[2]
	local term = args[3]

	if not langFrom or langFrom == "" or not term or term == "" then
		return ""
	end

	local linkArgs = {}
	for k, v in pairs(args) do
		linkArgs[k] = v
	end
	linkArgs[1] = langFrom
	linkArgs[2] = term

	local link = linki.make(frame, linkArgs)

	local out = ""

	if args.beztekstu ~= "1" then
		local intro = ucfirst(typ)
		if args["mała"] == "1" then
			intro = lcfirst(intro)
		end

		out = string.format(
			"[[%s|%s]] z %s ",
			typ,
			intro,
			getDopelniacz(langFrom)
		)
	end

	out = out .. link

	local cats = buildCategories(args, typ)
	local testMode = args.test == "1"

	if allowCategories() and not testMode then
		for _, c in ipairs(cats) do
			out = out .. "[[Kategoria:" .. c .. "]]"
		end
	elseif testMode then
		out = out .. "<br /><small>Kategorie:"
		for _, c in ipairs(cats) do
			out = out .. " [[" .. c .. "]]"
		end
		out = out .. "</small>"
	end

	return out
end

----------------------------------------------------------------
-- AFIKSY (NOWA LOGIKA)
----------------------------------------------------------------

local function isPrefix(term)
	return term:match("%-$") and not term:match("^%-")
end

local function isSuffix(term)
	return term:match("^%-") and not term:match("%-$")
end

local function isInfix(term)
	return term:match("^%-") and term:match("%-$")
end

local function makeAfiks(frame, args)
	local baseLang = args[1]
	if not baseLang or baseLang == "" then return "" end

	local outParts = {}
	local cats = {}

	local hasForeignBase = false
	local foreignLang = nil

	-- pomocnicza: wykrywanie typu afiksu
	local function detectAffixType(term)
		if term:match("^%-") and term:match("%-$") then
			return "międzyrostek"
		elseif term:match("^%-") then
			return "przyrostek"
		elseif term:match("%-$") then
			return "przedrostek"
		end
		return nil
	end

	local i = 2
	while args[i] do
		local term = args[i]
		local idx = i - 1

		local langOverride = args["jęz"..idx]
		local lang = langOverride or baseLang

		local isAffix = term:match("^%-") or term:match("%-$")

		-- wykrycie adaptowanej pożyczki
		if i == 2 and not isAffix and langOverride then
			hasForeignBase = true
			foreignLang = langOverride
		end

		-- link przez Module:linki
		local linkArgs = {
			[1] = lang,
			[2] = term,
			t = args["t"..idx],
			tr = args["tr"..idx]
		}

		local link = linki.make(frame, linkArgs)

		-- dodanie języka (ale NIE dla głównego terminu adaptacji)
		if langOverride and not (i == 2 and not isAffix) then
			local langName = isAffix and getMianownikLM(langOverride) or getDopelniacz(langOverride)
			link = langName .. " " .. link
		end

		table.insert(outParts, link)

		-- typ afiksu (ręczny > automatyczny)
		local forcedType = args["typ"..idx]
		local affixType = forcedType or detectAffixType(term)
		
		if affixType then
			-- normalizacja (usunięcie diakrytyków)
			local normalized = term
			if type(usunDiakrytyki) == "table"
				and type(usunDiakrytyki.stripPlain) == "function"
			then
				normalized = usunDiakrytyki.stripPlain(baseLang, term) or term
			end
		
			-- fleksja
			local inflected
			if affixType == "przedrostek" then
				inflected = "przedrostkiem"
			elseif affixType == "przyrostek" then
				inflected = "przyrostkiem"
			elseif affixType == "międzyrostek" then
				inflected = "międzyrostkiem"
			elseif affixType == "wrostek" then
				inflected = "wrostkiem"
			else
				inflected = affixType
			end
		
			table.insert(
				cats,
				"Terminy " .. getMianownikLM(baseLang) .. " z " .. inflected .. " " .. normalized
			)
		end

		i = i + 1
	end

	local out = table.concat(outParts, " + ")

	-- adaptowana pożyczka
	if hasForeignBase then
		out = "Adaptowana pożyczka z " .. getDopelniacz(foreignLang) .. " " .. out

		table.insert(cats, "Terminy " .. getMianownikLM(baseLang) .. " zapożyczone z " .. getDopelniacz(foreignLang))
		table.insert(cats, "Terminy " .. getMianownikLM(baseLang) .. " pochodzące z " .. getDopelniacz(foreignLang))
	end

	-- usunięcie duplikatów
	local seen = {}
	local finalCats = {}
	for _, c in ipairs(cats) do
		if not seen[c] then
			seen[c] = true
			table.insert(finalCats, c)
		end
	end

	-- dodanie kategorii / test
	if allowCategories() and args.test ~= "1" then
		for _, c in ipairs(finalCats) do
			out = out .. "[[Kategoria:" .. c .. "]]"
		end
	elseif args.test == "1" then
		if #finalCats > 0 then
			out = out .. "<br /><small>Kategorie:"
			for i, c in ipairs(finalCats) do
				if i > 1 then
					out = out .. ","
				end
				out = out .. " [[:Kategoria:" .. c .. "|" .. c .. "]]"
			end
			out = out .. "</small>"
		end
	end

	return out
end

----------------------------------------------------------------
-- PUBLIC
----------------------------------------------------------------

local function getArgs(frame)
	return frame:getParent() and frame:getParent().args or frame.args
end

function p.zapozyczenie(frame)
	return makeEtymology(frame, getArgs(frame), "zapożyczenie")
end

p["zapożyczenie sztuczne"] = function(frame)
	return makeEtymology(frame, getArgs(frame), "zapożyczenie sztuczne")
end

function p.pochodne(frame)
	return makeEtymology(frame, getArgs(frame), "pochodne")
end

function p.kalka(frame)
	return makeEtymology(frame, getArgs(frame), "kalka")
end

function p.afiks(frame)
	return makeAfiks(frame, getArgs(frame))
end

return p