class Decode::Syntax::Rewriter

Attributes

matches[R]
text[R]

Public Class Methods

new(text) click to toggle source
# File lib/decode/syntax/rewriter.rb, line 24
def initialize(text)
        @text = text
        @matches = []
end

Public Instance Methods

<<(match) click to toggle source
# File lib/decode/syntax/rewriter.rb, line 33
def << match
        @matches << match
end
apply(output = []) click to toggle source
# File lib/decode/syntax/rewriter.rb, line 42
def apply(output = [])
        offset = 0
        
        @matches.sort.each do |match|
                if match.offset > offset
                        output << text_for(offset...match.offset)
                        
                        offset = match.offset
                elsif match.offset < offset
                        # Match intersects last output buffer.
                        next
                end
                
                offset += match.apply(output, self)
        end
        
        if offset < @text.size
                output << text_for(offset...@text.size)
        end
        
        return output
end
text_for(range) click to toggle source

Returns a chunk of raw text with no formatting.

# File lib/decode/syntax/rewriter.rb, line 38
def text_for(range)
        @text[range]
end