Class: Decode::Syntax::Rewriter
- Inherits:
-
Object
- Object
- Decode::Syntax::Rewriter
- Defined in:
- lib/decode/syntax/rewriter.rb
Instance Attribute Summary collapse
-
#matches ⇒ Object
readonly
Returns the value of attribute matches.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Instance Method Summary collapse
- #<<(match) ⇒ Object
- #apply(output = []) ⇒ Object
-
#initialize(text) ⇒ Rewriter
constructor
A new instance of Rewriter.
- #link_to(definition, text) ⇒ Object
-
#text_for(range) ⇒ Object
Returns a chunk of raw text with no formatting.
Constructor Details
#initialize(text) ⇒ Rewriter
Returns a new instance of Rewriter.
9 10 11 12 |
# File 'lib/decode/syntax/rewriter.rb', line 9 def initialize(text) @text = text @matches = [] end |
Instance Attribute Details
#matches ⇒ Object (readonly)
Returns the value of attribute matches.
16 17 18 |
# File 'lib/decode/syntax/rewriter.rb', line 16 def matches @matches end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
14 15 16 |
# File 'lib/decode/syntax/rewriter.rb', line 14 def text @text end |
Instance Method Details
#<<(match) ⇒ Object
18 19 20 |
# File 'lib/decode/syntax/rewriter.rb', line 18 def << match @matches << match end |
#apply(output = []) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/decode/syntax/rewriter.rb', line 27 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 |
#link_to(definition, text) ⇒ Object
50 51 52 |
# File 'lib/decode/syntax/rewriter.rb', line 50 def link_to(definition, text) "[#{text}]" end |
#text_for(range) ⇒ Object
Returns a chunk of raw text with no formatting.
23 24 25 |
# File 'lib/decode/syntax/rewriter.rb', line 23 def text_for(range) @text[range] end |