Module:TemplateArgPassingTool

From wiki
Revision as of 20:51, 11 November 2023 by 1>Xiplus-abot (已保护“Module:TemplateArgPassingTool”:​高風險模板:1192引用<!-- 機器人3 -->([编辑=仅允许自动确认用户](无限期)[移动=仅允许自动确认用户](无限期)))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

local p={} local lib_arg={}

local module_pattern = "#%s*[Ii][Nn][Vv][Oo][Kk][Ee]%s*:%s*(.+)%s*%|%s*(.+)"

function p.passArgs(frame) return p.passNArgs(frame, true) end

function p.passEscapeArgs(frame)

   local args, working_frame
   if frame == mw.getCurrentFrame() then
       -- We're being called via #invoke. The args are passed through to the module
       -- from the template page, so use the args that were passed into the template.
       if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
       args = lib_arg.getArgs(frame, {parentFirst=true})
       working_frame = frame
   else
       -- We're being called from another module or from the debug console, so assume
       -- the args are passed in directly.
       args = frame
       working_frame = mw.getCurrentFrame()
       if type(args) ~= type({}) then args = {frame} end
   end
   local template_name =  .. (args.template_name or 'void')
   local module_name, function_name = nil,nil
   if mw.ustring.find(template_name, module_pattern) then

mw.ustring.gsub(template_name, module_pattern, function(str, fun)module_name, function_name = str, fun return str end)

   end
   local escape_args = {}
   for key, value in pairs(args) do
   	local arg_value = value
   	if mw.isSubsting() then

arg_value = mw.ustring.gsub(value, "[=%|%{%}]", function(esc_str) local mapping = {} mapping['|']='!' mapping['=']='=' mapping['{']='(' mapping['}']=')' if mapping[esc_str] then return "{{" .. mapping[esc_str] .. "}}" end return esc_str end) end

   	escape_args[key] = mw.text.encode(arg_value)
   end
   local body_frame = working_frame:newChild{ title = module_name or template_name, args = escape_args }

if module_name ~= nil then module_body = require("Module:"..module_name) if module_body ~= nil then func_body = module_body[function_name] if func_body ~= nil then return func_body(body_frame) end end return end return body_frame:expandTemplate{ title = template_name, args = escape_args } end

function p.passNArgs(frame, no_skip)

   local args, working_frame
   if frame == mw.getCurrentFrame() then
       -- We're being called via #invoke. The args are passed through to the module
       -- from the template page, so use the args that were passed into the template.
       if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
       args = lib_arg.getArgs(frame, {parentFirst=true})
       working_frame = frame
   else
       -- We're being called from another module or from the debug console, so assume
       -- the args are passed in directly.
       args = frame
       working_frame = mw.getCurrentFrame()
       if type(args) ~= type({}) then args = {frame} end
   end
   local skip_args = no_skip and 0 or (tonumber(args.skip) or 1)
   local template_name =  .. (args.template_name or 'void')
   local module_name, function_name = nil,nil
   if mw.ustring.find(template_name, module_pattern) then

mw.ustring.gsub(template_name, module_pattern, function(str, fun)module_name, function_name = str, fun return str end)

   end
   local sikp_args = {}
   for key, value in pairs(args) do
   	local i = tonumber(key)
   	local new_key = key
   	local skip_flag = false
   	if i ~= nil then
   		if i > 0 then

if i <= skip_args then skip_flag = true end new_key = i - skip_args

   		end
   	end
   	if not skip_flag then sikp_args[new_key] = value end
   end
   local body_frame = working_frame:newChild{ title = module_name or template_name, args = sikp_args }

if module_name ~= nil then module_body = require("Module:"..module_name) if module_body ~= nil then func_body = module_body[function_name] if func_body ~= nil then return func_body(body_frame) end end return end return body_frame:expandTemplate{ title = template_name, args = sikp_args } end

return p