mirror of
https://github.com/lise-henry/crowbook
synced 2024-05-23 21:36:15 +02:00
handles pdf support through pdflatex
This commit is contained in:
parent
15fb7064dd
commit
e60e173356
|
@ -1,6 +1,6 @@
|
|||
extern crate crowbook;
|
||||
|
||||
use crowbook::Book;
|
||||
use crowbook::{Book};
|
||||
use std::env;
|
||||
|
||||
fn main() {
|
||||
|
@ -10,8 +10,16 @@ fn main() {
|
|||
match args.next() {
|
||||
None => println!("Needs the name of a book config file"),
|
||||
Some(ref s) => {
|
||||
let book = Book::new_from_file(s).unwrap();
|
||||
book.render_all().unwrap();
|
||||
match Book::new_from_file(s) {
|
||||
Ok(book) => {
|
||||
if let Err(err) = book.render_all() {
|
||||
println!("{}", err);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
println!("{}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -256,6 +256,17 @@ impl Book {
|
|||
try!(f.write_all(&result.as_bytes()).map_err(|_| Error::Render("problem when writing to LaTeX file")));
|
||||
println!("Successfully generated LaTeX file: {}", file);
|
||||
}
|
||||
if let Some(ref file) = self.output_pdf {
|
||||
if self.verbose {
|
||||
println!("Attempting to generate pdf...");
|
||||
}
|
||||
let mut latex = LatexRenderer::new(&self);
|
||||
let result = try!(latex.render_pdf());
|
||||
if self.verbose {
|
||||
println!("{}", result);
|
||||
}
|
||||
println!("Successfully generated pdf file: {}", file);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
use book::{Book, Number};
|
||||
use error::{Error,Result};
|
||||
use token::Token;
|
||||
use zipper::Zipper;
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use mustache;
|
||||
|
||||
|
@ -19,6 +22,20 @@ impl<'a> LatexRenderer<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Render pdf in a file
|
||||
pub fn render_pdf(&mut self) -> Result<String> {
|
||||
if let Some(ref pdf_file) = self.book.output_pdf {
|
||||
let base_file = try!(Path::new(pdf_file).file_stem().ok_or(Error::Render("could not stem pdf filename")));
|
||||
let tex_file = format!("{}.tex", base_file.to_str().unwrap());
|
||||
let content = try!(self.render_book());
|
||||
let mut zipper = try!(Zipper::new(&self.book.temp_dir));
|
||||
try!(zipper.write(&tex_file, &content.as_bytes()));
|
||||
zipper.generate_pdf(&tex_file, pdf_file)
|
||||
} else {
|
||||
Err(Error::Render("no output pdf file specified in book config"))
|
||||
}
|
||||
}
|
||||
|
||||
/// Render latex in a string
|
||||
pub fn render_book(&mut self) -> Result<String> {
|
||||
let mut content = String::from("");
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
use error::{Error,Result};
|
||||
|
||||
use std::env;
|
||||
use std::str;
|
||||
use std::path::{Path,PathBuf};
|
||||
use std::io::Write;
|
||||
use std::process::Command;
|
||||
use std::fs::{self, File,DirBuilder};
|
||||
use uuid;
|
||||
use std::ops::Drop;
|
||||
|
||||
/// Struct used to create zip (using filesystem and zip command)
|
||||
pub struct Zipper {
|
||||
|
@ -27,7 +29,7 @@ impl Zipper {
|
|||
.create(zipper.path.join("META-INF"));
|
||||
|
||||
if !res.is_ok() {
|
||||
Err(Error::Render("could not create temporary directory for generating epub"))
|
||||
Err(Error::Render("could not create temporary directory "))
|
||||
} else {
|
||||
Ok(zipper)
|
||||
}
|
||||
|
@ -40,27 +42,49 @@ impl Zipper {
|
|||
self.args.push(String::from(file));
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::Render("could not write to temporary file while generating epub"))
|
||||
Err(Error::Render("could not write to temporary file"))
|
||||
}
|
||||
} else {
|
||||
Err(Error::Render("could not create temporary file for generating epub"))
|
||||
Err(Error::Render("could not create temporary file"))
|
||||
}
|
||||
}
|
||||
|
||||
/// generate an epub into given file name
|
||||
pub fn generate_epub(&mut self, file: &str) -> Result<String> {
|
||||
/// run command and copy file name to current dir
|
||||
pub fn run_command(&mut self, mut command: Command, file: &str) -> Result<String> {
|
||||
let dir = try!(env::current_dir().map_err(|_| Error::Render("could not get current directory")));
|
||||
try!(env::set_current_dir(&self.path).map_err(|_| Error::Render("could not change current directory")));
|
||||
|
||||
let output = Command::new("zip")
|
||||
.arg("-X")
|
||||
.arg(file)
|
||||
.args(&self.args)
|
||||
let output = command.args(&self.args)
|
||||
.output()
|
||||
.unwrap_or_else(|e| { panic!("failed to execute process: {}", e) });
|
||||
try!(env::set_current_dir(dir).map_err(|_| Error::Render("could not change back to old directory")));
|
||||
try!(fs::copy(self.path.join(file), file).map_err(|_| Error::Render("could not copy epub file")));
|
||||
try!(fs::remove_dir_all(&self.path).map_err(|_| Error::Render("could not delete temporary directory")));
|
||||
String::from_utf8(output.stdout).map_err(|_| Error::Render("invalid utf-8 code in command output"))
|
||||
try!(fs::copy(self.path.join(file), file).map_err(|_| {
|
||||
println!("{}", str::from_utf8(&output.stdout).unwrap());
|
||||
Error::Render("could not copy file")
|
||||
}));
|
||||
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
|
||||
}
|
||||
|
||||
/// generate a pdf file into given file name
|
||||
pub fn generate_pdf(&mut self, tex_file: &str, pdf_file: &str) -> Result<String> {
|
||||
let mut command = Command::new("pdflatex");
|
||||
command.arg(tex_file);
|
||||
self.run_command(command, pdf_file)
|
||||
}
|
||||
|
||||
/// generate an epub into given file name
|
||||
pub fn generate_epub(&mut self, file: &str) -> Result<String> {
|
||||
let mut command = Command::new("zip");
|
||||
command.arg("-X");
|
||||
command.arg(file);
|
||||
self.run_command(command, file)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Zipper {
|
||||
fn drop(&mut self) {
|
||||
if !fs::remove_dir_all(&self.path).is_ok() {
|
||||
println!("Error in zipper: could not delete temporary directory");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue