Module:ArgRest and Module:ArgRest/sandbox: Difference between pages

(Difference between pages)
Page 1
Page 2
imported>Lemondoge
enclose the pipe capture in a separate thingamajig
 
imported>Lemondoge
enclose pipe capturing in separate capture group
 
Line 17: Line 17:
     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
    -- Helper for replaceTripleBraces: reads through "default" to find aliases and the actual default
     if _ == "" then default = nil end -- mildly ugly hack for checking for {{{parameter|}}}
     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:match("{{(%b{})}}"):sub(1, -2), 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:match("{{(%b{})}}"):sub(1, -2), i)
    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
   
     for i = start, math.huge do
     for i = start, math.huge do
         -- Check if the parameter is defined
         -- Check if the parameter is defined. TODO: make this check for aliases
         if not frame:getParent().args[secondParam:gsub('%d+', tostring(i))] then  
         if not frame:getParent().args[secondParam:gsub('%d+', tostring(i))] then  
         break
         break
         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) -- Find stuff of the form {{{parameter}}} or {{{parameter|default}}} via pattern matching
        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