Factor common io error handling out of renderer
This commit is contained in:
parent
894a03655e
commit
f2b87f7944
|
@ -273,6 +273,16 @@ impl MDBook {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn write_file<P: AsRef<Path>>(&self, filename: P, content: &[u8]) -> Result<(), Box<Error>> {
|
||||||
|
let path = self.get_dest().join(filename);
|
||||||
|
try!(utils::fs::create_file(&path).and_then(|mut file| {
|
||||||
|
file.write_all(content)
|
||||||
|
}).map_err(|e| {
|
||||||
|
io::Error::new(io::ErrorKind::Other, format!("Could not create {}: {}", path.display(), e))
|
||||||
|
}));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Parses the `book.json` file (if it exists) to extract the configuration parameters.
|
/// Parses the `book.json` file (if it exists) to extract the configuration parameters.
|
||||||
/// The `book.json` file should be in the root directory of the book.
|
/// The `book.json` file should be in the root directory of the book.
|
||||||
/// The root directory is the one specified when creating a new `MDBook`
|
/// The root directory is the one specified when creating a new `MDBook`
|
||||||
|
|
|
@ -7,7 +7,7 @@ use {utils, theme};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{self, Read};
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use handlebars::Handlebars;
|
use handlebars::Handlebars;
|
||||||
|
@ -93,19 +93,15 @@ impl Renderer for HtmlHandlebars {
|
||||||
debug!("[*]: Render template");
|
debug!("[*]: Render template");
|
||||||
let rendered = try!(handlebars.render("index", &data));
|
let rendered = try!(handlebars.render("index", &data));
|
||||||
|
|
||||||
debug!("[*]: Create file {:?}", &book.get_dest().join(&ch.path).with_extension("html"));
|
|
||||||
// Write to file
|
// Write to file
|
||||||
let mut file =
|
let filename = Path::new(&ch.path).with_extension("html");
|
||||||
try!(utils::fs::create_file(&book.get_dest().join(&ch.path).with_extension("html")));
|
info!("[*] Creating {:?} ✓", filename.display());
|
||||||
info!("[*] Creating {:?} ✓", &book.get_dest().join(&ch.path).with_extension("html"));
|
try!(book.write_file(filename, &rendered.into_bytes()));
|
||||||
|
|
||||||
try!(file.write_all(&rendered.into_bytes()));
|
|
||||||
|
|
||||||
// Create an index.html from the first element in SUMMARY.md
|
// Create an index.html from the first element in SUMMARY.md
|
||||||
if index {
|
if index {
|
||||||
debug!("[*]: index.html");
|
debug!("[*]: index.html");
|
||||||
|
|
||||||
let mut index_file = try!(File::create(book.get_dest().join("index.html")));
|
|
||||||
let mut content = String::new();
|
let mut content = String::new();
|
||||||
let _source = try!(File::open(book.get_dest().join(&ch.path.with_extension("html"))))
|
let _source = try!(File::open(book.get_dest().join(&ch.path.with_extension("html"))))
|
||||||
.read_to_string(&mut content);
|
.read_to_string(&mut content);
|
||||||
|
@ -117,7 +113,7 @@ impl Renderer for HtmlHandlebars {
|
||||||
.collect::<Vec<&str>>()
|
.collect::<Vec<&str>>()
|
||||||
.join("\n");
|
.join("\n");
|
||||||
|
|
||||||
try!(index_file.write_all(content.as_bytes()));
|
try!(book.write_file("index.html", content.as_bytes()));
|
||||||
|
|
||||||
info!("[*] Creating index.html from {:?} ✓",
|
info!("[*] Creating index.html from {:?} ✓",
|
||||||
book.get_dest().join(&ch.path.with_extension("html")));
|
book.get_dest().join(&ch.path.with_extension("html")));
|
||||||
|
@ -139,117 +135,26 @@ impl Renderer for HtmlHandlebars {
|
||||||
// Render the handlebars template with the data
|
// Render the handlebars template with the data
|
||||||
debug!("[*]: Render template");
|
debug!("[*]: Render template");
|
||||||
let rendered = try!(handlebars.render("index", &data));
|
let rendered = try!(handlebars.render("index", &data));
|
||||||
let mut file = try!(utils::fs::create_file(&book.get_dest().join("print").with_extension("html")));
|
try!(book.write_file(Path::new("print").with_extension("html"), &rendered.into_bytes()));
|
||||||
try!(file.write_all(&rendered.into_bytes()));
|
|
||||||
info!("[*] Creating print.html ✓");
|
info!("[*] Creating print.html ✓");
|
||||||
|
|
||||||
// Copy static files (js, css, images, ...)
|
// Copy static files (js, css, images, ...)
|
||||||
|
|
||||||
debug!("[*] Copy static files");
|
debug!("[*] Copy static files");
|
||||||
// JavaScript
|
try!(book.write_file("book.js", &theme.js));
|
||||||
let mut js_file = if let Ok(f) = File::create(book.get_dest().join("book.js")) {
|
try!(book.write_file("book.css", &theme.css));
|
||||||
f
|
try!(book.write_file("favicon.png", &theme.favicon));
|
||||||
} else {
|
try!(book.write_file("jquery.js", &theme.jquery));
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create book.js")));
|
try!(book.write_file("highlight.css", &theme.highlight_css));
|
||||||
};
|
try!(book.write_file("tomorrow-night.css", &theme.tomorrow_night_css));
|
||||||
try!(js_file.write_all(&theme.js));
|
try!(book.write_file("highlight.js", &theme.highlight_js));
|
||||||
|
try!(book.write_file("_FontAwesome/css/font-awesome.css", theme::FONT_AWESOME));
|
||||||
// Css
|
try!(book.write_file("_FontAwesome/fonts/fontawesome-webfont.eot", theme::FONT_AWESOME_EOT));
|
||||||
let mut css_file = if let Ok(f) = File::create(book.get_dest().join("book.css")) {
|
try!(book.write_file("_FontAwesome/fonts/fontawesome-webfont.svg", theme::FONT_AWESOME_SVG));
|
||||||
f
|
try!(book.write_file("_FontAwesome/fonts/fontawesome-webfont.ttf", theme::FONT_AWESOME_TTF));
|
||||||
} else {
|
try!(book.write_file("_FontAwesome/fonts/fontawesome-webfont.woff", theme::FONT_AWESOME_WOFF));
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create book.css")));
|
try!(book.write_file("_FontAwesome/fonts/fontawesome-webfont.woff2", theme::FONT_AWESOME_WOFF2));
|
||||||
};
|
try!(book.write_file("_FontAwesome/fonts/FontAwesome.ttf", theme::FONT_AWESOME_TTF));
|
||||||
try!(css_file.write_all(&theme.css));
|
|
||||||
|
|
||||||
// Favicon
|
|
||||||
let mut favicon_file = if let Ok(f) = File::create(book.get_dest().join("favicon.png")) {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create favicon.png")));
|
|
||||||
};
|
|
||||||
try!(favicon_file.write_all(&theme.favicon));
|
|
||||||
|
|
||||||
// JQuery local fallback
|
|
||||||
let mut jquery = if let Ok(f) = File::create(book.get_dest().join("jquery.js")) {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create jquery.js")));
|
|
||||||
};
|
|
||||||
try!(jquery.write_all(&theme.jquery));
|
|
||||||
|
|
||||||
// syntax highlighting
|
|
||||||
let mut highlight_css = if let Ok(f) = File::create(book.get_dest().join("highlight.css")) {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create highlight.css")));
|
|
||||||
};
|
|
||||||
try!(highlight_css.write_all(&theme.highlight_css));
|
|
||||||
|
|
||||||
let mut tomorrow_night_css = if let Ok(f) = File::create(book.get_dest().join("tomorrow-night.css")) {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create tomorrow-night.css")));
|
|
||||||
};
|
|
||||||
try!(tomorrow_night_css.write_all(&theme.tomorrow_night_css));
|
|
||||||
|
|
||||||
let mut highlight_js = if let Ok(f) = File::create(book.get_dest().join("highlight.js")) {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create highlight.js")));
|
|
||||||
};
|
|
||||||
try!(highlight_js.write_all(&theme.highlight_js));
|
|
||||||
|
|
||||||
// Font Awesome local fallback
|
|
||||||
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
|
||||||
.join("_FontAwesome/css/font-awesome.css")) {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create font-awesome.css")));
|
|
||||||
};
|
|
||||||
try!(font_awesome.write_all(theme::FONT_AWESOME));
|
|
||||||
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
|
||||||
.join("_FontAwesome/fonts/fontawesome-webfont.eot")) {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.eot")));
|
|
||||||
};
|
|
||||||
try!(font_awesome.write_all(theme::FONT_AWESOME_EOT));
|
|
||||||
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
|
||||||
.join("_FontAwesome/fonts/fontawesome-webfont.svg")) {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.svg")));
|
|
||||||
};
|
|
||||||
try!(font_awesome.write_all(theme::FONT_AWESOME_SVG));
|
|
||||||
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
|
||||||
.join("_FontAwesome/fonts/fontawesome-webfont.ttf")) {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.ttf")));
|
|
||||||
};
|
|
||||||
try!(font_awesome.write_all(theme::FONT_AWESOME_TTF));
|
|
||||||
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
|
||||||
.join("_FontAwesome/fonts/fontawesome-webfont.woff")) {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.woff")));
|
|
||||||
};
|
|
||||||
try!(font_awesome.write_all(theme::FONT_AWESOME_WOFF));
|
|
||||||
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
|
||||||
.join("_FontAwesome/fonts/fontawesome-webfont.woff2")) {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.woff2")));
|
|
||||||
};
|
|
||||||
try!(font_awesome.write_all(theme::FONT_AWESOME_WOFF2));
|
|
||||||
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
|
||||||
.join("_FontAwesome/fonts/FontAwesome.ttf")) {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create FontAwesome.ttf")));
|
|
||||||
};
|
|
||||||
try!(font_awesome.write_all(theme::FONT_AWESOME_TTF));
|
|
||||||
|
|
||||||
// Copy all remaining files
|
// Copy all remaining files
|
||||||
try!(utils::fs::copy_files_except_ext(book.get_src(), book.get_dest(), true, &["md"]));
|
try!(utils::fs::copy_files_except_ext(book.get_src(), book.get_dest(), true, &["md"]));
|
||||||
|
|
Loading…
Reference in New Issue