Jump to content

Module:ArgRest/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Lemondoge (talk | contribs) at 20:00, 30 March 2023 (Apparently, the old code had something this needed). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
p = {}

---- Below is code partially borrowed from Module:Demo, because I needed a way to reverse nowiki sanitization (and I'm too scared to remove disinherit lol) ----
--creates a frame object that cannot access any of the parent's args
--unless a table containing a list keys of not to inherit is provided
function disinherit(frame)
	local orphan = frame:newChild{}
	orphan.getParent = frame.getParent --returns nil
	orphan.args = {}
	return orphan
end

-- undo sanitization
function p.get(frame)
	local orphan = disinherit(frame)
	local code = frame.args[1] or ''
	if code:match'nowiki' then
		code = mw.text.unstripNoWiki(code)
	end
	return orphan:preprocess(code)
end

function p.main(frame)
	local wikitext = p.get(frame):gsub("&lt;", "<"):gsub("&gt;", ">") -- angle brackets are still sanitized
	local secondParam = frame.args[2] or '' 
    local start = tonumber(secondParam:match('(%d+)')) -- Extract the first number from the second parameter
    local result = ''
    
    local function replaceTripleBraces(parameter, _, default, i) -- extract corresponding arguments from the parent function. the _ is necessary because the pipe still gets caught in the second capture group
		return frame:getParent().args[parameter:gsub("%d+", tostring(i))] or default
    end 
    
    for i = start, math.huge do
        -- Check if the parameter is defined
        if not frame:getParent().args[secondParam:gsub('%d+', tostring(i))] then 
        	break
        end

        local processed = wikitext:gsub("{{{([^{}<>|]+)(|?([^{}<>|]*))}}}", function(a, b, c) return replaceTripleBraces(a, b, c, i) end) -- Someday, I'm going to go to programmer prison for doing... this, and I'm okay with that.
        result = result .. processed
    end
    
    return result
end

return p
--[[
p = {}

function p.main(frame)
	local code = frame.args[1] or ''
	local secondParam = frame.args[2]
	
	if code:match'nowiki' then -- undo nowiki sanitization
		code = mw.text.unstripNoWiki(code)
	else return error("<nowiki> tags missing from first parameter") end
	
	if not secondParam then return error("second parameter missing") end -- Check for second parameter
	
	local wikitext = frame:preprocess(code):gsub("&lt;", "<"):gsub("&gt;", ">") -- angle brackets are still sanitized; undo it
    local start = tonumber(secondParam:match('%d+')) -- Extract the first number from the second parameter
    local result = ''
    
    local function replaceTripleBraces(parameter, _, default, i) -- extract corresponding arguments from the parent function. the _ is necessary because the pipe still gets caught in the second capture group
    	if tonumber(parameter) then
    		result = result .. "abc"
    		return frame:getParent().args[i] or default or "{{{" .. tostring(i) .. "}}}"
    	end
		return frame:getParent().args[parameter:gsub("%d+", tostring(i))] or default or "{{{" .. parameter .. "}}}"
    end
    
    for i = start, math.huge do
        -- Check if the parameter is defined
        if not frame:getParent().args[secondParam:gsub('%d+', tostring(i))] then 
        	break
        end

        local processed = wikitext:gsub("{{{([^{}<>|]+)(|?([^{}<>|]*))}}}", function(a, b, c) return replaceTripleBraces(a, b, c, i) end) -- Find stuff of the form {{{parameter}}} or {{{parameter|default}}} via pattern matching
        result = result .. processed
    end
    
    return result
end

return p
]]--