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
use parse::{RawParser, Event, Tag, Options, OPTION_FIRST_PASS};
use std::collections::HashSet;
pub struct Parser<'a> {
inner: RawParser<'a>,
loose_lists: HashSet<usize>,
loose_stack: Vec<bool>,
}
impl<'a> Parser<'a> {
pub fn new(text: &'a str) -> Parser<'a> {
Parser::new_ext(text, Options::empty())
}
pub fn new_ext(text: &'a str, mut opts: Options) -> Parser<'a> {
opts.remove(OPTION_FIRST_PASS);
let first_opts = opts | OPTION_FIRST_PASS;
let mut first = RawParser::new(text, first_opts);
while first.next().is_some() { }
let info = first.get_info();
let loose_lists = info.loose_lists;
let second = RawParser::new_with_links(text, opts, info.links);
Parser {
inner: second,
loose_lists: loose_lists,
loose_stack: Vec::new(),
}
}
pub fn get_offset(&self) -> usize {
self.inner.get_offset()
}
}
impl<'a> Iterator for Parser<'a> {
type Item = Event<'a>;
fn next(&mut self) -> Option<Event<'a>> {
loop {
match self.inner.next() {
Some(event) => {
match event {
Event::Start(Tag::List(_)) => {
let offset = self.inner.get_offset();
let is_loose = self.loose_lists.contains(&offset);
self.loose_stack.push(is_loose);
}
Event::Start(Tag::BlockQuote) => {
self.loose_stack.push(true);
}
Event::Start(Tag::Paragraph) | Event::End(Tag::Paragraph) => {
if let Some(&false) = self.loose_stack.last() {
continue;
}
}
Event::End(Tag::List(_)) | Event::End(Tag::BlockQuote) => {
let _ = self.loose_stack.pop();
}
_ => ()
}
return Some(event)
}
None => return None
}
}
}
}