模块:SplitFormat
跳转到导航
跳转到搜索
apply_template2
用法:
- str:按固定格式书写要批量应用模板的数据
- 使用的分隔符按下方定义为准
- sep0:第一层级(各单个数据之间的)分隔符
- sep1:第二层级(各数据传入参数的)分隔符
- 当该组数据需要独自传入参数,或者无法使用下面的 other 传入统一参数,则需要通过 <参数名>=<值> 显式声明所使用参数的所有的参数名
- sep2:第三层级(各单个数据应用模板后,在结果之间插入的)分隔符
- template:要应用的模板名
- other:对所有单个数据应用模板时统一传入相同参数
以下是两个样例:
{{#invoke:splitFormat|apply_template2|str=RMA70-24:罕见,至纯源石:三星获得|sep0=,|sep1=:|sep2=+|template=关卡报酬|other=:3=60px}}+
{{#invoke:splitFormat|apply_template2|str=:1=能天使:shop=1,:1=德克萨斯:shop2=1|sep0=,|sep1=:|sep2=|template=干员头像|other=:2=60px:elite=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,'<','<');
str=string.gsub(str,'>','>');
str=string.gsub(str,'"','"');
str=string.gsub(str,"'",''');
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