Mòdul:StringToSort
Aparença
Documentació del mòdul [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]
A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]
Mòdul utilitzat per obtenir una clau d'ordenació tot eliminant preposicions i articles en el cas d'una frase. Traient els diacrítics (majorment accents de les lletres) i posat la primera paraula a majúscules.
Utilitzat per: {{StringToSort}}, {{StringToSortDelFirst}} i {{StringToSortDelBegin}}.
local s = {}
function s._getParameters(frame_args, arg_list)
local new_args = {}
local index = 1
local value
for i, arg in ipairs(arg_list) do
value = frame_args[arg]
if value == nil then
value = frame_args[index]
index = index + 1
end
new_args[arg] = value
end
return new_args
end
function capitalize(arg)
local long = mw.ustring.len(arg)
if long == 0 then
arg = ''
else
local str1 = mw.ustring.sub(arg, 1, 1)
str1 = mw.ustring.upper(str1)
if long == 1 then
arg = str1
else
local str2 = mw.ustring.sub(arg, 2,long)
arg = str1 .. str2
end
end
return arg
end
function capitalize2(arg, del_first)
local sort_key = {}
local nosort_words = require("Module:Utilitats").llista({'','De','Del','Dels','El','Els','La','Les','Per','A','D','I','L','O'})
for w in mw.ustring.gmatch(arg, "%w+") do
w = capitalize(w)
if not nosort_words[w] then
if del_first ~= '' then
if w ~= del_first then
table.insert(sort_key, w)
end
del_first = ''
else
table.insert(sort_key, w)
end
end
end
return table.concat(sort_key, ' ')
end
function without_diacritics_loc(str)
local with_diacritics = 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüý·-'
local without_diacritics = 'AAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuy'
for j = 1, mw.ustring.len(with_diacritics) do
ch1 = mw.ustring.sub(with_diacritics, j, j)
ch2 = mw.ustring.sub(without_diacritics, j, j) or ''
str = mw.ustring.gsub(str, ch1, ch2)
end
return str
end
function s.without_diacritics (frame)
local new_args = s._getParameters(frame.args, {'str'})
local arg = new_args['str'] or ''
if type(arg) == 'string' then
without_diacritics_loc(arg)
return arg
else
local error_str = type(arg)
return error_str
end
end
function s.unaccent_firstcap (frame)
local new_args = s._getParameters(frame.args, {'str', 'del_first', 'del_begin', 'or_char'})
local arg = new_args['str'] or ''
local del_first = new_args['del_first'] or ''
local del_begin = new_args['del_begin'] or ''
local or_char = new_args['or_char'] or ''
if type( arg ) == 'string' then
if del_begin ~= '' then
local l_del_begin = mw.ustring.len(del_begin)
local l_temp = mw.ustring.sub(arg, 1, l_del_begin)
if l_temp == del_begin then
arg = mw.ustring.sub(arg, l_del_begin+1)
end
end
arg = mw.ustring.gsub(arg, "'", " ")
arg = without_diacritics_loc(arg)
arg = capitalize2(arg, del_first)
if (arg == '') and (or_char ~= '') then
arg = or_char
end
return arg
else
local error_str = type(arg)
return error_str
end
end
return s