模組:TryError
外观
local p={}
local lib_arg={};
function p.try(frame)
local args, working_frame
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.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame, {parentFirst=true})
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
local input_try = args[1] or args['1']
local catch_list = {}
local default_catch = {["value"]="{{{throw|error()}}}", ["exception"]={} }
for key,value in pairs(args) do
local argname = mw.ustring.lower(tostring(key))
local exception_data ,checker = mw.ustring.gsub(argname,'^%s*catch%s*%((.-)%)%s*$','%1')
if checker > 0 then
catch_list[#catch_list+1] = {["value"]=value, ["exception"]=(mw.text.split(exception_data,',') or exception_data)}
elseif mw.text.trim(argname) == 'catch' then
default_catch.value=value
end
end
local find_error = ' ' .. input_try .. ' '--/^...$/
local error_tags = {'strong', 'span', 'p', 'div'}
local exceptions = {}
for i=1,#error_tags do
local check_tag = error_tags[i]:gsub('^(.-)$','<%1[^>]+error.-</%1>')
find_error = mw.ustring.gsub(find_error,check_tag,function(exception)
exceptions[#exceptions + 1] = exception
return''end)
end
catch_list[#catch_list+1]=default_catch
mw.logObject(catch_list)
mw.logObject(exception)
return ''
end
return p