html2md/
paragraphs.rs

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