本WIKI于2020.07.21由往事仇心创建,2021.12.25正式建组“空桑档案馆”,编辑权限逐步开放,建议各位少主收藏。
目前正在搭建基础框架与美工优化,欢迎翻阅已开放区域,并提出宝贵建议。
“空桑档案馆”搭建组持续招募ing,期待更多能人异士参与食物语WIKI建设。
反馈留言  ·  编辑教程  ·  收藏方法  ·  

全站通知:

模块:SplitFormat

来自食物语-档案馆WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

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

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