sub.lua (852B)
1 local Subs = { } 2 3 local function doSubs( line ) 4 for i = 1, #Subs do 5 local sub = Subs[ i ] 6 7 if sub.enabled then 8 local ok, newLine, subs = pcall( string.gsub, line, sub.pattern, sub.replacement ) 9 10 if not ok then 11 mud.print( "\n#s> sub failed: %s\n%s\n%s", newLine, sub.pattern, sub.replacement ) 12 return line 13 end 14 15 if subs ~= 0 then 16 return newLine 17 end 18 end 19 end 20 21 return line 22 end 23 24 function mud.sub( pattern, replacement, disabled ) 25 enforce( pattern, "pattern", "string" ) 26 enforce( replacement, "replacement", "string", "function" ) 27 28 local sub = { 29 pattern = pattern, 30 replacement = replacement, 31 32 enabled = not disabled, 33 34 enable = function( self ) 35 self.enabled = true 36 end, 37 disable = function( self ) 38 self.enabled = false 39 end, 40 } 41 42 table.insert( Subs, sub ) 43 44 return sub 45 end 46 47 return { 48 doSubs = doSubs, 49 }