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
use super::TagHandler;
use super::StructuredPrinter;
use markup5ever_rcdom::Handle;
fn list_hierarchy(printer: &mut StructuredPrinter) -> Vec<&String> {
printer.parent_chain.iter().rev().filter(|&tag| tag == "ul" || tag == "ol" || tag == "menu").collect()
}
#[derive(Default)]
pub struct ListHandler;
impl TagHandler for ListHandler {
fn handle(&mut self, _tag: &Handle, printer: &mut StructuredPrinter) {
printer.insert_newline();
if list_hierarchy(printer).is_empty() {
printer.insert_newline();
}
}
fn after_handle(&mut self, printer: &mut StructuredPrinter) {
printer.insert_newline();
printer.insert_newline();
}
}
#[derive(Default)]
pub struct ListItemHandler {
start_pos: usize,
list_type: String
}
impl TagHandler for ListItemHandler {
fn handle(&mut self, _tag: &Handle, printer: &mut StructuredPrinter) {
{
let parent_lists = list_hierarchy(printer);
let nearest_parent_list = parent_lists.first();
if nearest_parent_list.is_none() {
return;
}
self.list_type = nearest_parent_list.unwrap().to_string();
}
if printer.data.chars().last() != Some('\n') {
printer.insert_newline();
}
let current_depth = printer.parent_chain.len();
let order = printer.siblings[¤t_depth].len() + 1;
match self.list_type.as_ref() {
"ul" | "menu" => printer.append_str("* "), "ol" => printer.append_str(&(order.to_string() + ". ")), _ => {} }
self.start_pos = printer.data.len();
}
fn after_handle(&mut self, printer: &mut StructuredPrinter) {
let padding = match self.list_type.as_ref() {
"ul" => 2,
"ol" => 3,
_ => 4
};
let index = self.start_pos;
while index < printer.data.len() {
if printer.data.bytes().nth(index) == Some(b'\n') || printer.data.bytes().nth(index) == Some(b' ') {
printer.data.remove(index);
} else {
break;
}
}
let mut index = printer.data.len();
while index > self.start_pos {
if printer.data.bytes().nth(index) == Some(b'\n') {
printer.insert_str(index + 1, &" ".repeat(padding));
}
index -= 1;
}
}
}