Class: Decode::Syntax::Rewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/syntax/rewriter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Rewriter

Returns a new instance of Rewriter.



24
25
26
27
# File 'lib/decode/syntax/rewriter.rb', line 24

def initialize(text)
  @text = text
  @matches = []
end

Instance Attribute Details

#matchesObject (readonly)

Returns the value of attribute matches.



31
32
33
# File 'lib/decode/syntax/rewriter.rb', line 31

def matches
  @matches
end

#textObject (readonly)

Returns the value of attribute text.



29
30
31
# File 'lib/decode/syntax/rewriter.rb', line 29

def text
  @text
end

Instance Method Details

#<<(match) ⇒ Object



33
34
35
# File 'lib/decode/syntax/rewriter.rb', line 33

def << match
  @matches << match
end

#apply(output = []) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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


65
66
67
# File 'lib/decode/syntax/rewriter.rb', line 65

def link_to(definition, text)
  "[#{text}]"
end

#text_for(range) ⇒ Object

Returns a chunk of raw text with no formatting.



38
39
40
# File 'lib/decode/syntax/rewriter.rb', line 38

def text_for(range)
  @text[range]
end