carlo_latex/
lib.rs

1//! Defines the LATEX subcommand.
2
3use carlotk::prelude::*;
4
5/// LaTeX header
6const HEADER: &str = include_str!("latex_header.tex");
7
8/// LaTeX footer
9const FOOTER: &str = include_str!("latex_footer.tex");
10
11/// Help menu
12const HELP: &str = include_str!("../help_latex.txt");
13
14/// Provide help to the user
15pub fn helpme() {
16    printhelp(HELP);
17}
18
19pub fn latex(args: CliArgs) {   
20    let inputfile = args.inputfile.clone();
21    let debug = args.contains(Flag::Debug);
22
23    // Create input method
24    let mut rl = DefaultEditor::new().unwrap();
25
26    // Get title
27    let title = match rl.readline("Title >> ") {
28        Ok (r) => r,
29        Err (_) => Error::CouldNotReadLine ("Title").throw(),
30    };
31
32    // Get author
33    let author = match rl.readline("Author >> ") {
34        Ok (r) => r,
35        Err (_) => Error::CouldNotReadLine ("Author").throw(),
36    };
37
38    let mut output = String::new();
39
40    let mut outputfile = match inputfile.clone() {
41        Some (f) => f,
42        _ => Error::NoInputFile::<&str>.throw(),
43    };
44
45    // Output header
46    output.push_str(HEADER);
47
48    // Output title and author
49    output.push_str(&format!(
50        "\\title{{\\textbf{{{}}}}}\n",
51        title,
52    ));
53    output.push_str(&format!(
54        "\\author{{{}}}\n",
55        author,
56    ));
57    output.push_str("\\maketitle\n");
58    output.push_str("\\tableofcontents\n");
59
60    // Parse and evaluate code
61    let expressions = parse(inputfile, debug);
62    let mut env = Environment::new();
63    let latex = env.latex_evaluate(&expressions);
64
65    // Output code
66    output.push_str(&latex);
67
68    // Output footer
69    output.push_str(FOOTER);
70
71    // Write to output file
72    outputfile.set_extension("tex");
73    let _ = fs::write(&outputfile, output);
74
75    println!("\nOutput written to {}", outputfile.display());
76}