模块:SplitFormat

来自节奏医生中文百科
跳到导航 跳到搜索
这个模块引用自PRTS,感谢所有编辑们做出的贡献。

参数解释

调用函数 apply_template2,该函数拥有以下参数

str 需要模块进行分隔的字符串
sep0 将 str 分隔为不同组字符串的符号
sep1 将 sep0 分隔后的字符串进一步分隔的符号
sep2 分隔各组字符串之间的符号
template 分隔后各组字符串所应用的模板
other 额外指定所套模板的参数设置,使用 sep1 指定的分隔符号

代码实例

{{#invoke:splitFormat|apply_template2|str=RMA70-24:罕见,至纯源石:三星获得|sep0=,|sep1=:|sep2=sep2|template=关卡报酬|other=:3=60px}}

模块执行过程中,现将 str 按 sep0 进行第一次分隔分组。在此,即为按逗号分隔

分隔为:

第一组 RMA70-24:罕见
第二组 至纯源石:三星获得

然后,模块将各个组再按 sep1 进行分隔,对于第一组来说,即为:

1.a组 RMA70-24
1.b组 罕见

当 other 指定了参数时,按前面 sep1 指定的符号将其分隔并组合到各组字符串中,最终:

第一组 RMA70-24|罕见|3=60px
第二组 至纯源石|三星获得|3=60px

然后将分离出的各组字符串分别套入指定的模板 关卡报酬 内

关卡报酬模板参数:{{关卡报酬|<道具名>|<掉落标签>|<图片尺寸>}}

输出:

第一组:{{关卡报酬|RMA70-24|罕见|3=60px}}
第二组:{{关卡报酬|至纯源石|三星获得|3=60px}}

最后,将得到的两组字符串用 sep2 指定的符号组合起来,输出如下结果:

{{关卡报酬|RMA70-24|罕见|3=60px}}<br>{{关卡报酬|至纯源石|三星获得|3=60px}}

该模板还可有如下进阶用法

{{#invoke:splitFormat|apply_template2|str=:家具=yes:1=饰牌《酬劳》:2=首次掉落,至纯源石:三星获得|sep0=,|sep1=:|sep2=<br>|template=关卡报酬|other=:3=60px}}
此处使用的模板参数:{{关卡报酬|<家具=1>|<家具名>|<掉落标签>|<图片尺寸>}} 其中后三个参数为数字参数,未命名

其中,1= 2= 为指定需要传递到模板的数字参数


local p = {}

function p.split(frame)
	local args = (frame == mw.getCurrentFrame() and frame.args) or frame
	
	local sep0 = (args.sep0 or ",")
	local sep1 = (args.sep1 or ",")
	local fmt = (args.fmt or "$")
	
	local res = {}
	local func = function(w)
		local w = string.gsub(w, "^%s*(.+)%s*$", "%1")
		w = string.gsub(fmt, "%$", w)
		table.insert(res, w)
	end
	
	local expand_tem = function()
		if args.template == "" then
			for i = 1, #res do res[i] = frame:expandTemplate{ title = res[i], args = {} } end
		elseif args.template then
			for i = 1, #res do res[i] = frame:expandTemplate{ title = args.template, args = { res[i] } } end
		end
	end
	
	string.gsub(args.str, string.format("([^%s]+)", sep0), func)
	expand_tem()
	
	return table.concat(res, sep1)
end

function p.apply_template(frame)
	local args = (frame == mw.getCurrentFrame() and frame.args) or frame
	
	local res = {}
	local func = function(w)
		local w = string.gsub(w, "^%s*(.+)%s*$", "%1")
		if string.find(w, "=") ~= nil then
			local eq = string.find(w, "=")
			res[string.sub(w, 1, eq - 1)] = string.sub(w, eq + 1)
		end
	end
	
	string.gsub(args.params, string.format("([^%s]+)", "|"), func)
	return frame:expandTemplate{ title = args.template, args = res }
end

function p.enhance_icons(frame)
	local args = (frame == mw.getCurrentFrame() and frame.args) or frame
	
	local res = {}
	local func = function(w)
		local i, t
		w = string.gsub(w, "^%s*(.+)%s*$", "%1")
		i, t = string.match(w, '^(%d+)(%D+)$')
		if t == "宝具" or t == "技能" then
			table.insert(res, {i, t})
		end
	end
	
	string.gsub(args.str, "([^,]+)", func)
	local size_px = (#res > 7) and 46 or 60
	
	res_str = ""
	for i = #res, 1, -1 do res_str = res_str .. frame:expandTemplate{ title = "从者强化图标", args = { res[i][1], res[i][2], ["size"] = size_px } } end
	
	return res_str
end

function p.del_smw(frame)
	local args = (frame == mw.getCurrentFrame() and frame.args) or frame
	
	local res = (args.str or "")
	res = string.gsub(res, "%[%[SMW::on%]%]", "")
	res = string.gsub(res, "%[%[SMW::off%]%]", "")
	
	return res
end

function p.str_decode(frame)
	local args = (frame == mw.getCurrentFrame() and frame.args) or frame
	return mw.text.decode(args.str, true)
end

function p.apply_template2(frame)
	local args = (frame == mw.getCurrentFrame() and frame.args) or frame
	local sep0 = (args.sep0 or ",")
	local sep1 = (args.sep1 or ",")
	local sep2 = (args.sep2 or ",")
	local other = {}
	local func = function(w)
		local w = string.gsub(w, "^%s*(.+)%s*$", "%1")
		if string.find(w, "=") ~= nil then
			local eq = string.find(w, "=")
			other[string.sub(w, 1, eq - 1)] = string.sub(w, eq + 1)
		end
	end
	local res={}
	local sdebug=''
	for i,v in ipairs(mw.text.split(args.str,sep0,true)) do
		other={}
		string.gsub(args.other, string.format("([^%s]+)", sep1), func)
		local temp ={}
		if string.find(v,'=')~=nil then
			temp = other
			string.gsub(v, string.format("([^%s]+)", sep1), func)
		else
			temp = mw.text.split(v,sep1,true)
		end
		for k, v in pairs( other ) do
        	temp[k] = v
    	end
		table.insert(res,frame:expandTemplate{title=args.template,args=temp})
	end
	return  table.concat(res,sep2)
end

--这个函数用于在set前将html tag屏蔽
function p.killHtml(frame)
	local args=(frame==mw.getCurrentFrame()and frame.args)or frame
	local str=args.str
	str=string.gsub(str,'<','&lt;');
	str=string.gsub(str,'>','&gt;');
	str=string.gsub(str,'"','&quot;');
	str=string.gsub(str,"'",'&apos;');
	return str
end

--替换参数
function p.replace(frame)
	local args=(frame==mw.getCurrentFrame()and frame.args)or frame
	local str=args.str
	local rep=args.rep
	str=string.gsub(str,rep,'');
	return str
end

return p