Module:Test2: Difference between revisions

From Elwiki
No edit summary
No edit summary
Line 2: Line 2:


function p.Patch(frame)
function p.Patch(frame)
   local origTemplateRaw = frame.args.orig
   local origArgs = {}
   local origTemplate = mw.text.unstrip(origTemplateRaw)
  local orig = frame.args.orig or ""
   orig = orig:gsub("<nowiki>(.-)</nowiki>", function(args)
    for k, v in args:gmatch("|%s*(.-)%s*=%s*(.-)%s*|") do
      origArgs[k] = v
    end
    return ""
  end)
 
   local newArgs = {}
   local newArgs = {}
   for k, v in pairs(frame.args) do
   for k, v in pairs(frame.args) do
Line 10: Line 17:
     end
     end
   end
   end
  -- Extract the old arguments from the orig template
  local oldArgs = {}
  string.gsub(origTemplate, "%|%s*(%w+)%s*=%s*(.-)%s*", function(k, v)
    oldArgs[k] = v
  end)


   -- Merge the old and new arguments
   -- Merge the old and new arguments
   local mergedArgs = {}
   local mergedArgs = {}
   for k, v in pairs(oldArgs) do
   for k, v in pairs(origArgs) do
     mergedArgs[k] = v
     mergedArgs[k] = v
   end
   end
Line 26: Line 27:
   end
   end


   -- Create the new template string with the merged arguments
   -- Call the Damage template with the merged arguments
   local newTemplate = "{{" .. string.match(origTemplate, "{{%s*(.-)%s*}}") .. "|"
   local damageTemplate = mw.getCurrentFrame():expandTemplate{ title = "Damage", args = mergedArgs }
  for k, v in pairs(mergedArgs) do
    newTemplate = newTemplate .. k .. "=" .. v .. "|"
  end
  newTemplate = newTemplate:sub(1, -2) .. "}}"
 
  -- Expand the new template
  local newTemplateExpanded = frame:expandTemplate{ title = mw.title.getCurrentTitle().text, args = mw.text.split(newTemplate, "|", true) }


   return newTemplateExpanded
   return damageTemplate
end
end


return p
return p

Revision as of 20:01, 25 April 2023

Documentation for this module may be created at Module:Test2/doc

local p = {}

function p.Patch(frame)
  local origArgs = {}
  local orig = frame.args.orig or ""
  orig = orig:gsub("<nowiki>(.-)</nowiki>", function(args)
    for k, v in args:gmatch("|%s*(.-)%s*=%s*(.-)%s*|") do
      origArgs[k] = v
    end
    return ""
  end)

  local newArgs = {}
  for k, v in pairs(frame.args) do
    if k ~= "orig" then
      newArgs[k] = v
    end
  end

  -- Merge the old and new arguments
  local mergedArgs = {}
  for k, v in pairs(origArgs) do
    mergedArgs[k] = v
  end
  for k, v in pairs(newArgs) do
    mergedArgs[k] = v
  end

  -- Call the Damage template with the merged arguments
  local damageTemplate = mw.getCurrentFrame():expandTemplate{ title = "Damage", args = mergedArgs }

  return damageTemplate
end

return p