Class: RedQuilt::HtmlBlock::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/red_quilt/html_block.rb

Overview

Cached collaborator for BlockParser. A single instance is created in BlockParser#initialize and reused; per-call state lives in method locals so reentrant calls are safe.

Instance Method Summary collapse

Constructor Details

#initialize(block_parser) ⇒ Parser

Returns a new instance of Parser.



102
103
104
# File 'lib/red_quilt/html_block.rb', line 102

def initialize(block_parser)
  @arena = block_parser.arena
end

Instance Method Details

#parse(parent_id, lines, index) ⇒ Object

Parses the HTML block starting at lines (its type already confirmed by HtmlBlock.start?). Returns the index past the block.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/red_quilt/html_block.rb', line 108

def parse(parent_id, lines, index)
  start_index = index
  type = HtmlBlock.type(lines[index].content)
  end_index = locate_end(lines, index, type)

  start_byte = lines[start_index].start_byte
  end_byte = lines[end_index].end_byte
  html_lines = (start_index..end_index).map { |i| lines[i].content }
  html_id = @arena.add_node(NodeType::HTML_BLOCK,
                            source_start: start_byte,
                            source_len: end_byte - start_byte,
                            str1: html_lines.join("\n"))
  @arena.append_child(parent_id, html_id)
  end_index + 1
end