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.



80
81
82
83
# File 'lib/red_quilt/blockquote.rb', line 80

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

Instance Method Details

#parse(parent_id, lines, index) ⇒ Object



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
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/red_quilt/blockquote.rb', line 85

def parse(parent_id, lines, index)
  # Captured before the loop: block_lines holds prefix-stripped lines,
  # whose start_byte sits after the `>`. The span must start at the
  # marker itself (excluding indent), as cmark and mdast both report.
  first_line = lines[index]
  source_start = first_line.start_byte + (Blockquote.marker_offset(first_line.content) || 0)
  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: source_start,
                             source_len: block_lines.last.end_byte - source_start)
  @arena.append_child(parent_id, block_id)
  @block_parser.parse_lines(block_id, block_lines, transformed: true)
  index
end