Modul:coglist

A Wikiszótárból, a nyitott szótárból

A modult a Modul:coglist/doc lapon tudod dokumentálni

local m_links = require("Module:links")
local m_etymology = require("Module:etymology")

local export = {}

-- Copied from Module:links/templates (function: export.l_term_t), and adapted.
function export.mention(l, w, alt, gloss, gender, id, tr, sc, pos, lit) -- l = language, w = word
	local face = "term"

	local lang = require("Module:languages").getByCode(l)
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	-- Forward the information to full_link
	return m_links.full_link({lang = lang, sc = sc, term = w, alt = alt, id = id, tr = tr, genders = gender, gloss = gloss, pos = pos, lit = lit}, face, allowSelfLink)
end

-- Copied from Module:etymology/templates (function: export.cognate), and adapted.
function export.cognate(l, w, alt, gloss, gender, id, tr, sc, pos, lit) -- l = language, w = word
	source =
		require("Module:languages").getByCode(l) or
		require("Module:etymology languages").getByCode(l) or
		require("Module:families").getByCode(l) or
		error("The language, family or etymology language code \"" .. l .. "\" is not valid.")

	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	return m_etymology.format_cognate(
		{lang = source, sc = sc, term = w, alt = alt, id = id, genders = gender, tr = tr, gloss = gloss, pos = pos, lit = lit}, nil)
end

--The main function, called by Template:coglist.
function export.coglist(frame)

	-- Parameter 1 = currentLanguageSection (e.g., "fr" if the current section is French).
	-- Parameter 2 = group of cognates, according to Module:coglist/data
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = ""},
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local currentLanguageSection = args[1]
	local group = args[2]

	-- Loads the list of cognates in Module:coglist/data.
	local data = mw.loadData("Module:coglist/data")

	-- Created the variable "list", which is going to be filled with a single list of all the cognates from the same group.
	local list = {}

	-- k = key, it is the key to "v", an iteration of each language code: "fr", "es", "pt", etc.
	-- v = value, it is an iteration of all the contents associated with the current language code (multiple terms, glosses, transliterations, etc.)
	for k, v in pairs(data[group]) do

		-- Tests if the current language is different from the current language section.
		-- This test is to avoid displaying "Compare Occitan foo" in an Occitan entry.
		if k ~= currentLanguageSection then

			-- The argument "current" is a text like "French word1, word2, word3", to be formatted below and then added as a field in "list".
			local current = ""

			-- kk = key, it is the key to "vv"
			-- vv = value, it is an iteration of all the contents associated with the current separate term (term, alt, gloss, etc.)
			for kk, vv in pairs(data[group][k]) do

				-- rawParameters is a list of all values "vv" separated by "|"
				local rawParameters = mw.text.split (vv, "|", true)

				-- namedParameters is going to be filled with named parameters (tr=foo, sc=bar, etc.)
				-- unnamedParameters is going to be filled with unnamed parameters (parameters without an equal sign)
				local namedParameters = {}
				local unnamedParameters = {}

				-- kkk = key, it is the key to "vvv"
				-- vvv = value, it is an iteration of all the items in rawParameters, to be separated into namedParameters and unnamedParameters
				for kkk, vvv in pairs(rawParameters) do

					-- Searches for the equal sign ("="), an indication that the parameter is named.
					if string.find(rawParameters[kkk], "=", 1, 1) then

						-- Inserts a named parameter in namedParameter.
						-- Example: in "tr=lorem ipsum", the parameterName is "tr" and the parameterValue is "lorem ipsum".
						-- In namedParameters, the key is parameterName and the value is parameterValue.
						local equalSignSplit = mw.text.split (rawParameters[kkk], "=", true)
						local parameterName = equalSignSplit[1]
						local parameterValue = equalSignSplit[2]
						namedParameters[parameterName] = parameterValue

					else
						-- Inserts an unnamed parameter in unnamedParameter.
						table.insert(unnamedParameters, rawParameters[kkk])
					end
				end

				-- These are all the values to be formatted, taken from unnamedParameters and namedParameters.
				local language = k
				local term = unnamedParameters[1]
				local alt = unnamedParameters[2]
					if alt == "" then alt = nil end
				local gloss = unnamedParameters[3]
				local gender = unnamedParameters["g"]
				local id = namedParameters["id"]
				local tr = namedParameters["tr"]
				local sc = namedParameters["sc"]
				local pos = namedParameters["pos"]
				local lit = namedParameters["lit"]

				-- Suppose there are 3 French words. The final result should look like:
				-- "{{cog|fr|word1}}, {{m|fr|word2}}, {{m|fr|word3}}" = "French word1, word2, word3"
				-- {{cog}} is a template that returns the language name along with the term. {{m}} returns only the term.
				-- Here, export.cognate takes the role of {{cog}} and export.mention takes the role of {{m}}.
				-- If the current word is the 1st French word, the module uses export.cognate; the subsequent words use export.mention.
				if kk > 1 then
					current = current .. ", " .. export.mention(language, term, alt, gloss, gender, id, tr, sc, pos, lit)
				else
					current = current .. export.cognate(language, term, alt, gloss, gender, id, tr, sc, pos, lit)
				end
			end

			-- This inserts the argument "current", a text like "French word1, word2, word3" as a field in "list".
			-- Each field of "list" is like this: "German foo", "Portuguese bar", etc.
			table.insert(list, current)

		end
	end

	-- Sorts the list alphabetically.
	table.sort(list)

	-- The variable "result" is going to contain the end text.
	local result = ""

	-- Opens a span tag with the CSS class "coglist".
	local opentag = '<span class="coglist">'
	result = result .. opentag

	-- Starts rendering the text, with the word "Compare" followed by a space.
	local intro = "Compare "
	result = result .. intro

	-- Separates all cognates by ", " (comma and space).
	for k, v in pairs(list) do
		if result ~= opentag .. intro then
			result = result .. ", "
		end

		result = result .. list[k]
	end

	-- Places a dot (.) at the end.
	result = result .. "."

	-- Closes the span tag with the CSS class "coglist".
	local closetag = '</span>'
	result = result .. closetag

	return result
end

return export