Module:cognates: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
Line 8: Line 8:
local args = getArgs(frame)
local args = getArgs(frame)
local links = {}
local links = {}
local nat = m_languages.find_by_code(args[1] or "") ~= nil and m_languages.get_by_code(args[1])
local nat = m_languages.find_by_code(args[1] or "")
local params = {
local params = {
nocap = false,
nocap = false,
nocat = false,
}
}
Line 17: Line 18:
for lang, _ in pairs(args) do
for lang, _ in pairs(args) do
if type(lang) == "string" then
if type(lang) == "string" then
if m_languages.find_by_code(lang) and lang ~= nat then
if m_languages.find_by_code(lang) then
table.insert(langs, lang)
table.insert(langs, lang)
elseif params[lang] ~= nil then --check for parameters
elseif params[lang] ~= nil then --check for parameters
Line 31: Line 32:
b = m_languages.get_by_code(b)
b = m_languages.get_by_code(b)
if nat == false then
if nat == nil then
return a.name < b.name
return a.name < b.name
end
end
Line 50: Line 51:
data[1], data[2] = lang, term
data[1], data[2] = lang, term
local link = frame:expandTemplate{title = "m+", args = data}
local link = frame:expandTemplate{title = "m+", args = data}
if nat then link = link .. "[[Category:" .. nat.name .. " terms with " .. lang.name .. " cognates]]" end
if nat and nocat == false then link = link .. "[[Category:" .. nat.name .. " terms with " .. m_languages.get_by_code(lang).name .. " cognates]]" end
table.insert(links, link)
table.insert(links, link)
end
end

Revision as of 01:29, 7 August 2024

Underlies {{cognates}}.


local export = {}
local getArgs = require('Module:Arguments').getArgs

local m_inline = require("Module:inline")
local m_languages = require("Module:languages")

function export.show(frame)
	local args = getArgs(frame)
	local links = {}
	local nat = m_languages.find_by_code(args[1] or "")
	local params = {
		nocap = false,
		nocat = false,
	}
	
	local langs = {}
	
	for lang, _ in pairs(args) do
		if type(lang) == "string" then
			if m_languages.find_by_code(lang) then
				table.insert(langs, lang)
			elseif params[lang] ~= nil then --check for parameters
				params[lang] = true
			else
				error("[" .. lang .. "] is not a valid language code or parameter")
			end
		end
	end
	
	table.sort(langs, function(a, b)
		a = m_languages.get_by_code(a)
		b = m_languages.get_by_code(b)
		
		if nat == nil then
			return a.name < b.name
		end
		
		a_test = m_languages.stage_at_split(nat, a)
		b_test = m_languages.stage_at_split(nat, b)
		
		if a_test == b_test then
			return a.name < b.name
		else
			return a_test > b_test
		end
	end
	)
	
	for _, lang in ipairs(langs) do
		local term, data = m_inline.parse(args[lang])
		data[1], data[2] = lang, term
		local link = frame:expandTemplate{title = "m+", args = data}
		if nat and nocat == false then link = link .. "[[Category:" .. nat.name .. " terms with " .. m_languages.get_by_code(lang).name .. " cognates]]" end
		table.insert(links, link)
	end
	
	local text = ((params.nocap and "c") or "C") .. "ognate with "
	
	for i, link in ipairs(links) do
		if i > 1 then
			if links[i + 1] then
				text = text .. ", "
			else
				text = text .. " and "	
			end
		end
		text = text .. link	
	end
	
	return text
end

return export

--[[
Debug console test string:
=p.show(mw.getCurrentFrame():newChild{title="whatever",args={["rad"]="vas", ["lfv"] = "vat"}})
]]