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")

----------------------------------------------------------------
-- Namespace i rekonstrukcje
----------------------------------------------------------------

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

local function isMain()
	return getTitle().namespace == 0
end

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

local function allowCategories()
	return isMain() or isReconstruction()
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)
	if d and d["mianownik lm"] then
		return d["mianownik lm"]
	end
	return iso
end

local function getDopelniacz(iso)
	local d = getLangData(iso)
	if d and d["dopełniacz"] then
		return d["dopełniacz"]
	end
	return 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
----------------------------------------------------------------

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
----------------------------------------------------------------

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

	----------------------------------------------------------------
	-- Link do terminu (Module:linki)
	----------------------------------------------------------------

	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)

	----------------------------------------------------------------
	-- Tekst wprowadzający
	----------------------------------------------------------------

	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

	----------------------------------------------------------------
	-- Kategorie / test
	----------------------------------------------------------------

	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

----------------------------------------------------------------
-- Funkcje publiczne (#invoke)
----------------------------------------------------------------

function p.zapozyczenie(frame)
	return makeEtymology(frame, frame:getParent().args, "zapożyczenie")
end

p["zapożyczenie sztuczne"] = function(frame)
	return makeEtymology(frame, frame:getParent().args, "zapożyczenie sztuczne")
end

function p.pochodne(frame)
	return makeEtymology(frame, frame:getParent().args, "pochodne")
end

function p.kalka(frame)
	return makeEtymology(frame, frame:getParent().args, "kalka")
end

return p