moved apply_to from WikiContent to Chunk, so tha UriChunk can override it and decide when it

wants to match a URL and when it doesn't want to the in the way of Textile syntax
This commit is contained in:
Alexey Verkhovsky 2005-01-19 20:29:43 +00:00
parent 02930e20fe
commit e8b2a3c30a
2 changed files with 34 additions and 17 deletions

View file

@ -11,10 +11,37 @@ module Chunk
attr_reader :text attr_reader :text
def initialize(match_data) @text = match_data[0] end def initialize(match_data) @text = match_data[0] end
def pre_mask() "chunk#{self.object_id}start " end
def post_mask() " chunk#{self.object_id}end" end # Find all the chunks of the given type in content
def mask(content) "chunk#{self.object_id}chunk" end # Each time the pattern is matched, create a new
def revert(content) content.sub!( Regexp.new(mask(content)), text ) end # chunk for it, and replace the occurance of the chunk
def unmask(content) self if revert(content) end # in this content with its mask.
def self.apply_to(content)
content.gsub!( self.pattern ) do |match|
content.chunks << self.new($~)
content.chunks.last.mask(content)
end
end
def pre_mask()
"chunk#{self.object_id}start "
end
def post_mask()
" chunk#{self.object_id}end"
end
def mask(content)
"chunk#{self.object_id}chunk"
end
def revert(content)
content.sub!( Regexp.new(mask(content)), text )
end
def unmask(content)
self if revert(content)
end
end end
end end

View file

@ -49,7 +49,7 @@ class WikiContent < String
:mode => [:display] :mode => [:display]
} }
attr_reader :web, :options, :rendered attr_reader :web, :options, :rendered, :chunks
# Create a new wiki content string from the given one. # Create a new wiki content string from the given one.
# The options are explained at the top of this file. # The options are explained at the top of this file.
@ -86,20 +86,10 @@ class WikiContent < String
# Render this content using the specified actions. # Render this content using the specified actions.
def render!(chunk_types) def render!(chunk_types)
@chunks = [] @chunks = []
chunk_types.each { |chunk_type| self.apply_type!(chunk_type) } chunk_types.each { |chunk_type| chunk_type.apply_to(self) }
@rendered = @chunks.map { |chunk| chunk.unmask(self) }.compact @rendered = @chunks.map { |chunk| chunk.unmask(self) }.compact
(@chunks - @rendered).each { |chunk| chunk.revert(self) } (@chunks - @rendered).each { |chunk| chunk.revert(self) }
end end
# Find all the chunks of the given type in this content
# Each time the type's pattern is matched, create a new
# chunk for it, and replace the occurance of the chunk
# in this content with its mask.
def apply_type!(chunk_type)
self.gsub!( chunk_type.pattern ) do |match|
@chunks << chunk_type.new($~)
@chunks.last.mask(self)
end
end
end end