Difference between revisions of "Module:Ustring"

From blackwiki
Jump to navigation Jump to search
blackwiki>Uzume
(maintain error messages)
blackwiki>Pppery
(Can you not reinvent the wheel?)
Line 1: Line 1:
 
require('Module:No globals')
 
require('Module:No globals')
 
return setmetatable({}, {
 
return setmetatable({}, {
__index = function(t, f)
+
__index = function(t, k)
 +
local what = mw.ustring[k]
 +
if type(what) ~= "function" then
 +
return what
 +
end
 
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 i, v in ipairs(args) do
+
for _, v in ipairs(args) do
args[i] = tonumber(v) or v:gsub("^\\", "", 1)
+
args[_] = tonumber(v) or v:gsub("^\\", "", 1)
 
end
 
end
 
if args.tag then
 
if args.tag then
local tagargs = {}
+
local tag = {name = args.tag, content = mw.ustring[k](unpack(args)), args = {}}
for k, v in pairs(args) do
+
for x, y in pairs(args) do
if 'number' ~= type(k) and 'tag' ~= k then tagargs[k] = v end
+
if type(x) ~= 'number' and x ~= 'tag' then tag.args[x] = y end
 
end
 
end
return frame:extensionTag{name = args.tag, content = mw.ustring[f](unpack(args)), args = tagargs}
+
return frame:extensionTag(tag)
 
end
 
end
return (mw.ustring[f](unpack(args)))
+
return (mw.ustring[k](unpack(args)))
 
end
 
end
 
end
 
end
 
})
 
})

Revision as of 23:35, 30 April 2020

This module directly imports all functions from the

  1. 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

  1. redirect Template:Scribunto

Usage

{{#invoke:Ustring|function_name|arg1|arg2|...}} is equivalent to

  1. 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

&amp;#{{#invoke:ustring|char|49|48|59}}

produces:

&#10;

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

  1. redirect Template:Scribunto should be maintained, e.g.:

{{#invoke:Ustring|xyzzy}}

should produce:

Template:Script error

and

{{#invoke:Ustring|maxPatternLength}}

should produce:

Template:Script error

See also


require('Module:No globals')
return setmetatable({}, {
	__index = function(t, k)
		local what = mw.ustring[k]
		if type(what) ~= "function" then
			return what
		end
		return function(frame)
			local args = frame.args
			for _, v in ipairs(args) do
				args[_] = tonumber(v) or v:gsub("^\\", "", 1)
			end
			if args.tag then
				local tag = {name = args.tag, content = mw.ustring[k](unpack(args)), args = {}}
				for x, y in pairs(args) do
					if type(x) ~= 'number' and x ~= 'tag' then tag.args[x] = y end
				end
				return frame:extensionTag(tag)
			end
			return (mw.ustring[k](unpack(args)))
		end
	end
})