Jump to content

Module:Ustring/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
sync
remove new 's<n>' args from "tagargs"
Line 1: Line 1:
require('Module:No globals')
return setmetatable({}, {
return setmetatable({}, {
__index = function(t, k)
__index = function(t, k)
Line 6: Line 7:
end
end
return function(frame)
return function(frame)
local fargs = frame.args
local fargsused = { tag = true }
local args = {}
local args = {}
local str_i = 1
local str_i = 1
while frame.args['s' .. str_i] do
while fargs['s' .. str_i] do
args[str_i] = frame.args['s' .. str_i]
fargsused['s' .. str_i] = true
args[str_i] = fargs['s' .. str_i]
str_i = str_i + 1
str_i = str_i + 1
end
end
for i, v in ipairs(frame.args) do
for i, v in ipairs(fargs) do
fargsused[i] = true
args[i + str_i - 1] = tonumber(v) or v:gsub("^\\", "", 1)
args[i + str_i - 1] = tonumber(v) or v:gsub("^\\", "", 1)
end
end
if not frame.args.tag then
if not fargs.tag then
return (what(unpack(args))) -- Outside parens truncate to first result avoiding tail call
-- These brackets are important! Without them gsub will return multiple arguments.
return (what(unpack(args)))
end
end
local tagargs = {}
local tagargs = {}
for x, y in pairs(frame.args) do
for x, y in pairs(fargs) do
if type(x) ~= 'number' and x ~= 'tag' then tagargs[x] = y end
if not fargsused[x] then tagargs[x] = y end
end
end
return frame:extensionTag{name = frame.args.tag, content = what(unpack(args)), args = tagargs}
return frame:extensionTag{name = fargs.tag, content = what(unpack(args)), args = tagargs}
end
end
end
end

Revision as of 19:51, 11 November 2021

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 fargs = frame.args
			local fargsused = { tag = true }
			local args = {}
			local str_i = 1
			while fargs['s' .. str_i] do
				fargsused['s' .. str_i] = true
				args[str_i] = fargs['s' .. str_i]
				str_i = str_i + 1
			end
			for i, v in ipairs(fargs) do
				fargsused[i] = true
				args[i + str_i - 1] = tonumber(v) or v:gsub("^\\", "", 1)
			end
			if not fargs.tag then
				return (what(unpack(args)))		-- Outside parens truncate to first result avoiding tail call
			end
			local tagargs = {}
			for x, y in pairs(fargs) do
				if not fargsused[x] then tagargs[x] = y end
			end
			return frame:extensionTag{name = fargs.tag, content = what(unpack(args)), args = tagargs}
		end
	end
})