html2md/
paragraphs.rs

1use crate::markup5ever_rcdom;
2
3use super::StructuredPrinter;
4use super::TagHandler;
5
6use markup5ever_rcdom::{Handle, NodeData};
7
8#[derive(Default)]
9pub struct ParagraphHandler {
10    paragraph_type: String,
11}
12
13impl TagHandler for ParagraphHandler {
14    fn handle(&mut self, tag: &Handle, printer: &mut StructuredPrinter) {
15        self.paragraph_type = match tag.data {
16            NodeData::Element { ref name, .. } => name.local.to_string(),
17            _ => String::new(),
18        };
19
20        // insert newlines at the start of paragraph
21        if self.paragraph_type == "p" {
22            printer.insert_newline();
23            printer.insert_newline();
24        }
25    }
26
27    fn after_handle(&mut self, printer: &mut StructuredPrinter) {
28        // insert newlines at the end of paragraph
29        match self.paragraph_type.as_ref() {
30            "p" => {
31                printer.insert_newline();
32                printer.insert_newline();
33            }
34            "hr" => {
35                printer.insert_newline();
36                printer.append_str("---");
37                printer.insert_newline();
38            }
39            "br" => printer.append_str("  \n"),
40            _ => {}
41        }
42    }
43}