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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use nom::IResult;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
/// Function type for mapping elements.
type ElementMapFn<ELT> = Rc<RefCell<Box<dyn FnMut(ELT) -> ELT>>>;
/// Function type for mapping elements.
type ElementFlatMapFn<ELT> = Rc<RefCell<Box<dyn FnMut(ELT) -> Vec<ELT>>>>;
/// Function type for custom block parsers.
type CustomBlockParserFn =
Rc<RefCell<Box<dyn for<'a> FnMut(&'a str) -> IResult<&'a str, Vec<crate::ast::Block>>>>>;
/// Function type for custom inline parsers.
type CustomInlineParserFn =
Rc<RefCell<Box<dyn for<'a> FnMut(&'a str) -> IResult<&'a str, Vec<crate::ast::Inline>>>>>;
/// Behavior of the parser when encountering certain elements.
#[derive(Clone)]
pub enum ElementBehavior<ELT> {
/// The parser will parse the element normally.
Parse,
/// The parser will ignore the element and not parse it. In this case, alternative
/// parsers will be tried.
Ignore,
/// Parse element but do not include it in the output.
Skip,
/// Parse the element and apply a custom function to it.
Map(ElementMapFn<ELT>),
/// Parse the element and apply a custom function to it which returns an array of elements.
FlatMap(ElementFlatMapFn<ELT>),
}
/// A configuration for the Markdown parser.
#[derive(Clone)]
pub struct MarkdownParserConfig {
/// If true, the parser will allow headings without a space after the hash marks.
pub(crate) allow_no_space_in_headings: bool,
/// A map of HTML entities to their corresponding `Entity` structs.
pub(crate) html_entities_map: HashMap<String, &'static entities::Entity>,
/// The behavior of the parser when encountering blockquotes.
pub(crate) block_blockquote_behavior: ElementBehavior<crate::ast::Block>,
/// The behavior of the parser when encountering GitHub alerts.
pub(crate) block_github_alert_behavior: ElementBehavior<crate::ast::Block>,
/// The behavior of the parser when encountering headings in style 1 (e.g., `# Heading`).
pub(crate) block_heading_v1_behavior: ElementBehavior<crate::ast::Block>,
/// The behavior of the parser when encountering headings in style 2 (e.g., `Heading\n===`).
pub(crate) block_heading_v2_behavior: ElementBehavior<crate::ast::Block>,
/// The behavior of the parser when encountering thematic breaks (e.g., `---`).
pub(crate) block_thematic_break_behavior: ElementBehavior<crate::ast::Block>,
/// The behavior of the parser when encountering lists.
pub(crate) block_list_behavior: ElementBehavior<crate::ast::Block>,
/// The behavior of the parser when encountering code blocks.
pub(crate) block_code_block_behavior: ElementBehavior<crate::ast::Block>,
/// The behavior of the parser when encountering HTML blocks.
pub(crate) block_html_block_behavior: ElementBehavior<crate::ast::Block>,
/// The behavior of the parser when encountering footnote definitions.
pub(crate) block_footnote_definition_behavior: ElementBehavior<crate::ast::Block>,
/// The behavior of the parser when encountering link definitions.
pub(crate) block_link_definition_behavior: ElementBehavior<crate::ast::Block>,
/// The behavior of the parser when encountering tables.
pub(crate) block_table_behavior: ElementBehavior<crate::ast::Block>,
/// The behavior of the parser when encountering block paragraphs.
pub(crate) block_paragraph_behavior: ElementBehavior<crate::ast::Block>,
/// The behavior of the parser when encountering inline autolinks.
pub(crate) inline_autolink_behavior: ElementBehavior<crate::ast::Inline>,
/// The behavior of the parser when encountering inline links.
pub(crate) inline_link_behavior: ElementBehavior<crate::ast::Inline>,
/// The behavior of the parser when encountering inline footnote references.
pub(crate) inline_footnote_reference_behavior: ElementBehavior<crate::ast::Inline>,
/// The behavior of the parser when encountering inline reference links.
pub(crate) inline_reference_link_behavior: ElementBehavior<crate::ast::Inline>,
/// The behavior of the parser when encountering inline hard newlines.
pub(crate) inline_hard_newline_behavior: ElementBehavior<crate::ast::Inline>,
/// The behavior of the parser when encountering inline images.
pub(crate) inline_image_behavior: ElementBehavior<crate::ast::Inline>,
/// The behavior of the parser when encountering inline code spans.
pub(crate) inline_code_span_behavior: ElementBehavior<crate::ast::Inline>,
/// The behavior of the parser when encountering inline emphasis.
pub(crate) inline_emphasis_behavior: ElementBehavior<crate::ast::Inline>,
/// The behavior of the parser when encountering inline strikethrough.
pub(crate) inline_strikethrough_behavior: ElementBehavior<crate::ast::Inline>,
/// The behavior of the parser when encountering inline text.
pub(crate) inline_text_behavior: ElementBehavior<crate::ast::Inline>,
/// A custom parser for blocks. This is a function that takes a string and returns a `Block`.
pub(crate) custom_block_parser: Option<CustomBlockParserFn>,
/// A custom parser for inlines. This is a function that takes a string and returns a `Inline`.
pub(crate) custom_inline_parser: Option<CustomInlineParserFn>,
}
impl Default for MarkdownParserConfig {
fn default() -> Self {
Self {
allow_no_space_in_headings: false,
html_entities_map: Self::make_html_entities_map(),
block_blockquote_behavior: ElementBehavior::Parse,
block_github_alert_behavior: ElementBehavior::Parse,
block_heading_v1_behavior: ElementBehavior::Parse,
block_heading_v2_behavior: ElementBehavior::Parse,
block_thematic_break_behavior: ElementBehavior::Parse,
block_list_behavior: ElementBehavior::Parse,
block_code_block_behavior: ElementBehavior::Parse,
block_html_block_behavior: ElementBehavior::Parse,
block_footnote_definition_behavior: ElementBehavior::Parse,
block_link_definition_behavior: ElementBehavior::Parse,
block_table_behavior: ElementBehavior::Parse,
block_paragraph_behavior: ElementBehavior::Parse,
inline_autolink_behavior: ElementBehavior::Parse,
inline_link_behavior: ElementBehavior::Parse,
inline_footnote_reference_behavior: ElementBehavior::Parse,
inline_reference_link_behavior: ElementBehavior::Parse,
inline_hard_newline_behavior: ElementBehavior::Parse,
inline_image_behavior: ElementBehavior::Parse,
inline_code_span_behavior: ElementBehavior::Parse,
inline_emphasis_behavior: ElementBehavior::Parse,
inline_strikethrough_behavior: ElementBehavior::Parse,
inline_text_behavior: ElementBehavior::Parse,
custom_block_parser: None,
custom_inline_parser: None,
}
}
}
impl MarkdownParserConfig {
fn make_html_entities_map() -> HashMap<String, &'static entities::Entity> {
let mut map = HashMap::new();
for entity in entities::ENTITIES.iter() {
map.insert(entity.entity.to_string(), entity);
}
map
}
/// Enable the parser to allow headings without a space after the hash marks.
pub fn with_allow_no_space_in_headings(self) -> Self {
Self {
allow_no_space_in_headings: true,
..self
}
}
/// Set a custom map of HTML entities.
pub fn with_html_entities_map(
self,
html_entities_map: HashMap<String, &'static entities::Entity>,
) -> Self {
Self {
html_entities_map,
..self
}
}
/// Set the behavior of the parser when encountering blockquotes.
pub fn with_block_blockquote_behavior(
self,
behavior: ElementBehavior<crate::ast::Block>,
) -> Self {
Self {
block_blockquote_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering GitHub alerts.
pub fn with_block_github_alert_behavior(
self,
behavior: ElementBehavior<crate::ast::Block>,
) -> Self {
Self {
block_github_alert_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering headings in style 1 (e.g., `# Heading`).
pub fn with_block_heading_v1_behavior(
self,
behavior: ElementBehavior<crate::ast::Block>,
) -> Self {
Self {
block_heading_v1_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering headings in style 2 (e.g., `Heading\n===`).
pub fn with_block_heading_v2_behavior(
self,
behavior: ElementBehavior<crate::ast::Block>,
) -> Self {
Self {
block_heading_v2_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering thematic breaks (e.g., `---`).
pub fn with_block_thematic_break_behavior(
self,
behavior: ElementBehavior<crate::ast::Block>,
) -> Self {
Self {
block_thematic_break_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering lists.
pub fn with_block_list_behavior(self, behavior: ElementBehavior<crate::ast::Block>) -> Self {
Self {
block_list_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering code blocks.
pub fn with_block_code_block_behavior(
self,
behavior: ElementBehavior<crate::ast::Block>,
) -> Self {
Self {
block_code_block_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering HTML blocks.
pub fn with_block_html_block_behavior(
self,
behavior: ElementBehavior<crate::ast::Block>,
) -> Self {
Self {
block_html_block_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering footnote definitions.
pub fn with_block_footnote_definition_behavior(
self,
behavior: ElementBehavior<crate::ast::Block>,
) -> Self {
Self {
block_footnote_definition_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering link definitions.
pub fn with_block_link_definition_behavior(
self,
behavior: ElementBehavior<crate::ast::Block>,
) -> Self {
Self {
block_link_definition_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering tables.
pub fn with_block_table_behavior(self, behavior: ElementBehavior<crate::ast::Block>) -> Self {
Self {
block_table_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering block paragraphs.
pub fn with_block_paragraph_behavior(
self,
behavior: ElementBehavior<crate::ast::Block>,
) -> Self {
Self {
block_paragraph_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering inline autolinks.
pub fn with_inline_autolink_behavior(
self,
behavior: ElementBehavior<crate::ast::Inline>,
) -> Self {
Self {
inline_autolink_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering inline links.
pub fn with_inline_link_behavior(self, behavior: ElementBehavior<crate::ast::Inline>) -> Self {
Self {
inline_link_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering inline footnote references.
pub fn with_inline_footnote_reference_behavior(
self,
behavior: ElementBehavior<crate::ast::Inline>,
) -> Self {
Self {
inline_footnote_reference_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering inline reference links.
pub fn with_inline_reference_link_behavior(
self,
behavior: ElementBehavior<crate::ast::Inline>,
) -> Self {
Self {
inline_reference_link_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering inline hard newlines.
pub fn with_inline_hard_newline_behavior(
self,
behavior: ElementBehavior<crate::ast::Inline>,
) -> Self {
Self {
inline_hard_newline_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering inline images.
pub fn with_inline_image_behavior(self, behavior: ElementBehavior<crate::ast::Inline>) -> Self {
Self {
inline_image_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering inline code spans.
pub fn with_inline_code_span_behavior(
self,
behavior: ElementBehavior<crate::ast::Inline>,
) -> Self {
Self {
inline_code_span_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering inline emphasis.
pub fn with_inline_emphasis_behavior(
self,
behavior: ElementBehavior<crate::ast::Inline>,
) -> Self {
Self {
inline_emphasis_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering inline strikethrough.
pub fn with_inline_strikethrough_behavior(
self,
behavior: ElementBehavior<crate::ast::Inline>,
) -> Self {
Self {
inline_strikethrough_behavior: behavior,
..self
}
}
/// Set the behavior of the parser when encountering inline text.
pub fn with_inline_text_behavior(self, behavior: ElementBehavior<crate::ast::Inline>) -> Self {
Self {
inline_text_behavior: behavior,
..self
}
}
/// Set a custom parser for blocks.
pub fn with_custom_block_parser(self, parser: CustomBlockParserFn) -> Self {
Self {
custom_block_parser: Some(parser),
..self
}
}
/// Set a custom parser for inlines.
pub fn with_custom_inline_parser(self, parser: CustomInlineParserFn) -> Self {
Self {
custom_inline_parser: Some(parser),
..self
}
}
}