1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! Tracks the containers and the leaf block that are open in the lines a container has collected, to
//! tell whether a following line without the container prefix continues a paragraph.
use super::{
FOOTNOTE_INDENT, Interrupt, ItemMarker, LineStart, MAX_DEPTH, after_blockquote_marker, interrupts_paragraph,
is_lazy_continuation, setext_depth,
};
use crate::parser::code::Fence;
use crate::parser::definition::only_definitions;
use crate::parser::html_flow::{self, Kind as HtmlKind};
use crate::parser::line::{Indent, Line};
use crate::parser::mdx_flow::{FlowEnd, FlowStart, flow_end, flow_start};
use crate::parser::table;
/// MDX flow content that spans lines is followed for this many lines, which bounds the work.
const MAX_FLOW_LINES: usize = 64;
/// A container that is open in the collected lines, inside the one that collects them.
enum Open {
Quote,
/// A list item, whose content is indented by `width` columns.
Item {
width: usize,
/// Whether the item started empty and has no content yet: a blank line then ends it.
empty: bool,
},
Footnote,
}
/// MDX flow content that goes on in the next line.
struct Pending<'a> {
text: String,
/// Terminator of the last line in `text`, which separates it from the next line.
eol: &'a str,
lines: usize,
}
pub(super) struct LeafState<'a> {
/// The containers that are open, outermost first. The leaf blocks below belong to the innermost.
open: Vec<Open>,
fence: Option<Fence<'a>>,
/// An HTML block that has not ended yet.
html: Option<HtmlKind>,
/// A table that has not ended yet. Its rows cannot be continued lazily.
table: bool,
/// The last line when it was paragraph text, which a delimiter row with as many cells can turn
/// into a header.
header: Option<Line<'a>>,
paragraph: bool,
/// The text of the open paragraph when it starts like a definition, which a setext underline does
/// not turn into a heading when there is nothing else in it.
definitions: Option<String>,
flow: Option<Pending<'a>>,
/// Restrictions on the next fed line, the first line of a container that interrupted a paragraph.
restricted: Interrupt,
/// How many containers can still be open before the depth limit, which no longer open any.
budget: usize,
}
impl<'a> LeafState<'a> {
/// `depth` is the depth of the container that collects the lines.
pub(super) fn new(restricted: Interrupt, depth: usize) -> Self {
Self {
open: Vec::new(),
fence: None,
html: None,
table: false,
header: None,
paragraph: false,
definitions: None,
flow: None,
restricted,
budget: MAX_DEPTH.saturating_sub(depth + 1),
}
}
/// Whether `line` without its container prefix continues the paragraph of the collected lines.
pub(super) fn continues_with(&self, line: &Line<'_>) -> bool {
self.paragraph && is_lazy_continuation(line)
}
pub(super) fn feed(&mut self, line: &Line<'a>) {
self.feed_line(*line);
if !self.paragraph {
self.definitions = None;
}
}
/// Whether the blank `line` is part of the fence that is open, which a container does not end for
/// the blank lines that come last in it.
pub(super) fn fence_holds(&self, blank: &Line<'a>) -> bool {
self.fence.is_some() && self.strip_prefixes(*blank).0 == self.open.len()
}
/// Removes the prefixes of the open containers from `line`, and returns how many of them it has.
fn strip_prefixes(&self, mut line: Line<'a>) -> (usize, Line<'a>) {
for (index, open) in self.open.iter().enumerate() {
let width = match open {
Open::Quote => {
let indent = line.indent();
if !matches!(LineStart::of(&line, indent), Some(LineStart::Blockquote)) {
return (index, line);
}
line = after_blockquote_marker(line, indent);
continue;
}
// Another blank line ends an item that started empty.
Open::Item { empty: true, .. } if line.is_blank() => return (index, line),
Open::Item { width, .. } => *width,
Open::Footnote => FOOTNOTE_INDENT,
};
// A blank line has the prefix of an item, whatever its indentation.
if !line.is_blank() && line.indent().columns < width {
return (index, line);
}
line = line.skip_columns(width);
}
(self.open.len(), line)
}
/// Ends the containers from the `keep`th on, and with them what they hold.
fn close_from(&mut self, keep: usize) {
self.open.truncate(keep);
self.fence = None;
self.html = None;
self.table = false;
self.header = None;
self.paragraph = false;
self.flow = None;
}
fn feed_line(&mut self, line: Line<'a>) {
let restricted = std::mem::take(&mut self.restricted);
let mut line = line;
// A lazy line was already accepted by the container that collects the lines.
if !line.lazy {
let (matched, rest) = self.strip_prefixes(line);
if matched < self.open.len() {
// Containers that do not match end, unless the line goes on with their paragraph.
if self.paragraph && flow_start(&rest).is_none() && is_lazy_continuation(&rest) {
line = Line { lazy: true, ..rest };
} else {
self.close_from(matched);
line = rest;
}
} else {
line = rest;
}
if !line.is_blank() {
for open in &mut self.open {
if let Open::Item { empty, .. } = open {
*empty = false;
}
}
}
}
// A paragraph that this line is in the container of, which markers have to interrupt.
let in_paragraph = self.paragraph && !line.lazy;
loop {
let line_indent = line.indent();
let Indent { columns, bytes: indent } = line_indent;
let rest = &line.text[indent..];
if let Some(fence) = &self.fence {
if columns < line.code_indent() && fence.is_closed_by(rest) {
self.fence = None;
}
return;
}
if let Some(kind) = self.html {
let ends = match kind {
HtmlKind::Basic | HtmlKind::Complete => line.is_blank(),
_ => html_flow::ends_in(kind, line.text),
};
if ends {
self.html = None;
}
self.paragraph = false;
return;
}
if let Some(pending) = &mut self.flow {
pending.text.push_str(pending.eol);
pending.text.push_str(line.text);
pending.eol = line.eol;
pending.lines += 1;
let end = if pending.lines < MAX_FLOW_LINES {
flow_end(&pending.text)
} else {
FlowEnd::Done
};
if matches!(end, FlowEnd::Open) {
return;
}
self.flow = None;
self.paragraph = matches!(end, FlowEnd::Text);
return;
}
if line.is_blank() {
self.paragraph = false;
self.table = false;
self.header = None;
return;
}
let header = std::mem::take(&mut self.header);
if self.table {
if interrupts_paragraph(&line) {
self.table = false;
} else {
self.paragraph = false;
return;
}
} else if self.paragraph
&& !line.lazy
&& let Some(cells) = table::delimiter_cells(&line)
&& header.as_ref().and_then(table::header_width) == Some(cells)
&& !interrupts_paragraph(&line)
{
self.table = true;
self.paragraph = false;
return;
}
if columns >= line.code_indent() {
// Continues an open paragraph, otherwise it is indented code (or text when the
// container interrupted a paragraph).
if !self.paragraph {
self.paragraph = restricted.code;
} else if let Some(text) = &mut self.definitions {
text.push('\n');
text.push_str(rest);
}
return;
}
let start = LineStart::of(&line, line_indent);
if let Some(LineStart::Fence(fence)) = start {
self.fence = Some(fence);
self.paragraph = false;
return;
}
if self.paragraph && setext_depth(&line).is_some() {
// Under nothing but definitions an underline is text, unless it is a rule.
if self.definitions.as_deref().is_some_and(only_definitions) {
self.definitions = None;
self.paragraph = !matches!(start, Some(LineStart::ThematicBreak));
} else {
self.paragraph = false;
}
return;
}
let can_open = self.open.len() < self.budget;
match start {
Some(LineStart::Atx(_) | LineStart::ThematicBreak) => {
self.paragraph = false;
return;
}
Some(LineStart::Html(kind)) if kind != HtmlKind::Complete || !self.paragraph => {
let closed = match kind {
HtmlKind::Basic | HtmlKind::Complete => false,
_ => html_flow::ends_in(kind, &rest[html_flow::first_line_offset(kind)..]),
};
self.html = (!closed).then_some(kind);
self.paragraph = false;
return;
}
Some(LineStart::Blockquote) if can_open => {
line = after_blockquote_marker(line, line_indent);
self.open.push(Open::Quote);
self.paragraph = false;
continue;
}
Some(LineStart::Footnote(marker)) if can_open => {
line = line.skip(marker.content);
self.open.push(Open::Footnote);
self.paragraph = false;
continue;
}
// A marker in the container of the open paragraph has to be able to interrupt it.
Some(LineStart::Item(marker))
if can_open && (!(restricted.list || in_paragraph) || marker.interrupts_paragraph()) =>
{
self.open.push(item(&marker));
line = marker.content(line);
// The rest of the line starts a new item.
self.paragraph = false;
continue;
}
_ => {}
}
// MDX flow content is not part of a paragraph.
if let Some(start) = flow_start(&line) {
self.paragraph = false;
if matches!(start, FlowStart::Pending) {
self.flow = Some(Pending {
text: line.text.to_string(),
eol: line.eol,
lines: 1,
});
}
return;
}
self.header = Some(line);
if self.paragraph {
if let Some(text) = &mut self.definitions {
text.push('\n');
text.push_str(rest);
}
} else {
self.definitions = rest.starts_with('[').then(|| rest.to_string());
}
self.paragraph = true;
return;
}
}
}
fn item(marker: &ItemMarker) -> Open {
Open::Item {
width: marker.width,
empty: marker.empty,
}
}