1use crate::markup5ever_rcdom;
2
3use super::StructuredPrinter;
4use super::TagHandler;
5
6use markup5ever_rcdom::{Handle, NodeData};
7
8#[derive(Default)]
9pub struct CodeHandler {
10 code_type: String,
11}
12
13impl CodeHandler {
14 fn do_handle(&mut self, printer: &mut StructuredPrinter, start: bool) {
16 let immediate_parent = printer.parent_chain.last().unwrap().to_owned();
17 if self.code_type == "code" && immediate_parent == "pre" {
18 return;
20 }
21
22 match self.code_type.as_ref() {
23 "pre" => {
24 if start {
26 printer.insert_newline();
27 }
28 printer.append_str("\n```\n");
29 if !start {
30 printer.insert_newline();
31 }
32 }
33 "code" | "samp" => printer.append_str("`"),
34 _ => {}
35 }
36 }
37}
38
39impl TagHandler for CodeHandler {
40 fn handle(&mut self, tag: &Handle, printer: &mut StructuredPrinter) {
41 self.code_type = match tag.data {
42 NodeData::Element { ref name, .. } => name.local.to_string(),
43 _ => String::new(),
44 };
45
46 self.do_handle(printer, true);
47 }
48 fn after_handle(&mut self, printer: &mut StructuredPrinter) {
49 self.do_handle(printer, false);
50 }
51}