Jump to content

Module:Ustring: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
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)
maintain error messages
Line 1: Line 1:
require('Module:No globals')
require('Module:No globals')
return setmetatable({}, {
return setmetatable({}, {
__index = function(t, k)
__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 _, v in ipairs(args) do
for i, v in ipairs(args) do
args[_] = tonumber(v) or v:gsub("^\\", "", 1)
args[i] = 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 x, y in pairs(args) do
for k, v in pairs(args) do
if type(x) ~= 'number' and x ~= 'tag' then tag.args[x] = y end
if 'number' ~= type(k) and 'tag' ~= k then tagargs[k] = v end
end
end
return frame:extensionTag(tag)
return frame:extensionTag{name = args.tag, content = mw.ustring[f](unpack(args)), args = tagargs}
end
end
return (mw.ustring[k](unpack(args)))
return (mw.ustring[f](unpack(args)))
end
end
end
end

Revision as of 20:08, 30 April 2020

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
})