Module:Str
La documentation pour ce module peut être créée à Module:Str/doc
-- Adapté de https://fr.wiktionary.org/wiki/Module:str écrit par TAKASUGI Shinji
local p = {}
function p.len(frame)
local string = frame.args[1] or ''
return mw.ustring.len(mw.text.trim(string))
end
function p.sub(frame)
local args = frame.args
local string = args[1] or ''
local start = (tonumber(args[2]) or 0) + 1
local length = tonumber(args[3]) or 0
if length <= 0 then return ''
else
return mw.ustring.sub(mw.text.trim(string), start, start + length - 1)
end
end
function p.find(frame)
local args = frame.args
local first, last = mw.ustring.find(mw.text.trim(args[1]), args[2], 1, true)
if first and first <= last then return first
else return ''
end
end
function p.char(frame)
return mw.ustring.char(frame.args[1])
end
function p.codepoint(frame)
local str = frame.args[1] or ""
local format = frame.args["format"] or "%d"
str = mw.ustring.gsub(str, "(.)",
function (caractero)
return mw.ustring.format(format, mw.ustring.codepoint(caractero))
end)
return str
end
function p.gsub(frame)
return (mw.ustring.gsub(frame.args[1], frame.args[2], frame.args[3])) -- parentèses nècèssères
end
function p.gmatch(frame)
local str = ''
for pattern in mw.ustring.gmatch(frame.args[1], frame.args[2]) do
str = str .. pattern
end
return str
end
function p.sen_balisa(frame)
local str = frame.args[1] or ""
str = mw.ustring.gsub(str, "<[^>]*>", "")
str = mw.ustring.gsub(str, "'''?", "")
str = mw.ustring.gsub(str, "%[%[(.-)%]%]",
function (chars)
chars = mw.ustring.gsub(chars, ".-%|", "")
return chars
end)
return str
end
function p.format(frame)
local format = frame.args[1] or ""
-- u més 4 valors ora
return mw.ustring.format(format, frame.args[2], frame.args[3], frame.args[4], frame.args[5])
end
function p.lista_de_domenos(frame)
local str = frame.args[1] or ""
local ou = frame.args["ou ben"] or ""
str = mw.ustring.gsub(str, "%)''([^']*)$", "%1")
str = mw.ustring.gsub(str, "%)''", ", ")
str = mw.ustring.gsub(str, "''%(", "")
if ou ~= "" then
str = mw.ustring.gsub(str, ", ([^,]*)$", " ou ben %1")
end
return "''(" .. str .. ")''"
end
function p.ucfirstall(frame)
local str = frame.args[1] or ""
str = mw.ustring.gsub(str, "^%S", mw.ustring.upper)
str = mw.ustring.gsub(str, "%s%S", mw.ustring.upper)
return str
end
function p.suprimar_p(frame)
local str = frame.args[1] or ""
str = mw.ustring.gsub(str, "^</?p>\n?", " \n")
str = mw.ustring.gsub(str, "</?p>", "<br />")
return str
end
function p.non_latin(frame)
local str = frame.args[1] or ''
str = mw.text.decode(str, true)
str = mw.ustring.lower(mw.ustring.gsub(mw.ustring.toNFD(str), "[^%w]", ""))
str = mw.ustring.gsub(str, "[!-˿Ḁ-ỿ]", "") -- U+0021-02FF, U+1E00-1EFF
return str
end
return p