mudgangster

Log | Files | Refs

sub.lua (704B)


      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 newLine, subs = line:gsub( sub.pattern, sub.replacement )
      9 
     10 			if subs ~= 0 then
     11 				return newLine
     12 			end
     13 		end
     14 	end
     15 
     16 	return line
     17 end
     18 
     19 function mud.sub( pattern, replacement, disabled )
     20 	enforce( pattern, "pattern", "string" )
     21 	enforce( replacement, "replacement", "string", "function" )
     22 
     23 	local sub = {
     24 		pattern = pattern,
     25 		replacement = replacement,
     26 
     27 		enabled = not disabled,
     28 
     29 		enable = function( self )
     30 			self.enabled = true
     31 		end,
     32 		disable = function( self )
     33 			self.enabled = false
     34 		end,
     35 	}
     36 
     37 	table.insert( Subs, sub )
     38 
     39 	return sub
     40 end
     41 
     42 return {
     43 	doSubs = doSubs,
     44 }