Modulo:utilitats
Come che se vede
La documentazione per questo modulo può essere creata in Modulo:utilitats/man
local p = {}
function p.ordenacio(base, lang)
local taula = mw.loadData("Module:łéngua/órdene")
if not lang then
lang = "mul" -- ordenació multilingüe per alfabet llatí
end
local llenguaTaula = taula[lang]
if not llenguaTaula then
llenguaTaula = taula["mul"]
end
-- Generate a default sort key
local sortBase = base
if llenguaTaula.sort_key_module ~= nil then
sortBase = require("Module:" .. llenguaTaula.sort_key_module).ordena(base)
else
-- Remove initial hyphens and *
base = mw.ustring.gsub(base, "^[-־ـ*]+(.)",
"%1")
-- Remove anything in parentheses, as long as they are either preceded or followed by something
base = mw.ustring.gsub(base, "(.)%([^()]+%)", "%1")
base = mw.ustring.gsub(base, "%([^()]+%)(.)", "%1")
-- If there are language-specific rules to generate the key, use those
if llenguaTaula.sort_key then
local lowerBase = mw.ustring.lower(base)
sortBase = lowerBase
for i, from in ipairs(llenguaTaula.sort_key.from) do
local to = llenguaTaula.sort_key.to[i] or ""
sortBase = mw.ustring.gsub(sortBase, from, to)
end
if sortBase ~= lowerBase then
sortBase = sortBase .. lowerBase
end
if lowerBase ~= base then
sortBase = sortBase .. base
end
else
sortBase = base
end
end
return sortBase
end
-- Format the categories with the appropriate sort key
function p.format_categories(categories, lang, sort_key, sort_base)
local NAMESPACE = mw.title.getCurrentTitle().nsText
if NAMESPACE == "" then
local PAGENAME = mw.title.getCurrentTitle().text
local SUBPAGENAME = mw.title.getCurrentTitle().subpageText
if not sort_key then
sort_key = p.ordenacio(sort_base or SUBPAGENAME, lang)
end
-- If the resulting key is the same as the wiki software's default, remove it
if sort_key == PAGENAME or sort_key == "" then
sort_key = nil
end
local out_categories = {}
for key, cat in ipairs(categories) do
out_categories[key] = "[[Categoria:" .. cat .. (sort_key and "|" .. sort_key or "") .. "]]"
end
return table.concat(out_categories, "")
else
return ""
end
end
-- Utilitzada per {{categoritza}} i {{catllengua}}
function p.template_categorize(frame)
local NAMESPACE = mw.title.getCurrentTitle().nsText
local format = frame.args["format"]
local args = frame:getParent().args
local langcode = args[1]; if langcode == "" then langcode = nil end
local sort_key = args["ordene"]; if sort_key == "" then sort_key = nil end
local categories = {}
local lang
if langcode then
lang = require("Module:łéngua").nom(langcode)
end
if lang == langcode then
if NAMESPACE == "Modèl" then return "" end
error("El codi de llengua \"" .. (langcode or '-') .. "\" no és vàlid.")
end
local sufix = ""
if format == "pos" then
sufix = " en " .. lang
end
local i = 2
local cat = args[i]
while cat do
if cat ~= "" then
table.insert(categories, mw.ustring.gsub(cat, "^%l", mw.ustring.upper) .. sufix)
end
i = i + 1
cat = args[i]
end
return p.format_categories(categories, langcode, sort_key)
end
-- Converteix una llista en una taula amb valor true per cada element
function p.llista (list)
local set = {}
for _, l in ipairs(list) do set[l] = true end
return set
end
-- Afegeix una subModèl de rastreig, copiat de [[:en:Module:debug]]
-- Vegeu https://en.wiktionary.org/wiki/Template:tracking
function p.track(key)
if type(key) == "table" then key = key.args[1] end -- des de plantilles via invoke o des de mòduls via require
local frame = mw.getCurrentFrame()
pcall(frame.expandTemplate, frame, { title = 'rastreig/' .. key })
end
-- Funció equivalent a pairs() però ordenat per key
function p.spairs(t)
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
table.sort(keys)
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
return p