2005-01-15 21:26:54 +01:00
|
|
|
require 'digest/md5'
|
|
|
|
require 'uri/common'
|
|
|
|
|
|
|
|
# A chunk is a pattern of text that can be protected
|
|
|
|
# and interrogated by a renderer. Each Chunk class has a
|
|
|
|
# +pattern+ that states what sort of text it matches.
|
|
|
|
# Chunks are initalized by passing in the result of a
|
|
|
|
# match by its pattern.
|
|
|
|
module Chunk
|
|
|
|
class Abstract
|
|
|
|
attr_reader :text
|
|
|
|
|
|
|
|
def initialize(match_data) @text = match_data[0] end
|
2005-01-19 21:29:43 +01:00
|
|
|
|
|
|
|
# Find all the chunks of the given type in content
|
|
|
|
# Each time the pattern is matched, create a new
|
|
|
|
# chunk for it, and replace the occurance of the chunk
|
|
|
|
# in this content with its mask.
|
|
|
|
def self.apply_to(content)
|
|
|
|
content.gsub!( self.pattern ) do |match|
|
2005-01-23 04:27:45 +01:00
|
|
|
new_chunk = self.new($~)
|
|
|
|
content.chunks << new_chunk
|
|
|
|
new_chunk.mask(content)
|
2005-01-19 21:29:43 +01:00
|
|
|
end
|
|
|
|
end
|
2005-01-23 04:27:45 +01:00
|
|
|
|
2005-01-19 21:29:43 +01:00
|
|
|
def mask(content)
|
2005-01-23 04:27:45 +01:00
|
|
|
"chunk#{self.object_id}#{self.class.to_s.delete(':').downcase}chunk"
|
2005-01-19 21:29:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def revert(content)
|
2005-01-22 17:38:33 +01:00
|
|
|
content.sub!( Regexp.new(mask(content)), text )
|
2005-01-19 21:29:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def unmask(content)
|
|
|
|
self if revert(content)
|
|
|
|
end
|
|
|
|
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|
|
|
|
end
|