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
//! The link reference definitions of a body, which comrak consumes: an
//! entry of `ref_defs` for each one, and the `references` blocks of those
//! at the top level.
use std::collections::HashSet;
use comrak::{Arena, nodes::NodeValue};
use super::{Parser, options};
use crate::markdown::{
Block, BlockKind, Unit, code,
link_definitions::{self, LinkDef},
};
impl Parser<'_> {
/// Reference definitions comrak consumed, as comrak reads them: those
/// on the lines outside every leaf block, of which each at the top level
/// of the body is a unit of a `references` block, and those that open a
/// paragraph inside an item or a quote. Each one is an entry of
/// `ref_defs`, at the line of its label.
// sokf-implements: TLMD_268
// sokf-implements: TLMD_278
// sokf-implements: MD-3_AC-17
pub(super) fn reference_blocks(&mut self) {
let covered = |file_line: usize| {
self.covered
.iter()
.any(|&(s, e)| s <= file_line && file_line <= e)
};
// The runs of lines that no block covers, each read alone, as file
// lines.
let mut free: Vec<LinkDef> = Vec::new();
let mut i = 0;
while i < self.lines.len() {
if covered(self.offset + 1 + i) {
i += 1;
continue;
}
let end = (i..self.lines.len())
.find(|&k| covered(self.offset + 1 + k))
.unwrap_or(self.lines.len());
let base = self.offset + 1 + i;
free.extend(
link_definitions::read(&self.lines[i..end])
.defs
.into_iter()
.map(|d| LinkDef {
first: base + d.first,
last: base + d.last,
..d
}),
);
i = end;
}
let nested = self.nested_lines(&free);
let mut current: Option<Block> = None;
let mut free = free.into_iter().peekable();
// The last line of the definition that the walk is in.
let mut within = 0;
for (i, line) in self.lines.iter().enumerate() {
let file_line = self.offset + 1 + i;
if let Some(d) = free.next_if(|d| d.first == file_line) {
within = d.last;
self.body.ref_defs.push((d.label, d.dest, file_line));
// A definition inside an item, a quote, or a footnote is
// part of it, and forms no block of the top level.
if line.trim_start().starts_with('[') && !nested.contains(&file_line) {
let text = self.lines[i..=i + d.last - d.first]
.iter()
.map(|l| l.trim())
.collect::<Vec<_>>()
.join(" ");
let unit = Unit::leaf(text, file_line, d.last);
match current.as_mut() {
Some(b) => {
b.units.push(unit);
b.end_line = d.last;
}
None => {
current = Some(Block::new(
BlockKind::References,
file_line,
d.last,
vec![unit],
));
}
}
continue;
}
} else if file_line <= within {
continue;
}
if (covered(file_line) || !line.trim().is_empty())
&& let Some(b) = current.take()
{
self.body.blocks.push(b);
}
}
if let Some(b) = current.take() {
self.body.blocks.push(b);
}
// The definitions that open a paragraph inside an item or a quote.
for &(first, last) in &self.opening {
let (Some(from), Some(to)) = (
first.checked_sub(self.offset + 1),
last.checked_sub(self.offset + 1),
) else {
continue;
};
let Some(lines) = self.lines.get(from..=to) else {
continue;
};
for d in link_definitions::read(lines).defs {
self.body.ref_defs.push((d.label, d.dest, first + d.first));
}
}
self.body.ref_defs.sort_by_key(|d| d.2);
}
/// The first line of each definition of `free` whose label opens its
/// line and that stands inside a list item, a quote, or a footnote's
/// definition, as an indented or a lazy line does. comrak keeps no node
/// for a definition, but it reads the lines of definitions as a
/// paragraph before it takes them out, so the parser writes a letter in
/// place of the `[` of each such label, which keeps every line in the
/// containers that held it, though the paragraph can read as another
/// leaf, such as a setext heading, and reads the containers of the leaf
/// that holds the line. A label at the first column of the body's first
/// line, of a line after a blank line, or of a line after such a
/// definition stands at the top level, so it needs no second parse.
fn nested_lines(&self, free: &[LinkDef]) -> HashSet<usize> {
let line_at = |n: usize| n.checked_sub(1).and_then(|i| self.lines.get(i)).copied();
let mut top_until = 0;
let mut candidates = HashSet::new();
for d in free {
let n = d.first - self.offset;
let Some(line) = line_at(n).filter(|l| l.trim_start().starts_with('[')) else {
continue;
};
let after_top =
n == 1 || n - 1 <= top_until || line_at(n - 1).is_some_and(|l| l.trim().is_empty());
if line.starts_with('[') && after_top {
top_until = d.last - self.offset;
} else {
candidates.insert(n);
}
}
if candidates.is_empty() {
return HashSet::new();
}
// Each line as comrak ends it, so that a carriage return alone ends
// a line here as it does in `lines`.
let mut probe = self.source.as_bytes().to_vec();
for (i, line) in code::each_line(self.source).enumerate() {
if candidates.contains(&(i + 1))
&& let Some(at) = self.source[line.clone()].find('[')
{
probe[line.start + at] = b'x';
}
}
let probe = String::from_utf8(probe)
.expect("an ASCII `[` replaced with an ASCII `x` keeps the text UTF-8");
let arena = Arena::new();
let root = comrak::parse_document(&arena, &probe, &options());
let mut inside: Vec<(usize, usize)> = root
.descendants()
.filter(|n| {
matches!(n.data().value, NodeValue::Paragraph | NodeValue::Heading(_))
&& n.ancestors().skip(1).any(|a| {
matches!(
a.data().value,
NodeValue::Item(_)
| NodeValue::TaskItem(_)
| NodeValue::BlockQuote
| NodeValue::FootnoteDefinition(_)
)
})
})
.map(|n| (n.data().sourcepos.start.line, n.data().sourcepos.end.line))
.collect();
// Leaves don't overlap, so the one that can hold a line is the last
// that starts at it or before it.
inside.sort_unstable();
candidates
.into_iter()
.filter(|&l| {
let k = inside.partition_point(|&(s, _)| s <= l);
k > 0 && inside[k - 1].1 >= l
})
.map(|l| l + self.offset)
.collect()
}
}