跳转到内容

模块:TemplateArgPassingTool

本页使用了标题或全文手工转换
被永久保护的模块
维基百科,自由的百科全书

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