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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use std::{collections::HashMap, fmt::Display};

use itertools::Itertools;

use crate::*;

/// Currently, only these four types are supported.
/// There is also nothing preventing you from putting a \part{} in a document of class "part",
/// but latex will show an error. If you want those restrictions to be implemented, please put
/// up an issue
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DocumentClassType {
    Article,
    Amsart,
    Part,
    Report,
    Book,
    Beamer,
}
impl Display for DocumentClassType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match &self {
                Self::Article => "article",
                Self::Part => "part",
                Self::Report => "report",
                Self::Book => "book",
                Self::Amsart => "amsart",
                Self::Beamer => "beamer",
            }
        )?;

        Ok(())
    }
}
impl From<&str> for DocumentClassType {
    fn from(value: &str) -> Self {
        match value {
            "article" => Self::Article,
            "part" => Self::Part,
            "book" => Self::Book,
            "report" => Self::Report,
            "amsart" => Self::Amsart,
            "beamer" => Self::Beamer,
            _ => Self::Article,
        }
    }
}

/// Wrapper around `DocumentClassType`, contains whatever options you want to add.
/// Nothing prevents you from adding absolute gibberish as an option. If you want those
/// restrictions implemented, please put up an issue.
#[derive(Debug, Clone)]
pub struct DocumentClass {
    typ: DocumentClassType,
    opt: Vec<String>,
}
impl DocumentClass {
    pub fn new(typ: &str) -> Self {
        Self {
            typ: typ.into(),
            opt: vec![],
        }
    }
}
impl AsLatex for DocumentClass {
    fn to_string(&self) -> String {
        let options = self
            .opt
            .iter()
            .map(|s| format!("{}, ", s))
            .collect::<String>();
        format!("\\documentclass[{}]{{{}}}", options, self.typ.to_string())
    }
}
impl Opt for DocumentClass {
    fn add_option(&mut self, opt: &str) {
        self.opt.push(opt.to_string())
    }
}

/// This here is the main reason I made this crate - other crates don't let you add options
/// to packages. Has macro support, to please use it :).
#[derive(Debug, Clone)]
pub struct Package {
    name: String,
    opt: Vec<String>,
}
impl AsLatex for Package {
    fn to_string(&self) -> String {
        let options = self
            .opt
            .iter()
            .map(|s| format!("{}, ", s))
            .collect::<String>();
        format!("\\usepackage[{}]{{{}}}\n", options, self.name)
    }
}
impl Opt for Package {
    fn add_option(&mut self, opt: &str) {
        self.opt.push(opt.to_string())
    }
}
impl Package {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            opt: vec![],
        }
    }
}

/// Title and author, if y'all want more, please put up an issue.
#[derive(Debug, Clone)]
pub struct Metadata {
    class: DocumentClass,
    title: String,
    author: Vec<String>,
    pub maketitle: bool,
    pub tableofcontents: bool,
    pub date: bool,
}
impl AsLatex for Metadata {
    fn to_string(&self) -> String {
        let title_author = format!(
            "\\title{{{}}}\n\\author{{{}}}\n",
            self.title,
            self.author
                .iter()
                .join(r"\\ \and ")
        );
        match self.class {
            DocumentClass {
                typ: DocumentClassType::Beamer,
                ..
            } => {
                // todo!()
                format!(
                    "{title_author}\n{}\n",
                    if self.date { r"\date{\today}" } else { "" },
                )
            }
            _ => {
                format!(
                    "{title_author}\n{}\n{}\n{}\n",
                    if self.date { r"\today" } else { "" },
                    if self.maketitle { r"\maketitle" } else { "" },
                    if self.tableofcontents {
                        r"\tableofcontents"
                    } else {
                        ""
                    },
                )
            }
        }
    }
}
impl Metadata {
    pub fn new(class: DocumentClass, title: &str, author: &[&str]) -> Self {
        Self {
            class,
            title: title.to_string(),
            author: author.iter().map(|x| x.to_string()).collect(),
            maketitle: true,
            tableofcontents: false,
            date: true,
        }
    }
}

/// 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>,
    img: 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 {
            "".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 {
        Self {
            // document_class: class,
            packages: vec![],
            metadata: Metadata::new(class, "title", &["author"]),
            components: vec![],
            commands: HashMap::new(),
            img: false,
            scratch: false,
            graphics_path: None,
        }
    }

    pub fn get_command(&self, cmd: &str) -> Res<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 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) -> Res<&mut Self> {
        self.new_component(other);
        Ok(self)
    }

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