Jump to content

Module:ArgRest/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
looks like it can just be preprocessed at the end; passes all of my testcases that way. Don't think it can be used to summon parameters, unfortunately
m removed useless pair of parentheses
Line 18: Line 18:
local secondParam = frame.args[2]
local secondParam = frame.args[2]
local start = tonumber(secondParam:match('(%d+)')) -- Extract the first number from the second parameter
local start = tonumber(secondParam:match('%d+')) -- Extract the first number from the second parameter
local result = ''
local result = ''

Revision as of 20:11, 31 March 2023

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

-- undo sanitization

function p.main(frame)
	-- Undo sanitization:
	local code = frame.args[1] or ''
	if code:match'nowiki' then
		code = mw.text.unstripNoWiki(code)
	else error("<nowiki> missing from first parameter") end
	
	-- Angle brackets still remain santiized; unsanitize them
	local wikitext = code:gsub('&lt;', '<'):gsub('&gt;', '>')
	
	local secondParam = frame.args[2]
    local start = tonumber(secondParam:match('%d+')) -- Extract the first number from the second parameter
    local result = ''
    
    if not secondParam then
    	error("second parameter missing")
    end
    
    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 default == "" then default = nil 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)
        result = result .. processed
    end
    
    return frame:preprocess(result)
end

return p