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
use super::{segment_str, Block, Color, LinkTag, Links, MetaTag, Modifier, Style, Word};
use pulldown_cmark::{Event, Tag};
use std::ops::Range;
use term_rustdoc::util::{ToCompactString, XString};
macro_rules! ele {
($iter:ident, $tag:ident, $range:ident) => {
$iter
.by_ref()
.take_while(|(e, r)| {
!(*e == ::pulldown_cmark::Event::End(::pulldown_cmark::TagEnd::$tag)
&& *r == $range)
})
.collect::<Vec<_>>()
.into_iter()
};
(#heading $iter:ident, $level:ident, $range:ident) => {
$iter
.by_ref()
.take_while(|(e, r)| {
!(*e == ::pulldown_cmark::Event::End(::pulldown_cmark::TagEnd::Heading($level))
&& *r == $range)
})
.collect::<Vec<_>>()
.into_iter()
};
(#list $iter:ident, $ordered:expr, $range:ident) => {
$iter
.by_ref()
.take_while(|(e, r)| {
!(*e == ::pulldown_cmark::Event::End(::pulldown_cmark::TagEnd::List($ordered))
&& *r == $range)
})
.collect::<Vec<_>>()
.into_iter()
};
}
pub type EventRange<'doc> = (Event<'doc>, Range<usize>);
pub struct Element<'doc, 'block, 'links, I> {
doc: &'doc str,
iter: I,
block: &'block mut Block,
links: &'links mut Links,
}
impl<'doc, 'block, 'links, I> Element<'doc, 'block, 'links, I>
where
I: Iterator<Item = EventRange<'doc>>,
{
pub fn new(
doc: &'doc str,
block: &'block mut Block,
links: &'links mut Links,
iter: I,
) -> Self {
Element {
doc,
iter,
block,
links,
}
}
pub fn parse_paragraph(self) {
let Element {
doc,
iter,
block,
links,
} = self;
// FIXME: We can remove some branches on List/Item. But for now, let's keep it simple.
super::list::parse(&mut 0, None, iter, block, doc, links);
// while let Some((event, range)) = para.next() {
// match event {
// Event::Text(text) => block.push_normal_words(&text),
// Event::Start(Tag::Link { dest_url, .. }) => {
// Element::new(doc, block, links, ele!(para, Link, range)).parse_link(&dest_url);
// }
// Event::Start(Tag::Emphasis) => {
// Element::new(doc, block, links, ele!(para, Emphasis, range)).parse_emphasis();
// }
// Event::Start(Tag::Strong) => {
// Element::new(doc, block, links, ele!(para, Strong, range)).parse_strong();
// }
// Event::Start(Tag::Strikethrough) => {
// Element::new(doc, block, links, ele!(para, Strikethrough, range))
// .parse_strike_through();
// }
// Event::Code(intra_code) => parse_intra_code(&intra_code, block),
// Event::SoftBreak | Event::HardBreak => block.push_a_word(Word {
// // To indicate there is a whitespace in case the last word in this line
// // and word in next line are on the same line with whitespace separator after wrapping.
// trailling_whitespace: true,
// ..Default::default()
// }),
// Event::FootnoteReference(key) => block.push_a_word(Word {
// word: "[^_]".into(),
// style: Style {
// fg: Some(Color::LightMagenta),
// ..Default::default()
// },
// tag: MetaTag::Link(LinkTag::Footnote((&*key).into())),
// trailling_whitespace: false,
// }),
// Event::Start(Tag::Image { dest_url, .. }) => {
// Element::new(doc, block, links, ele!(para, Image, range))
// .parse_image(&dest_url);
// }
// Event::TaskListMarker(done) => task_maker(done, block),
// _ => (),
// }
// }
}
/// TODO: support local/external crate item links
pub fn parse_link(self, link: &str) {
let Element {
mut iter,
block,
links,
..
} = self;
let idx = links.push_link(link.into());
block.push_link(idx);
let tag = MetaTag::Link(LinkTag::ReferenceLink(idx));
let style = LINK;
let alink = |word| Word {
word,
style,
tag: tag.clone(),
trailling_whitespace: false,
};
block.push_a_word(alink(XString::new_inline("[")));
while let Some((event, range)) = iter.next() {
match event {
Event::Text(words) => {
block.push_words(&words, style, tag.clone());
}
Event::Code(code) => {
parse_intra_code_in_link(&code, block);
}
Event::Start(Tag::Emphasis) => {
let style = style.add_modifier(Modifier::ITALIC);
// we use for-loop here to discard further nested styles
for (event, _) in ele!(iter, Emphasis, range) {
if let Event::Text(words) = event {
block.push_words(&words, style, tag.clone());
}
}
}
Event::Start(Tag::Strong) => {
let style = style.add_modifier(Modifier::BOLD);
for (event, _) in ele!(iter, Strong, range) {
if let Event::Text(words) = event {
block.push_words(&words, style, tag.clone());
}
}
}
Event::Start(Tag::Strikethrough) => {
let style = style.add_modifier(Modifier::CROSSED_OUT);
for (event, _) in ele!(iter, Strikethrough, range) {
if let Event::Text(words) = event {
block.push_words(&words, style, tag.clone());
}
}
}
_ => (),
}
}
block.push_a_word(alink(XString::new_inline("]")));
block.push_a_word(alink(XString::new_inline("[")));
block.push_a_word(alink(idx.to_compact_string()));
block.push_a_word(alink(XString::new_inline("]")));
}
/// Images are like links, e.g. `![ref]` are valid syntax, or ``.
/// But when parsing them, don't show further styles and just truncate the img line if too long.
pub fn parse_image(self, link: &str) {
let Element { iter, block, .. } = self;
let tag = MetaTag::Image;
let style = Style {
fg: Some(Color::Rgb(192, 192, 192)), // #C0C0C0
..Default::default()
};
let mut img = String::with_capacity(32);
img.push_str(";
img.push_str(link);
img.push(')');
img.shrink_to_fit();
block.push_a_word(Word {
word: img.into(),
style,
tag,
trailling_whitespace: false,
});
}
// Used in `parse_{emphasis,strong,strike_through}`.
//
// FIXME: the nested styles are simplified as follows:
// * only one kind style is applied to Text
// * intra_code and links are not applied
// but we could support real nested styles because add_modifier is additive
fn parse_nested_styles(self, style: Style) {
let Element {
doc,
mut iter,
block,
links,
} = self;
while let Some((event, range)) = iter.next() {
match event {
Event::Text(text) => {
block.push_words(&text, style, MetaTag::Normal);
}
Event::Code(code) => parse_intra_code(&code, block),
Event::Start(Tag::Link { dest_url, .. }) => {
let iter = ele!(iter, Link, range);
Element::new(doc, block, links, iter).parse_link(&dest_url);
}
Event::SoftBreak | Event::HardBreak => block.push_a_word(Word {
word: "".into(),
style,
tag: MetaTag::Normal,
trailling_whitespace: true,
}),
_ => (),
}
}
}
pub fn parse_emphasis(self) {
let style = Style {
add_modifier: Modifier::ITALIC,
..Default::default()
};
self.parse_nested_styles(style);
}
pub fn parse_strong(self) {
let style = Style {
add_modifier: Modifier::BOLD,
..Default::default()
};
self.parse_nested_styles(style);
}
pub fn parse_strike_through(self) {
let style = Style {
add_modifier: Modifier::CROSSED_OUT,
..Default::default()
};
self.parse_nested_styles(style);
}
}
pub const LINK: Style = Style {
fg: Some(Color::Rgb(30, 144, 255)), // #1E90FF
add_modifier: Modifier::empty(),
bg: None,
underline_color: None,
sub_modifier: Modifier::empty(),
};
const INTRA_CODE: Style = Style {
fg: Some(Color::Rgb(255, 184, 162)), // #FFB8A2
bg: None,
underline_color: None,
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
};
pub const FOOTNOTE: Style = Style {
fg: Some(Color::LightMagenta),
bg: None,
underline_color: None,
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
};
pub fn parse_intra_code(code: &str, block: &mut Block) {
fn word(s: &str) -> Word {
Word {
word: s.into(),
style: INTRA_CODE,
tag: MetaTag::InlineCode,
trailling_whitespace: false,
}
}
block.push_a_word(word("`"));
segment_str(code, |s| {
block.push_a_word(word(s));
});
let end = word("`");
block.push_a_word(end);
}
pub fn parse_intra_code_in_link(code: &str, block: &mut Block) {
fn word(s: &str, style: Style) -> Word {
Word {
word: s.into(),
style,
tag: MetaTag::InlineCode,
trailling_whitespace: false,
}
}
let tick = word("`", INTRA_CODE);
block.push_a_word(tick.clone());
segment_str(code, |s| {
let word = word(s, LINK);
block.push_a_word(word);
});
block.push_a_word(tick);
}