မေႃႇၵျူး:str
Appearance
တူၺ်းပႃး
[မႄးထတ်း]- Module:string utilities – main module for string manipulation
- Module:string
-- Adapted from https://fr.wiktionary.org/wiki/Module:str originally written by 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 (caractere)
return mw.ustring.format(format, mw.ustring.codepoint(caractere))
end)
return str
end
function p.gsub(frame)
local args
if frame.args["direct"] then
args = frame.args
else
args = frame:getParent().args
end
if args[3] and args[4] then
local table = {}
local i = 3
repeat
table[args[i]] = args[i + 1]
i = i + 2
until not (args[i] and args[i + 1])
-- étrangement, un chiffre dans une table ne marche pas avec mw.ustring.gsub
return (mw.ustring.gsub(args[1] or "", args[2] or "", function (x) return table[x] end)) -- parenthèses nécessaires
else
return (mw.ustring.gsub(args[1] or "", args[2] or "", args[3] or "")) -- parenthèses nécessaires
end
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.sans_balise(frame)
local str = frame.args[1] or ""
str = mw.ustring.gsub(str, "<ruby[^>]->(.-)</ruby[^>]->",
function (chars)
chars = mw.ustring.gsub(chars, "<r[tp][^>]->(.-)</r[tp][^>]->", "")
return chars
end)
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 ""
-- au plus 4 valeurs maintenant
return mw.ustring.format(format, frame.args[2], frame.args[3], frame.args[4], frame.args[5])
end
function p.liste_de_domaines(frame)
local str = frame.args[1] or ""
local ou = frame.args["ou"] 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 %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.supprimer_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