跳转到内容

模組:TemplateArgPassingTool

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

这是本页的一个历史版本,由A2569875留言 | 贡献2023年11月11日 (六) 04:26 建立内容为“local p={} local lib_arg={} 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})…”的新页面)编辑。这可能和当前版本存在着巨大的差异。

(差异) ←上一修订 | 最后版本 (差异) | 下一修订→ (差异)

local p={}
local lib_arg={}

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, "#[Ii][Nn][Vv][Oo][Kk][Ee]:.+%|.+") then
		mw.ustring.gsub(template_name, "#[Ii][Nn][Vv][Oo][Kk][Ee]:(.+)%|(.+)", 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)
    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 skip_args = tonumber(args.skip) or 1
    local numberic_args = {}
    local text_args = {}
    local min_args, max_args = tonumber("inf"), tonumber("-inf")
    for key, value in pairs(args) do
    	local i = tonumber(key)
    	local check = mw.text.trim(key):lower()
    	local 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)
    	if i ~= nil and check~='inf' and check~='-inf' and check~='nan' and check~='-nan' then
    		numberic_args[i] = arg_value
    		if i > max_args then max_args = i end
    		if i < min_args then min_args = i end
    	else
    		text_args[key] = arg_value
    	end
    end
    
	if mw.isSubsting() then
		template_name = mw.ustring.gsub(template_name, "%{%{!%}%}", "|")
	end
    local body = "{{" .. template_name
    local continuous = true
    for i = min_args, max_args do
    	if numberic_args[i] ~= nil then
    		if i <= 0 or i > skip_args then
	    		local value = numberic_args[i]
		    	body = body .. '|'
		    	if i <= 0 or not continuous then
		    		body = body .. (i - ((i > 0) and skip_args or 0)) .. '='
		    	end
		    	body = body .. value
    		end
	    else
	    	if i > skip_args then continuous = false end
    	end
    end
    for key, value in pairs(text_args) do
    	body = body .. '|' .. key .. '=' .. value
    end
    body = body .. '}}'
    return working_frame:preprocess(body)
end

return p