“模块:TemplateParameters2”的版本间的差异

来自中文乐谱库
wiki_>Xiplus
(已保护“模块:TemplateParameters2”:高風險模板:5644引用([编辑=仅管理员](无限期)[移动=仅管理员](无限期)))
 
(导入1个版本)
 

2020年7月3日 (五) 15:01的最新版本

此模块的文档可以在模块:TemplateParameters2/doc创建

脚本错误:Lua错误:无法创建进程:proc_open(/dev/null): failed to open stream: Operation not permitted

--[=[
以[[Module:TemplateParameters]]發想的模組
功能類似,但套用位置不同,傳遞方式也不一樣
部分地方為複製品,避免調用時效果走樣
]=]--
local p = {}
local lib_arg = {}
local yesno = require('Module:Yesno')
local lib_editstate = {}
local function errmsg(msg)
	return require('Module:Error').error({[1] = '[[Module:TemplateParameters2]]錯誤:' .. msg})
end

function p._pass_spstr2(template, arg_to_spstr, args)
	---- Module:TemplateParameters.pass_spstr 變種 ----
	local frame = mw.getCurrentFrame()
	local input_text = template
	local input_args = {}
	local input_title = mw.title.getCurrentTitle()
	local spilt_args = mw.text.split( arg_to_spstr, '<參數分隔/>')
	local k, v
	for _,x in pairs(spilt_args) do
		local text = mw.text.trim(x)
		if text ~= '' then
			if mw.ustring.gmatch(text, '<參數值/>') then
				local spilt_kv = mw.text.split( text, '<參數值/>')
				local spilt_list = {}
				k, v = mw.text.trim(spilt_kv[1]), spilt_kv[2]
				if k ~= '' then
					input_args[k] = v
				end
			end
		end
	end
	if args and type(args) == type(frame) then
		for k,v in pairs(spilt_args) do
			input_args[k] = v
		end
	end
	frame = frame:newChild{ title = tostring(mw.title.getCurrentTitle().fullText), args = input_args }
	return frame:preprocess({ text = input_text })
end

function p.pass_spstr2(frame)
	local args
	local template = '{{safesubst:'
	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.
		args = frame.args
		--local parent = frame:getParent().args
	else
		-- We're being called from another module or from the debug console, so assume
		-- the args are passed in directly.
		if type(frame) ~= type({}) then args = {frame}
		else args = frame end
	end
	if args['_TemplateTitle'] then
		template = template .. args['_TemplateTitle']
	end
	for k, v in pairs( args ) do
		if mw.text.trim(v) == '' then elseif k ~= '_args' or k ~= '_parent' or k ~= '_TemplateTitle' then
			local numberargs = tonumber(k)
			if template == '{{safesubst:' then
				template = template .. v
			elseif (mw.ustring.gmatch(args[1], '#invoke:') and k == 2) then
				template = template .. '|' .. v
			elseif ((not numberargs) or (numberargs ~= k) or (numberargs == k and mw.ustring.gmatch(v, '=')) ) then
				template = template .. '|' .. k .. '=' .. v
			else
				template = template .. '|' .. v
			end
		end
	end
	template = template .. '}}'
	--require('Module:Template invocation').invocation('foo', {'bar', 'baz', abc = 'def'})
	if ((type(parent) == nil) and (not args['_args']) and (not args['_parent'])) then
		return mw.getCurrentFrame():preprocess( template )
	end
	if (args['_args']) then
		pass_spstr_arg = args['_args']
	elseif (args['_parent']) then
		pass_spstr_arg = args['_parent']
	end
	--[=[
	return require('Module:TemplateParameters').pass_spstr({
		delnowiki = 0,
		delmsgnw = 0,
		code = template,
		TemplateTitle = args['_TemplateTitle'] or mw.title.getCurrentTitle(),
		args = pass_spstr_arg
	})]=]--
	return p._pass_spstr2(template, pass_spstr_arg, args)
end

function p.arg_to_spstr(frame)
	local args
	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.
		args = frame.args
	else
		-- We're being called from another module or from the debug console, so assume
		-- the args are passed in directly.
		if type(frame) ~= type({}) then args = {frame}
		else args = frame end
	end
	local wt = ''
	for k,v in pairs(args) do
		if wt == '' then
			wt = k .. '<參數值/>' ..v
		else
			wt = wt .. '<參數分隔/>' .. k .. '<參數值/>' ..v
		end
	end
	return wt
end

function p.arg_to_spstr2(frame)
	if frame ~= mw.getCurrentFrame() then
		return errmsg('arg_to_spstr2僅給解析<code>frame:getParent().args</code>用,無法套用在模組間接調用!')
	end
	local wt = ''
	for k,v in pairs(frame:getParent().args) do
		if wt == '' then
			wt = k .. '<參數值/>' ..v
		else
			wt = wt .. '<參數分隔/>' .. k .. '<參數值/>' ..v
		end
	end
	return wt
end

return p