Difference between revisions of "Module:Ustring"
blackwiki>Uzume (Invert: dynamically import any function from mw.ustring upon demand instead of traversing and importing all functions from mw.ustring at every invocation since only one can actually be called at a time; even Module:LuaCall does this better passing just the single name to access instead of attempting to traverse and export an entire namespace like _G (which is clearly infeasible)) |
blackwiki>Uzume (maintain error messages) |
||
| Line 1: | Line 1: | ||
require('Module:No globals') | require('Module:No globals') | ||
return setmetatable({}, { | return setmetatable({}, { | ||
| − | __index = function(t, | + | __index = function(t, f) |
return function(frame) | return function(frame) | ||
| + | if nil == mw.ustring[f] then | ||
| + | return '<strong class="error">'..mw.message.new('scribunto-common-nosuchfunction', '', f):plain()..'</strong>' | ||
| + | end | ||
| + | if 'function' ~= type(mw.ustring[f]) then | ||
| + | return '<strong class="error">'..mw.message.new('scribunto-common-notafunction', '', f):plain()..'</strong>' | ||
| + | end | ||
local args = frame.args | local args = frame.args | ||
| − | for | + | for i, v in ipairs(args) do |
| − | args[ | + | args[i] = tonumber(v) or v:gsub("^\\", "", 1) |
end | end | ||
if args.tag then | if args.tag then | ||
| − | local | + | local tagargs = {} |
| − | for | + | for k, v in pairs(args) do |
| − | if | + | if 'number' ~= type(k) and 'tag' ~= k then tagargs[k] = v end |
end | end | ||
| − | return frame:extensionTag( | + | return frame:extensionTag{name = args.tag, content = mw.ustring[f](unpack(args)), args = tagargs} |
end | end | ||
| − | return (mw.ustring[ | + | return (mw.ustring[f](unpack(args))) |
end | end | ||
end | end | ||
}) | }) | ||
Revision as of 20:08, 30 April 2020
| 40x40px | This module is rated as ready for general use. It has reached a mature form and is thought to be bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
This module directly imports all functions from the
- redirect Template:Scribunto library. Documentation for each function can be found there.
The module takes an indefinite number of arguments. All arguments are coerced as number type if possible. If you wish for something to remain a string, you can simply escape it by insert \ at the beginning of the string.
You can also wrap results in tags. All non-number indexed arguments will be passed to
- redirect Template:Scribunto
Contents
Usage
{{#invoke:Ustring|function_name|arg1|arg2|...}} is equivalent to
- redirect Template:Scribunto
Example using mw.ustring.sub
{{#invoke:Ustring|sub|abcde|2|4}}
produces:
bcd
Example using mw.ustring.gsub
{{#invoke:Ustring|gsub|1234|23|}}
produces:
14
Example using mw.ustring.char
&#{{#invoke:ustring|char|49|48|59}}
produces:
This is nice for escaping character sequences in other modules; for example:
p = {}
function p.main(frame)
local str = '1-2-2{{#invoke:ustring|char|45}}1-3'
return frame:preprocess(str:gsub('-', '/'))-- == '1/2/2-1/3'
end
return p
Example using mw.ustring.match
{{#invoke:Ustring|match|abcde|(c%w)}}
produces:
cd
Note: Only the first match is returned. Additional returns are omitted because mw.ustring.gsub's second return value is generally undesirable.
Example using tag arguments
{{#invoke:Ustring|match
|{{Module:Ustring}}|%s%s%sif%snot%s[^%s]+%sthen.+%
<!--enter an actual newline character to match '\n'-->%s%s%send
|tag=syntaxhighlight|lang=lua}}
produces:
Note that:
<syntaxhighlight lang="lua">{{#invoke:Ustring|match
|{{Module:Ustring}}|%s%s%sif%snot%s[^%s]+%sthen.+%
<!--enter an actual newline character to match '\n'-->%s%s%send}}</syntaxhighlight>
produces:
{{#invoke:Ustring|match
|{{Module:Ustring}}|%s%s%sif%snot%s[^%s]+%sthen.+%
<!--enter an actual newline character to match '\n'-->%s%s%send}}
Errors
Errors from accessing
- redirect Template:Scribunto should be maintained, e.g.:
{{#invoke:Ustring|xyzzy}}
should produce:
and
{{#invoke:Ustring|maxPatternLength}}
should produce:
See also
require('Module:No globals')
return setmetatable({}, {
__index = function(t, f)
return function(frame)
if nil == mw.ustring[f] then
return '<strong class="error">'..mw.message.new('scribunto-common-nosuchfunction', '', f):plain()..'</strong>'
end
if 'function' ~= type(mw.ustring[f]) then
return '<strong class="error">'..mw.message.new('scribunto-common-notafunction', '', f):plain()..'</strong>'
end
local args = frame.args
for i, v in ipairs(args) do
args[i] = tonumber(v) or v:gsub("^\\", "", 1)
end
if args.tag then
local tagargs = {}
for k, v in pairs(args) do
if 'number' ~= type(k) and 'tag' ~= k then tagargs[k] = v end
end
return frame:extensionTag{name = args.tag, content = mw.ustring[f](unpack(args)), args = tagargs}
end
return (mw.ustring[f](unpack(args)))
end
end
})