45,333
edits
No edit summary |
No edit summary |
||
Line 41: | Line 41: | ||
local s = new_args['s'] or ''; | local s = new_args['s'] or ''; | ||
return mw.ustring.len(s) | return mw.ustring.len(s) | ||
end | |||
--[[ | |||
len_visible | |||
This function returns the length of the target string, excluding the text encompassed in < ... > | |||
Usage: exactly as len, above. | |||
]] | |||
function str.len_visible(frame) | |||
local new_args = str._getParameters(frame.args, { 's' }); | |||
local s = new_args['s'] or ''; | |||
return mw.ustring.len(mw.ustring.gsub(s, "<[^<>]+>", "")) | |||
end | end | ||
Line 311: | Line 324: | ||
function str.find(frame) | function str.find(frame) | ||
local params = { | local params = { | ||
[1] = { required = true }, | [1] = { required = true, allow_empty = true }, | ||
[2] = { required = true }, | [2] = { required = true, allow_empty = true }, | ||
[3] = { type = "number" }, | [3] = { type = "number" }, | ||
[4] = { type = "boolean" }, | [4] = { type = "boolean" }, | ||
Line 400: | Line 413: | ||
str.lc = str.lower | str.lc = str.lower | ||
--[[ | |||
format | |||
This function allows one to format strings according to a template. This is a direct interface onto | |||
str.format() in Lua, and works like the C printf() function. | |||
For example: | |||
{{#invoke:string|format|page_%04d.html|65}} | |||
will produce the result | |||
page_0065.html | |||
Parameters | |||
1: The format template. See https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#string.format | |||
2, 3, ...: Arguments to be inserted into the template. | |||
Note that leading and trailing whitespace is not removed from the arguments. | |||
]] | |||
function str.format(frame) | |||
local fmt = frame.args[1] | |||
-- You can't call unpack() directly on frame.args because it isn't really a | |||
-- table, and doesn't support the # operator. | |||
local args = {} | |||
local i = 2 | |||
while true do | |||
local val = frame.args[i] | |||
if not val then | |||
break | |||
end | |||
table.insert(args, val) | |||
i = i + 1 | |||
end | |||
return fmt:format(unpack(args)) | |||
end | |||
--[[ | --[[ | ||
Line 473: | Line 520: | ||
function str.pattern_escape(pattern_str) | function str.pattern_escape(pattern_str) | ||
local invoked = false | local invoked = false | ||
local escape = require("Module:string/pattern_escape") | |||
if type(pattern_str) == "table" then | if type(pattern_str) == "table" then | ||
Line 492: | Line 540: | ||
if invoked then | if invoked then | ||
return (escape(pattern_str)) -- only the first value | |||
else | else | ||
return | return escape(pattern_str) | ||
end | end | ||
end | end | ||
Line 667: | Line 714: | ||
function str.URIdecode(frame) | function str.URIdecode(frame) | ||
return mw.uri.decode(frame.args[1], frame.args[2] or "PATH") | return mw.uri.decode(frame.args[1], frame.args[2] or "PATH") | ||
end | |||
--Reverses a UTF8 string; equivalent to string.reverse | |||
function str.reverse(s) | |||
s = s:gsub(UTF8_char, function (c) return #c > 1 and c:reverse() end) | |||
return s:reverse() | |||
end | end | ||
return str | return str |