Vés al contingut

Mòdul:síl·labes

De Viccionari
Icona de documentació de mòdul Documentació del mòdul[mostra] [modifica] [refresca]

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Mòdul de suport de {{síl}} per mostrar la sil·labificació.

local p = {}

local categorise_syllables = {
	["ca"] = "català",
	["es"] = "castellà",
	["it"] = "italià",
}

local function isogram(word)
	local diacritics = { -- letters in alphabets of Romance languages, check for others
		from = {"[äáâàåāã]", "æ" , "[ëéêèēẽє]", "[ïíîìīĩ]", "[öóôòōõø]", "œ" , "[üúûùůūŭũ]", "ç", "ñ", "[ÿýŷỳ]", "['·-]"},
		to   = {"a"        , "ae", "e"        , "i"       , "o"        , "oe", "u"         , "c", "n", "y"     , ""}
		}
	local plain = mw.ustring.lower(word)
	for i, from in ipairs(diacritics.from) do
		plain = mw.ustring.gsub(plain, from, diacritics.to[i])
	end
	local letters = mw.text.split(plain, '')
	local count = {}
	for _, letter in ipairs(letters) do
		count[letter] = (count[letter] or 0) + 1
	end
	local level
	for _, v in pairs(count) do
		if level == nil then
			level = v
		elseif level ~= v then
			return ""
		end
	end
	
	if level == 1 then
		if #letters > 5 then
			table.sort(letters)
			return "\n* Heterograma " .. (#letters == 11 and "d'" or "de ") .. #letters .. " lletres (" .. table.concat(letters) .. ")"
		end
	else
		return "\n* Isograma de " .. #letters/level .. " lletres × " .. level .. " (" .. #letters .. ")"
	end
	return ""
end

--[=[
Meant to be called from a module. `data` is a table containing the following fields:

{
  lang = LANGUAGE_OBJECT,
  hyphs = {{hyph = {"SYL", "SYL", ...}, qualifiers = nil or {"QUALIFIER", "QUALIFIER", ...}}, ...},
  sc = nil or SCRIPT_OBJECT,
  caption = nil or "CAPTION",
  nocaption = BOOLEAN,
}

Here:

* `lang` is a language object.
* `hyphs` is the list of hyphenations to display. SYL is a syllable. QUALIFIER is a qualifier string to display before
  the specific hyphenation in question, formatted using format_qualifier() in [[Module:qualifier]].
* `sc`, if specified, is a script object.
* `caption`, if specified, overrides the default caption "Hyphenation". A colon and space is automatically added after
  the caption.
* `nocaption`, if specified, suppresses the caption entirely.
]=]
function p.format_hyphenations(data)
	local hyphtexts = {}
	local hyphcats = {}

	for _, hyph in ipairs(data.hyphs) do
		if #hyph == 0 then
			error("Paràmetre de síl·labes buit")
		end
		local num_syl = table.maxn(mw.text.split(hyph[1], '·'))
		local text
		if num_syl > 1 then
			text = require("Module:enllaç").full_link {
				lang = data.lang, sc = data.sc, alt = hyph[1], tr = "-" }
			text = text .. " (" .. num_syl .. ")"
		else
			text = "1"
		end
		if hyph.qualifier then
			text = text .. " <span class=\"ib-content\">" .. hyph.qualifier .. "</span>"
		end
		table.insert(hyphtexts, text)
		if categorise_syllables[data.lang.code] then
			if num_syl == 1 then
				table.insert(hyphcats, "Mots en " .. categorise_syllables[data.lang.code] .. " d'1 síl·laba")
			elseif num_syl == 11 then
				table.insert(hyphcats, "Mots en " .. categorise_syllables[data.lang.code] .. " d'11 síl·labes")
			else
				table.insert(hyphcats, "Mots en " .. categorise_syllables[data.lang.code] .. " de " .. num_syl .. " síl·labes")
			end
		end
	end

	local text = table.concat(hyphtexts, ", ")
	local categories = #hyphcats > 0 and require("Module:utilitats").format_categories(hyphcats, data.lang) or ""
	return (data.nocaption and "" or (data.caption or "Síl·labes") .. ": ") .. text .. isogram(data.hyphs[1][1]) .. categories
end


-- The implementation of the {{hyphenation}} template.
function p.hyphenate(args)
	local lang = require("Module:llengua").getByCode(args[1] or "und")
	lang.sc = args.sc or lang.sc
	local data = {
		lang = lang,
		sc = args.sc,
		hyphs = {},
		caption = args.caption,
		nocaption = args.nocaption,
	}

	--copy all unnamed parameters to hyphs
	local list = 1
	for i, param in ipairs(args) do
		if i > 1 then
			table.insert(data.hyphs, {param, ["qualifier"] = args["q" .. (i - 1)]})
			list = list + 1
		end
	end
	
	return p.format_hyphenations(data)
end

-- Entry point for {{hyphenation}} template.
function p.hyphenation(frame)
	local parent_args = frame:getParent().args
	return p.hyphenate(parent_args)
end

return p