模块:SplitFormat

来自Mooncell - 玩家共同构筑的FGO中文Wiki
跳到导航 跳到搜索

可在模块: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 = mw.ustring.gsub(w, "^%s*(.+)%s*$", "%1")
		w = mw.ustring.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
	
	mw.ustring.gsub(args.str, mw.ustring.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 = mw.ustring.gsub(w, "^%s*(.+)%s*$", "%1")
		if mw.ustring.find(w, "=") ~= nil then
			local eq = mw.ustring.find(w, "=")
			res[mw.ustring.sub(w, 1, eq - 1)] = mw.ustring.sub(w, eq + 1)
		end
	end
	
	mw.ustring.gsub(args.params, mw.ustring.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 = mw.ustring.gsub(w, "^%s*(.+)%s*$", "%1")
		i, t = mw.ustring.match(w, '^(%d+)(%D+)$')
		if t == "宝具" or t == "技能" then
			table.insert(res, {i, t})
		end
	end
	
	mw.ustring.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 = mw.ustring.gsub(res, "%[%[SMW::on%]%]", "")
	res = mw.ustring.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

return p