From 8476a3d03dd47da15c18bb4b9fe55852d454484c Mon Sep 17 00:00:00 2001 From: Adam Solove Date: Mon, 11 Jan 2016 10:08:27 -0500 Subject: [PATCH 1/2] First experiment with building a new renderer. --- src/book/mdbook.rs | 4 ++-- src/renderer/mod.rs | 4 +++- src/renderer/pandoc/mod.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/renderer/pandoc/mod.rs diff --git a/src/book/mdbook.rs b/src/book/mdbook.rs index 2209bc367b..d779ecc579 100644 --- a/src/book/mdbook.rs +++ b/src/book/mdbook.rs @@ -8,7 +8,7 @@ use std::process::Command; use {BookConfig, BookItem, theme, parse, utils}; use book::BookItems; -use renderer::{Renderer, HtmlHandlebars}; +use renderer::{Renderer, HtmlHandlebars, Pandoc}; pub struct MDBook { @@ -38,7 +38,7 @@ impl MDBook { .set_src(&root.join("src")) .set_dest(&root.join("book")) .to_owned(), - renderer: Box::new(HtmlHandlebars::new()), + renderer: Box::new(Pandoc::new()), } } diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 8216db270e..f64b263d30 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -1,5 +1,7 @@ pub use self::renderer::Renderer; pub use self::html_handlebars::HtmlHandlebars; +pub use self::pandoc::Pandoc; pub mod renderer; -mod html_handlebars; +mod pandoc; +mod html_handlebars; \ No newline at end of file diff --git a/src/renderer/pandoc/mod.rs b/src/renderer/pandoc/mod.rs new file mode 100644 index 0000000000..1a4362af3d --- /dev/null +++ b/src/renderer/pandoc/mod.rs @@ -0,0 +1,36 @@ +use renderer::Renderer; +use book::MDBook; +use book::bookitem::BookItem; + +use std::error::Error; + +enum PandocOutput { + Epub, + Pdf +} + +pub struct Pandoc { + output: PandocOutput +} + +impl Pandoc { + pub fn new() -> Pandoc { + Pandoc { output: PandocOutput::Epub } + } +} + +impl Renderer for Pandoc { + fn render(&self, book: &MDBook) -> Result<(), Box> { + + for item in book.iter() { + match *item { + BookItem::Chapter(ref title, _) => { + println!("{}", title) + }, + _ => println!("Something I don't understand") + } + } + + Ok(()) + } +} \ No newline at end of file From 7da175bba308626165fd2d4a387afcd56c66e35c Mon Sep 17 00:00:00 2001 From: Adam Solove Date: Mon, 11 Jan 2016 21:19:22 -0500 Subject: [PATCH 2/2] Get book contents and pass to pandoc for epub version. --- src/renderer/pandoc/mod.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/renderer/pandoc/mod.rs b/src/renderer/pandoc/mod.rs index 1a4362af3d..c428e2b0ec 100644 --- a/src/renderer/pandoc/mod.rs +++ b/src/renderer/pandoc/mod.rs @@ -3,34 +3,40 @@ use book::MDBook; use book::bookitem::BookItem; use std::error::Error; +use std::process::Command; -enum PandocOutput { - Epub, - Pdf -} - -pub struct Pandoc { - output: PandocOutput -} +pub struct Pandoc; impl Pandoc { pub fn new() -> Pandoc { - Pandoc { output: PandocOutput::Epub } + Pandoc } } impl Renderer for Pandoc { fn render(&self, book: &MDBook) -> Result<(), Box> { + let mut paths = vec!(); for item in book.iter() { match *item { - BookItem::Chapter(ref title, _) => { - println!("{}", title) + BookItem::Chapter(_, ref ch) => { + paths.push(book.get_src().join(&ch.path).into_os_string()); }, - _ => println!("Something I don't understand") + _ => println!("FIXME: don't understand this kind of BookItem") } } - Ok(()) + let output = Command::new("pandoc") + .arg("-S") + .arg("-osample.epub") + .args(&paths) + .output(); + + match output { + Ok(_) => Ok(()), + Err(e) => Err(Box::new(e)) + } + // FIXME: why doesn't this work + // output.map(|_| ()).map_err(|e| Box::new(e)) } } \ No newline at end of file