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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::
    collections::HashMap
;

use crate::prelude::*;

mod doc_class;
mod package;
mod metadata;

pub use doc_class::*;
pub use package::*;
pub use metadata::*;


/// The king of the land. The `Document` type is where you start.
/// Has macro support.
/// Also contains a restriction on the latex commands you use - you can't use one without
/// declaring it. Atypical of this crate, this particular feature prevents a latex error.
#[derive(Debug, Clone)]
pub struct Document {
    // document_class: DocumentClass,
    packages: Vec<Package>,
    pub metadata: Metadata,
    components: Vec<Component>,
    commands: HashMap<String, Command>,
    // labels: HashSet<&'a Label>,
    img: bool,
    href: bool,
    scratch: bool,
    graphics_path: Option<Vec<String>>,
}
impl AsLatex for Document {
    fn to_string(&self) -> String {
        let dc = self.metadata.class.to_string();
        let pkgs = self
            .packages
            .iter()
            .map(|x| x.to_string())
            .collect::<String>();
        let md = if !self.scratch {
            self.metadata.to_string()
        } else {
            "\n".to_string()
        };
        let beamer_init_frames: String = if self.metadata.class.typ == DocumentClassType::Beamer {
            // Warning: Unused result. Again, cannot n-choose-2 Component Variants.
            let title_frame = Frame::with_components("", vec![textchunk!(r"\titlepage", "normal")]);

            let mut toc = "".to_string();
            if self.metadata.tableofcontents {
                toc = Frame::with_components("", vec![textchunk!(r"\tableofcontents", "normal")])
                    .to_string();
            };

            format!("{}\n{}\n", title_frame.to_string(), toc)
        } else {
            "\n".to_string()
        };
        let body = beamer_init_frames
            + &self
                .components
                .iter()
                .map(|x| x.to_string())
                .collect::<String>();

        let cmd = self
            .commands
            .iter()
            .map(|x| format!("{} \n", x.1.declare()))
            .collect::<String>();

        let gpath = if let Some(path) = &self.graphics_path {
            format!(
                "\\graphicspath{{{}}} \n",
                path.iter()
                    .map(|x| format!("{{{}}}, ", x))
                    .collect::<String>()
            )
        } else {
            "".to_string()
        };
        format!(
            "{}\n{}\n{}\n{}\\begin{{document}}\n{}\n{}\n\\end{{document}}",
            dc, pkgs, cmd, gpath, md, body
        )
    }
}
impl Document {
    pub fn new(class: DocumentClass) -> Self {
        let mut out = Self {
            // document_class: class,
            packages: vec![],
            metadata: Metadata::new(class, "title", &["author"]),
            components: vec![],
            commands: HashMap::new(),
            // labels: HashSet::new(),
            img: true,
            href: true,
            scratch: false,
            graphics_path: None,
        };
        out.new_package(package!("graphicx"));
        out.new_package(package!("hyperref"));
        out
    }

    pub fn get_command(&self, cmd: &str) -> TexResult<Command> {
        match self.commands.get(cmd) {
            Some(s) => Ok(s.clone()),
            None => Err(TexError::Undefined.into()),
        }
    }

    pub fn scratch(&mut self) {
        self.scratch = true;
    }

    pub fn new_command(&mut self, c: Command) {
        self.commands.insert(c.name.clone(), c);
    }

    pub fn new_component(&mut self, new: Component) {
        self.components.push(new);
    }

    pub fn set_md(&mut self, title: &str, author: &[&str]) {
        self.metadata.title = title.to_string();
        self.metadata.author = author.iter().map(|x| x.to_string()).collect();
    }

    pub fn new_package(&mut self, new: Package) {
        self.packages.push(new);
    }

    pub fn enable_graphicx(&mut self, path: &str) {
        self.img = true;
        self.new_package(package!("graphicx"));
        self.graphics_path = Some(vec![path.to_string()]);
    }

    pub fn disable_graphicx(&mut self) {
        self.img = false;
        self.packages.retain(|x| x.name != "graphicx");
        self.graphics_path = None;
    }

    pub fn enable_hyperref(&mut self) {
        self.href = true;
        self.new_package(package!("hyperref"));
    }

    pub fn disable_hyperref(&mut self) {
        self.href = false;
        self.packages.retain(|x| x.name != "hyperref");
    }

    pub fn push_gpath(&mut self, path: &str) {
        self.graphics_path.as_mut().unwrap().push(path.to_string());
    }
}
impl Opt for Document {
    fn add_option(&mut self, opt: &str) {
        self.metadata.class.add_option(opt);
    }
}
impl Populate for Document {
    fn attach(&mut self, other: Component) -> TexResult<&mut Self> {
        self.new_component(other);
        Ok(self)
    }

    fn attach_vec(&mut self, other: Vec<Component>) -> TexResult<&mut Self> {
        for i in other {
            self.attach(i)?;
        }
        Ok(self)
    }
}