Jump to content

Module:ArgRest

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Lemondoge (talk | contribs) at 18:55, 29 March 2023 (Major improvements; likely finished version). 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