Module:Sandbox/Aidan9382

Source: Wikipedia, the free encyclopedia.

This is an old revision of this page, as edited by Aidan9382 (talk | contribs) at 15:29, 6 June 2023 (testing). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Sandboxes:


Any linter errors here are likely unintentional, and may be fixed

require('Module:Module wikitext')._addText('{{User:Aidan9382/sandbox/header|nolint=y}}')
require("strict")
local p = {}

function p.main()
	return "<div style=text-align:right;font-size:80%>This does nothing as of right now. "..os.time().."</div>"
end

function p.iloveregexold()
	local r = "%[?%[?.-:([^{|]+)%]?%]?"
	local args = {mw.ustring.match("[[File:Hey.jpg|thumb|left|250px]]",r)}
	return table.concat(args," -- ")
end
function p.iloveregex()
	--This assumes [[ is present, and that therefore ]] is present
	local r = "%[%[%a-:([^{|]+)(.+)%]%]"
	local args = {mw.ustring.match("[[File:Hey.jpg|thumb|left|250px|All for [[Module:Excerpt/portals]]|alt=text]]",r)}
	return table.concat(args," -- ")
end

function p.parentingLogic(frame)
	--Note: Not even sure this thing goes beyond 1 parent, but have overcomplicated code anyways
	local parentChain = {}
	local newframe = frame
	while true do
		local parent = newframe:getParent()
		if not parent then
			break
		end
		newframe = parent
		parentChain[#parentChain+1] = parent
	end
	local parentText = ""
	for i,p in next,parentChain do
		parentText = parentText .. " parent"..i.."="..p:getTitle()
	end
	return "frame="..frame:getTitle()..parentText.." mw.gct="..mw.title.getCurrentTitle().text
end

p["module-use"] = function(frame)
	local parentLogicStuff = p.parentingLogic(frame)
	local testlogic = "\n\n"
	local i = 1
	while true do
		local c = frame.args["_test"..i]
		if c then
			testlogic = testlogic .. "_test"..i.."="..c
		else
			break
		end
		i = i + 1
	end
	return parentLogicStuff..testlogic
end

function p.getargs(frame)
	local text = "Frame arguments:"
	for k,v in pairs(frame.args) do
		k,v = tostring(k), tostring(v)
		text = text .. "\n\n'<code>" .. mw.text.nowiki(mw.text.unstrip(k)) .. "</code>' = '<code>" .. mw.text.nowiki(mw.text.unstrip(v)) .. "</code>'"
	end
	return text
end

function p.testGP(frame)
	local s1 = "Frame args:"
	local s2 = "getParameters args:"
	local p1 = frame.args
	local p2 = require("Module:GetParameters").getParameters(p1, {"a", "b", "c"})
	for k,v in pairs(p1) do
		s1 = s1 .. "\n'" .. tostring(k) .. "' = '" .. v .. "'"
	end
	for k,v in pairs(p2) do
		s2 = s2 .. "\n'" .. tostring(k) .. "' = '" .. v .. "'"
	end
	return s1 .. "\n\n" .. s2 .. "\n\n\n"
end

function p.testframe(options) --For the Debug console - easy way into template entry points
	--To be invoked with the wanted arguments and page titles, or none for the default.
	--Setting .args after testframe() has been called is allowed but discouraged over simply
	--calling the function with provided arguments so as to match the real functionality
	local options = options or {}
	local base = mw.getCurrentFrame()

	local childArgs = options.args or {}
	local parentArgs = options.parentArgs or childArgs
	local childTitle = options.title or base:getTitle()
	local parentTitle = options.parentTitle

	local parentFrame = base:newChild({title=parentTitle, args=parentArgs})
	local childFrame = parentFrame:newChild({title=childTitle, args=childArgs})

	--Hide the extra parents, which would be unexpected outside of a debug setting
	parentFrame.getParent = function() return nil end
	--This 2nd hook is required, or else childFrame:getParent() somehow returns
	--the unmodified parent frame, which doesn't have the getParent hook. :/
	childFrame.getParent = function() return parentFrame end

	return childFrame
end

--[[ Note
Scribunto really doesn't want or like you doing things like what this function
aims to do (basically "hook" _G), and the outputs are completely not what you
would expect from the below code. Unsure how much of this is exactly "intended"
--]]
function p.globalExperiments()
	local _log = mw.log
	local _tostring = tostring
	local _type = type
	local _next = next
	local _pairs = pairs
	local _rawset = rawset
	local scanned = {}
	local function recursive(t, path)
		scanned[t] = true
		for a,b in _next,t do
		--for a,b in _pairs(t) do
			if _type(b) == "table" and not scanned[b] then
				recursive(b, path .. _tostring(a) .. ".")
			elseif _type(b) == "function" then
				_rawset(t, a, function(...)
					_log("(GHook) Just accessed " .. path .. _tostring(a))
					return b(...)
				end)
				_log("(GHook) Hooked " .. path .. _tostring(a))
			end
		end
	end
	recursive(_G, "")
end

function p.magicWordTests(frame)
	local args = frame.args
	if args.process == "y" then
		frame:preprocess("{{DEFAULTSORT:XYZ}}")
	elseif args.returnprocess == "y" then
		return frame:preprocess("{{DEFAULTSORT:XYZ}}")
	else
		return "{{DEFAULTSORT:XYZ}}"
	end
end

function p:namecall()
	return self
end

return setmetatable(p,{
	__index=function(t,k)
		mw.log("Attempted to access unknown function '"..tostring(k).."' from Module:Sandbox/Aidan9382\n"..debug.traceback())
		return function()
			return "<span class=\"error\">Attempted to access unknown function '"..tostring(k).."'</span>"
			--local frame = mw.getCurrentFrame()
			--local traceback = frame:extensionTag("syntaxhighlight", debug.traceback(), {lang="text"})
			--return "<span class=\"error\">Attempted to access unknown function '"..tostring(k).."'</span>\n"..traceback
		end
	end
})