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
use super::TagHandler;
use super::StructuredPrinter;
use markup5ever_rcdom::{Handle,NodeData};
#[derive(Default)]
pub struct CodeHandler {
code_type: String
}
impl CodeHandler {
fn do_handle(&mut self, printer: &mut StructuredPrinter, start: bool) {
let immediate_parent = printer.parent_chain.last().unwrap().to_owned();
if self.code_type == "code" && immediate_parent == "pre" {
return;
}
match self.code_type.as_ref() {
"pre" => {
if start {
printer.insert_newline();
}
printer.append_str("\n```\n");
if !start {
printer.insert_newline();
}
},
"code" | "samp" => printer.append_str("`"),
_ => {}
}
}
}
impl TagHandler for CodeHandler {
fn handle(&mut self, tag: &Handle, printer: &mut StructuredPrinter) {
self.code_type = match tag.data {
NodeData::Element { ref name, .. } => name.local.to_string(),
_ => String::new()
};
self.do_handle(printer, true);
}
fn after_handle(&mut self, printer: &mut StructuredPrinter) {
self.do_handle(printer, false);
}
}