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
|
//! Infer things from events.
//!
//! Used to share between `to_html` and `to_mdast`.
use crate::event::{Event, Kind, Name};
use crate::mdast::AlignKind;
use alloc::{vec, vec::Vec};
/// Figure out if a list is spread or not.
///
/// When `include_items: true` is passed, infers whether the list as a whole
/// is “loose”.
pub fn list_loose(events: &[Event], mut index: usize, include_items: bool) -> bool {
let mut balance = 0;
let name = &events[index].name;
debug_assert!(
matches!(name, Name::ListOrdered | Name::ListUnordered),
"expected list"
);
while index < events.len() {
let event = &events[index];
if event.kind == Kind::Enter {
balance += 1;
if include_items
&& balance == 2
&& event.name == Name::ListItem
&& list_item_loose(events, index)
{
return true;
}
} else {
balance -= 1;
if balance == 1 && event.name == Name::BlankLineEnding {
// Blank line directly after item, which is just a prefix.
//
// ```markdown
// > | -␊
// ^
// | - a
// ```
let mut at_empty_list_item = false;
// Blank line at block quote prefix:
//
// ```markdown
// > | * >␊
// ^
// | * a
// ```
let mut at_empty_block_quote = false;
// List.
let mut before = index - 2;
if events[before].name == Name::ListItem {
before -= 1;
if events[before].name == Name::SpaceOrTab {
before -= 2;
}
if events[before].name == Name::BlockQuote
&& events[before - 1].name == Name::BlockQuotePrefix
{
at_empty_block_quote = true;
} else if events[before].name == Name::ListItemPrefix {
at_empty_list_item = true;
}
}
if !at_empty_list_item && !at_empty_block_quote {
return true;
}
}
// Done.
if balance == 0 && event.name == *name {
break;
}
}
index += 1;
}
false
}
/// Figure out if an item is spread or not.
pub fn list_item_loose(events: &[Event], mut index: usize) -> bool {
debug_assert!(
matches!(events[index].name, Name::ListItem),
"expected list item"
);
let mut balance = 0;
while index < events.len() {
let event = &events[index];
if event.kind == Kind::Enter {
balance += 1;
} else {
balance -= 1;
if balance == 1 && event.name == Name::BlankLineEnding {
// Blank line directly after a prefix:
//
// ```markdown
// > | -␊
// ^
// | a
// ```
let mut at_prefix = false;
// List item.
let mut before = index - 2;
if events[before].name == Name::SpaceOrTab {
before -= 2;
}
if events[before].name == Name::ListItemPrefix {
at_prefix = true;
}
if !at_prefix {
return true;
}
}
// Done.
if balance == 0 && event.name == Name::ListItem {
break;
}
}
index += 1;
}
false
}
/// Figure out the alignment of a GFM table.
pub fn gfm_table_align(events: &[Event], mut index: usize) -> Vec<AlignKind> {
debug_assert!(
matches!(events[index].name, Name::GfmTable),
"expected table"
);
let mut in_delimiter_row = false;
let mut align = vec![];
while index < events.len() {
let event = &events[index];
if in_delimiter_row {
if event.kind == Kind::Enter {
// Start of alignment value: set a new column.
if event.name == Name::GfmTableDelimiterCellValue {
align.push(if events[index + 1].name == Name::GfmTableDelimiterMarker {
AlignKind::Left
} else {
AlignKind::None
});
}
} else {
// End of alignment value: change the column.
if event.name == Name::GfmTableDelimiterCellValue {
if events[index - 1].name == Name::GfmTableDelimiterMarker {
let align_index = align.len() - 1;
align[align_index] = if align[align_index] == AlignKind::Left {
AlignKind::Center
} else {
AlignKind::Right
}
}
}
// Done!
else if event.name == Name::GfmTableDelimiterRow {
break;
}
}
} else if event.kind == Kind::Enter && event.name == Name::GfmTableDelimiterRow {
in_delimiter_row = true;
}
index += 1;
}
align
}
|