Jump to content

Module:ArgRest/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
(Hopefully) adding support for aliases and whatnot
perhaps, I done it
Line 1: Line 1:
p = {}
p = {}

-- undo sanitization


function p.main(frame)
function p.main(frame)
Line 18: Line 16:
assert(secondParam, "second parameter missing")
assert(secondParam, "second parameter missing")
--[[
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
]]--
-- Helper for replaceTripleBraces: reads through "default" to find aliases and the actual default
-- Helper for replaceTripleBraces: reads through "default" to find aliases and the actual default
local function recurseOverBraces(str2, i)
local function recurseOverBraces(str2, i)
local alias, _, default = str:match("([^{}<>|]+)(|?(.*))")
local alias, _, default = str2:match("([^{}<>|]+)(|?(.*))")
if default == "" then default = nil end
if _ == "" then default = nil end
if default.match("{{%b{}}}") then
if default and default:match("{{%b{}}}") then
return frame:getParent().args[alias:gsub("%d+", tostring(i))] or recurseOverBraces(default:sub(-4, 4), i)
return frame:getParent().args[alias:gsub("%d+", tostring(i))] or recurseOverBraces(default:sub(-4, 4), i)
else
else
Line 39: Line 30:
---- Since %b{} doesn't allow fine control, we must do a second search within the matched string
---- Since %b{} doesn't allow fine control, we must do a second search within the matched string
local parameter, _, default = str:match("([^{}<>|]+)(|?(.*))")
local parameter, _, default = str:match("([^{}<>|]+)(|?(.*))")
if default == "" then default = nil elseif default.match("{{%b{}}}") then
if _ == "" then default = nil elseif default:match("{{%b{}}}") then
default = recurseOverBraces(default:sub(-4, 4), i)
default = recurseOverBraces(default:sub(-4, 4), i)
end
end
return frame:getParent().args[parameter:gsub("%d+", tostring(i))] or default or "{{{" .. parameter .. "}}}"
return frame:getParent().args[parameter:gsub("%d+", tostring(i))] or default or "{{{" .. parameter .. "}}}"
end
end
Line 50: Line 43:
end
end
-- local processed = wikitext:gsub("{{{([^{}<>|]+)(|?([^{}<>|]*))}}}", function(a, b, c) return replaceTripleBraces(a, b, c, i) end)
-- local processed = wikitext:gsub("{{{([^{}<>|]+)(|?([^{}<>|]*))}}}", function(a, b, c) return replaceTripleBraces(a, b, c, i) end)
local processed = wikitext:gsub("{{%b{}}}", function(a) return replaceTripleBraces(a:sub(-4, 4), i) end) --matches everything in triple braces
local processed = wikitext:gsub("%{%{(%b{})%}%}", function(a) return replaceTripleBraces(a:sub(1, -2), i) end) --matches everything in triple braces
result = result .. processed
result = result .. processed
end
end

Revision as of 20:56, 1 April 2023

p = {}

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 = ''
    
    assert(secondParam, "second parameter missing")
    
    -- Helper for replaceTripleBraces: reads through "default" to find aliases and the actual default
    local function recurseOverBraces(str2, i)
    	local alias, _, default = str2:match("([^{}<>|]+)(|?(.*))")
    	if _ == "" then default = nil end
    	if default and default:match("{{%b{}}}") then
    		return frame:getParent().args[alias:gsub("%d+", tostring(i))] or recurseOverBraces(default:sub(-4, 4), i)
    	else
    		return frame:getParent().args[alias:gsub("%d+", tostring(i))] or default or "{{{" .. alias .. "}}}" 
    	end
    end
    local function replaceTripleBraces(str, i) -- extract corresponding arguments from the parent function.
    	---- Since %b{} doesn't allow fine control, we must do a second search within the matched string
    	local parameter, _, default = str:match("([^{}<>|]+)(|?(.*))")
    	
    	if _ == "" then default = nil elseif default:match("{{%b{}}}") then
    		default = recurseOverBraces(default:sub(-4, 4), 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)
        local processed = wikitext:gsub("%{%{(%b{})%}%}", function(a) return replaceTripleBraces(a:sub(1, -2), i) end)  --matches everything in triple braces
        result = result .. processed
    end
    
    return frame:preprocess(result)
end

return p