Class: RedQuilt::Blockquote::Parser

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

Instance Method Summary collapse

Constructor Details

#initialize(block_parser) ⇒ Parser

Returns a new instance of Parser.



72
73
74
75
# File 'lib/red_quilt/blockquote.rb', line 72

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

Instance Method Details

#parse(parent_id, lines, index) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/red_quilt/blockquote.rb', line 77

def parse(parent_id, lines, index)
  block_lines = []
  paragraph_open = false

  while index < lines.length
    line = lines[index]

    if line.blank
      # Blank line outside the blockquote prefix closes it.
      break
    elsif Blockquote.match?(line.content)
      stripped = Blockquote.strip_prefix(line)
      paragraph_open =
        if stripped.content.strip.empty?
          false # `>` 単独 (or `>` followed by blank) ends any open paragraph
        else
          # Recurse through any inner blockquote prefixes — an
          # innermost open paragraph (e.g. `> > > foo` where
          # `foo` is paragraph-eligible) lets a `>`-less follow-
          # up line lazily continue it even at the outer level.
          paragraph_eligible_through_blockquotes?(stripped.content)
        end
      block_lines << stripped
    elsif paragraph_open && !@block_parser.lazy_break?(lines, index)
      # Lazy continuation: a `>`-less line is absorbed into the
      # currently open paragraph as long as it doesn't itself
      # start a new block. Only allowed while the most recent
      # in-quote line is paragraph-eligible content. The `lazy`
      # flag prevents the paragraph parser from interpreting
      # `===` / `---` on such a line as a setext underline.
      block_lines << Line.new(line.content, line.start_byte, line.end_byte, line.blank, true)
    else
      break
    end

    index += 1
  end

  block_id = @arena.add_node(NodeType::BLOCKQUOTE,
                             source_start: block_lines.first.start_byte,
                             source_len: block_lines.last.end_byte - block_lines.first.start_byte)
  @arena.append_child(parent_id, block_id)
  @block_parser.parse_lines(block_id, block_lines, transformed: true)
  index
end