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
use std::fmt::{self, Display, Formatter};
use std::io::Write;
use std::ops::Deref;

use paragraph::Paragraph;
use section::Section;
use equations::Align;
use errors::*;
use lists::List;
use super::Renderable;

/// The root Document node.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Document {
    /// The document class.
    pub class: DocumentClass,
    /// The `Document`'s preamble.
    pub preamble: Preamble,
    /// The various elements inside this `Document`.
    pub elements: Vec<Element>,
}

impl Document {
    /// Create a new `Document` with the specified `DocumentClass`.
    pub fn new(document_class: DocumentClass) -> Self {
        Document {
            class: document_class,
            ..Default::default()
        }
    }

    /// Add an element to the `Document`.
    ///
    /// For convenience, Elements are automatically converted using `into()`.
    pub fn push<E>(&mut self, element: E) -> &mut Self
        where E: Into<Element>
    {
        self.elements.push(element.into());
        self
    }
}

impl Deref for Document {
    type Target = Vec<Element>;

    /// A shortcut to let you iterate over the elements in the `Document`.
    fn deref(&self) -> &Self::Target {
        &self.elements
    }
}

impl Renderable for Document {
    fn render<W>(&self, writer: &mut W) -> Result<()>
        where W: Write
    {
        writeln!(writer, r"\documentclass{{{}}}", self.class)?;

        self.preamble.render(writer)?;

        writeln!(writer, r"\begin{{document}}")?;

        for element in &self.elements {
            element.render(writer)?;
        }

        writeln!(writer, r"\end{{document}}")?;
        Ok(())
    }
}

/// The major elements in a `Document`.
#[derive(Clone, Debug, PartialEq)]
pub enum Element {
    /// A bare paragraph.
    ///
    /// # Note
    ///
    /// You probably don't want to add a paragraph directly to your document,
    /// instead add it to a `Section` so that if you are walking the AST later
    /// on things make sense.
    Para(Paragraph),
    /// A section.
    Section(Section),
    /// The table of contents.
    TableOfContents,
    /// The title page.
    TitlePage,
    /// Clear the page.
    ClearPage,
    /// An `align` environment for containing a bunch of equations.
    Align(Align),

    /// A generic environment and its lines.
    Environment(String, Vec<String>),

    /// Any other element.
    ///
    /// This can be used as an escape hatch if the particular element you want
    /// isn't directly supported or if you need to do something which isn't
    /// easily expressed any other way. You simply provide the raw string you
    /// want and it will be rendered unchanged in the final document.
    UserDefined(String),
    /// A list.
    List(List),

    // Add a dummy element so we can expand later on without breaking stuff
    #[doc(hidden)]
    _Other,
}

impl From<Section> for Element {
    fn from(other: Section) -> Self {
        Element::Section(other)
    }
}

impl Renderable for Element {
    fn render<W>(&self, writer: &mut W) -> Result<()>
        where W: Write
    {
        match *self {
            Element::Para(ref p) => p.render(writer)?,
            Element::Section(ref s) => s.render(writer)?,
            Element::TableOfContents => writeln!(writer, r"\tableofcontents")?,
            Element::TitlePage => writeln!(writer, r"\maketitle")?,
            Element::ClearPage => writeln!(writer, r"\clearpage")?,
            Element::UserDefined(ref s) => writeln!(writer, "{}", s)?,
            Element::Align(ref equations) => equations.render(writer)?,

            Element::Environment(ref name, ref lines) => {
                writeln!(writer, r"\begin{{{}}}", name)?;
                for line in lines {
                    writeln!(writer, "{}", line)?;
                }
                writeln!(writer, r"\end{{{}}}", name)?;
            }
            Element::List(ref list) => list.render(writer)?,

            Element::_Other => unreachable!(),
        }

        Ok(())
    }
}

/// The kind of Document being generated.
#[derive(Clone, Debug, PartialEq)]
#[allow(missing_docs)]
pub enum DocumentClass {
    Article,
    Book,
    Report,
}

impl Default for DocumentClass {
    fn default() -> Self {
        DocumentClass::Article
    }
}

impl Display for DocumentClass {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match *self {
            DocumentClass::Article => write!(f, "article"),
            DocumentClass::Book => write!(f, "book"),
            DocumentClass::Report => write!(f, "report"),
        }
    }
}


/// A node representing the document's preamble.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Preamble {
    author: Option<String>,
    title: Option<String>,
    uses: Vec<String>,
}

impl Preamble {
    /// Set the document's author.
    pub fn author(&mut self, name: &str) -> &mut Self {
        self.author = Some(name.to_string());
        self
    }

    /// Set the document title.
    pub fn title(&mut self, name: &str) -> &mut Self {
        self.title = Some(name.to_string());
        self
    }

    /// Add a package import to the preamble.
    pub fn use_package(&mut self, name: &str) -> &mut Self {
        self.uses.push(name.to_string());
        self
    }
}

impl Renderable for Preamble {
    fn render<W: Write>(&self, writer: &mut W) -> Result<()> {
        for item in &self.uses {
            writeln!(writer, r"\usepackage{{{}}}", item)?;
        }

        if !self.uses.is_empty() && (self.title.is_some() || self.author.is_some()) {
            writeln!(writer)?;
        }

        if let Some(ref title) = self.title {
            writeln!(writer, r"\title{{{}}}", title)?;
        }
        if let Some(ref author) = self.author {
            writeln!(writer, r"\author{{{}}}", author)?;
        }

        Ok(())
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn render_empty_document() {
        let should_be = r#"\documentclass{article}
\begin{document}
\end{document}
"#;

        let doc = Document::new(DocumentClass::Article);
        let mut rendered = vec![];
        doc.render(&mut rendered).unwrap();

        assert_eq!(String::from_utf8(rendered).unwrap(), should_be);
    }

    #[test]
    fn preamble_with_author_and_title() {
        let should_be = r#"\title{Sample Document}
\author{Michael-F-Bryan}
"#;
        let mut preamble = Preamble::default();
        preamble.title("Sample Document").author("Michael-F-Bryan");

        let mut rendered = vec![];
        preamble.render(&mut rendered).unwrap();

        assert_eq!(String::from_utf8(rendered).unwrap(), should_be);
    }

    #[test]
    fn preamble_with_title_and_package_imports() {
        let should_be = r#"\usepackage{amsmath}
\usepackage{graphics}

\title{Sample Document}
"#;
        let mut preamble = Preamble::default();
        preamble
            .title("Sample Document")
            .use_package("amsmath")
            .use_package("graphics");

        let mut rendered = vec![];
        preamble.render(&mut rendered).unwrap();

        assert_eq!(String::from_utf8(rendered).unwrap(), should_be);
    }
}