Skip to content

Commit

Permalink
Add template.tex file for all LaTeX data.
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeckman314 committed Aug 25, 2019
1 parent 8eddbe1 commit c0f6d6e
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 193 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

ci/book
ci/mdBook
4 changes: 1 addition & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ mdbook = "0.3.1"
serde = "1.0.98"
serde_derive = "1.0.98"
tectonic = "0.1.11"
md2tex = "0.1.2"
#md2tex = { path = "/home/liam/Documents/code/projects/md2tex" }
#md2tex = "0.1.2"
md2tex = { path = "/home/liam/Documents/code/projects/md2tex" }
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,11 @@ Below is a list of features I am currently working on (loosely in a "top-down" d
- [x] Configure [resvg](https://github.com/RazrFalcon/resvg) library to convert SVG's to PNG.
- [ ] Save SVG's in `book/latex` directory to keep `src` clean.
- [x] Add CI/CD pipeline ([travis](https://travis-ci.org/))
- [x] Move all LaTeX data to single template file (src/template.tex).
- [ ] Compile *The Rust Book* and *mdbook* documentation without any errors or warnings (e.g. missing Unicode characters). See [Status of Rust Bookshelf](#status-of-rust-bookshelf) for up to date progress.
- [ ] Put "tectonic" dependency in "pdf" feature configuration.
- [ ] Add "table of contents" mdbook toml option.
- [ ] Add "markdown" mdbook toml option.
- [x] Add "markdown" mdbook toml option.
- [ ] Add "number of words" mdbook toml option.
- [ ] Add "examples" directory.
- [ ] Create documentation and move relevent docs to md2tex.
Expand Down
86 changes: 0 additions & 86 deletions src/header.tex

This file was deleted.

106 changes: 47 additions & 59 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern crate md2tex;
extern crate tectonic;

//use latex::*;
use md2tex::markdown_to_latex;
use md2tex::markdown_to_tex;
use mdbook::book::BookItem;
use mdbook::renderer::RenderContext;
use std::error::Error;
Expand All @@ -24,24 +24,19 @@ use std::path::Path;
pub struct LatexConfig {
// chapters that will not be exported.
pub ignores: Vec<String>,

// output latex file.
pub latex: bool,

// output PDF.
pub pdf: bool,

// output markdown file.
pub markdown: bool,
}

// TODO move these latex parts to seperate file.
pub const LATEX_BEGIN: &str = r#"
\begin{document}
\maketitle
\clearpage
\tableofcontents
\clearpage
"#;

pub const LATEX_FOOTER: &str = "\n\\end{document}\n";
// TODO use user's LaTeX template file instead of default (template.tex).
// pub custom_template: bool,
}

fn main() -> std::io::Result<()> {
let mut stdin = io::stdin();
Expand All @@ -59,17 +54,10 @@ fn main() -> std::io::Result<()> {
let title = ctx.config.book.title.unwrap();
let authors = ctx.config.book.authors.join(" \\and ");

let mut latex = String::new();

let latex_header = include_str!("header.tex");
let latex_languages = include_str!("languages.tex");
// copy template data into memory.
let mut template = include_str!("template.tex").to_string();

latex.push_str(&latex_header);
latex.push_str(&latex_languages);
latex.push_str(&format!("\\title{{{title}}}", title = title));
latex.push_str(&format!("\\author{{{authors}}}", authors = authors));

latex.push_str(&LATEX_BEGIN);
let mut latex = String::new();

// iterate through markdown source.
let mut content = String::new();
Expand All @@ -84,54 +72,31 @@ fn main() -> std::io::Result<()> {
}
}

// output markdown file.
if cfg.markdown {
let mut file_md = title.clone();
file_md.push_str(".md");
let path = Path::new(&file_md);
let display = path.display();
let mut _file = match File::create(&file_md) {
Err(why) => panic!("couldn't create {}: {}", display, why.description()),
Ok(file) => file,
};

// write to file.
match _file.write_all(content.as_bytes()) {
Err(why) => panic!("couldn't write to {}: {}", display, why.description()),
Ok(_) => println!("successfully wrote to {}", display),
}
// output markdown file.
output(".md".to_string(), title.clone(), &latex, &ctx.destination);
}

latex.push_str(&markdown_to_latex(content.to_string()));
latex.push_str(&LATEX_FOOTER);
if cfg.latex || cfg.pdf {
// convert markdown data to LaTeX
latex.push_str(&markdown_to_tex(content.to_string()));

// insert new LaTeX data into template after "%% mdbook-latex begin".
let begin = "mdbook-latex begin";
let pos = template.find(&begin).unwrap() + begin.len();
template.insert_str(pos, &latex);
}

// output latex file.
if cfg.latex {
let mut file_latex = title.clone();
file_latex.push_str(".tex");
let path = Path::new(&file_latex);
let display = path.display();

// create output directory/file.
let _ = fs::create_dir_all(&ctx.destination);

let mut file = match File::create(&path) {
Err(why) => panic!("couldn't create {}: {}", display, why.description()),
Ok(file) => file,
};

// write to file.
match file.write_all(latex.as_bytes()) {
Err(why) => panic!("couldn't write to {}: {}", display, why.description()),
Ok(_) => println!("successfully wrote to {}", display),
}
// output latex file.
output(".tex".to_string(), title.clone(), &template, &ctx.destination);
}

// output PDF file.
if cfg.pdf {
// write PDF with tectonic.
println!("Writing PDF with Tectonic...");
let pdf_data: Vec<u8> = tectonic::latex_to_pdf(latex).expect("processing failed");
let pdf_data: Vec<u8> = tectonic::latex_to_pdf(&template).expect("processing failed");
println!("Output PDF size is {} bytes", pdf_data.len());

let mut pos = 0;
Expand All @@ -148,3 +113,26 @@ fn main() -> std::io::Result<()> {

Ok(())
}

/// Output plain text file.
///
/// Used for writing markdown and latex data to files.
fn output<P: AsRef<Path>>(extension: String, mut filename: String, data: &String, destination: P) {
filename.push_str(&extension);
let path = Path::new(&filename);
let display = path.display();

// create output directory/file.
let _ = fs::create_dir_all(destination);

let mut file = match File::create(&path) {
Err(why) => panic!("Couldn't create {}: {}", display, why.description()),
Ok(file) => file,
};

// write to file.
match file.write_all(data.as_bytes()) {
Err(why) => panic!("Couldn't write to {}: {}", display, why.description()),
Ok(_) => println!("Successfully wrote to {}", display),
}
}
Loading

0 comments on commit c0f6d6e

Please sign in to comment.