summaryrefslogtreecommitdiff
path: root/src/visitor/embedded.rs
blob: 891240b576a27a6b3e64ba3c2dc0dbe35d2633ea (plain)
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
use crate::panhelper;
use crate::EmbeddedFile;
use crate::EmbeddedFiles;

use pandoc_ast::{Block, MutVisitor};

impl MutVisitor for EmbeddedFiles {
    fn visit_vec_block(&mut self, vec_block: &mut Vec<Block>) {
        use panhelper::is_class;
        for block in vec_block {
            match block {
                Block::CodeBlock(attr, contents) => {
                    if is_class(attr, "file") {
                        let add_newline = match panhelper::find_attr_kv(attr, "add-newline").next()
                        {
                            None | Some("auto") => !contents.ends_with('\n'),
                            Some("yes") => true,
                            Some("no") => false,
                            _ => unreachable!(),
                        };
                        let contents = if add_newline {
                            format!("{}\n", contents)
                        } else {
                            contents.clone()
                        };
                        self.push(EmbeddedFile::new(panhelper::get_filename(attr), contents));
                    }
                }
                _ => {
                    self.visit_block(block);
                }
            }
        }
    }
}