Merge pull request #195 from mbrubeck/refactor
Various refactoring and cleanup
This commit is contained in:
commit
fe8d46b8e6
|
@ -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;
|
||||||
|
@ -81,47 +81,27 @@ impl Renderer for HtmlHandlebars {
|
||||||
content = utils::render_markdown(&content);
|
content = utils::render_markdown(&content);
|
||||||
print_content.push_str(&content);
|
print_content.push_str(&content);
|
||||||
|
|
||||||
// Remove content from previous file and render content for this one
|
// Update the context with data for this file
|
||||||
data.remove("path");
|
let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other,
|
||||||
match ch.path.to_str() {
|
"Could not convert path to str"))?;
|
||||||
Some(p) => {
|
data.insert("path".to_owned(), path.to_json());
|
||||||
data.insert("path".to_owned(), p.to_json());
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other,
|
|
||||||
"Could not convert path to str")))
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove content from previous file and render content for this one
|
|
||||||
data.remove("content");
|
|
||||||
data.insert("content".to_owned(), content.to_json());
|
data.insert("content".to_owned(), content.to_json());
|
||||||
|
|
||||||
// Remove chapter title from previous file and add title for this one
|
|
||||||
data.remove("chapter_title");
|
|
||||||
data.insert("chapter_title".to_owned(), ch.name.to_json());
|
data.insert("chapter_title".to_owned(), ch.name.to_json());
|
||||||
|
|
||||||
// Remove path to root from previous file and render content for this one
|
|
||||||
data.remove("path_to_root");
|
|
||||||
data.insert("path_to_root".to_owned(), utils::fs::path_to_root(&ch.path).to_json());
|
data.insert("path_to_root".to_owned(), utils::fs::path_to_root(&ch.path).to_json());
|
||||||
|
|
||||||
// Rendere 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));
|
||||||
|
|
||||||
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);
|
||||||
|
@ -133,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")));
|
||||||
|
@ -147,132 +127,34 @@ impl Renderer for HtmlHandlebars {
|
||||||
|
|
||||||
// Print version
|
// Print version
|
||||||
|
|
||||||
// Remove content from previous file and render content for this one
|
// Update the context with data for this file
|
||||||
data.remove("path");
|
|
||||||
data.insert("path".to_owned(), "print.md".to_json());
|
data.insert("path".to_owned(), "print.md".to_json());
|
||||||
|
|
||||||
// Remove content from previous file and render content for this one
|
|
||||||
data.remove("content");
|
|
||||||
data.insert("content".to_owned(), print_content.to_json());
|
data.insert("content".to_owned(), print_content.to_json());
|
||||||
|
|
||||||
// Remove path to root from previous file and render content for this one
|
|
||||||
data.remove("path_to_root");
|
|
||||||
data.insert("path_to_root".to_owned(), utils::fs::path_to_root(Path::new("print.md")).to_json());
|
data.insert("path_to_root".to_owned(), utils::fs::path_to_root(Path::new("print.md")).to_json());
|
||||||
|
|
||||||
// Rendere 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"]));
|
||||||
|
@ -302,22 +184,16 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
|
||||||
match *item {
|
match *item {
|
||||||
BookItem::Affix(ref ch) => {
|
BookItem::Affix(ref ch) => {
|
||||||
chapter.insert("name".to_owned(), ch.name.to_json());
|
chapter.insert("name".to_owned(), ch.name.to_json());
|
||||||
match ch.path.to_str() {
|
let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other,
|
||||||
Some(p) => {
|
"Could not convert path to str"))?;
|
||||||
chapter.insert("path".to_owned(), p.to_json());
|
chapter.insert("path".to_owned(), path.to_json());
|
||||||
},
|
|
||||||
None => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
BookItem::Chapter(ref s, ref ch) => {
|
BookItem::Chapter(ref s, ref ch) => {
|
||||||
chapter.insert("section".to_owned(), s.to_json());
|
chapter.insert("section".to_owned(), s.to_json());
|
||||||
chapter.insert("name".to_owned(), ch.name.to_json());
|
chapter.insert("name".to_owned(), ch.name.to_json());
|
||||||
match ch.path.to_str() {
|
let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other,
|
||||||
Some(p) => {
|
"Could not convert path to str"))?;
|
||||||
chapter.insert("path".to_owned(), p.to_json());
|
chapter.insert("path".to_owned(), path.to_json());
|
||||||
},
|
|
||||||
None => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
BookItem::Spacer => {
|
BookItem::Spacer => {
|
||||||
chapter.insert("spacer".to_owned(), "_spacer_".to_json());
|
chapter.insert("spacer".to_owned(), "_spacer_".to_json());
|
||||||
|
|
|
@ -69,7 +69,7 @@ pub fn path_to_root(path: &Path) -> String {
|
||||||
/// This function creates a file and returns it. But before creating the file it checks every
|
/// This function creates a file and returns it. But before creating the file it checks every
|
||||||
/// directory in the path to see if it exists, and if it does not it will be created.
|
/// directory in the path to see if it exists, and if it does not it will be created.
|
||||||
|
|
||||||
pub fn create_file(path: &Path) -> Result<File, Box<Error>> {
|
pub fn create_file(path: &Path) -> io::Result<File> {
|
||||||
debug!("[fn]: create_file");
|
debug!("[fn]: create_file");
|
||||||
|
|
||||||
// Construct path
|
// Construct path
|
||||||
|
@ -80,15 +80,7 @@ pub fn create_file(path: &Path) -> Result<File, Box<Error>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!("[*]: Create file: {:?}", path);
|
debug!("[*]: Create file: {:?}", path);
|
||||||
let f = match File::create(path) {
|
File::create(path)
|
||||||
Ok(f) => f,
|
|
||||||
Err(e) => {
|
|
||||||
debug!("File::create: {}", e);
|
|
||||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, format!("{}", e))));
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(f)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes all the content of a directory but not the directory itself
|
/// Removes all the content of a directory but not the directory itself
|
||||||
|
|
Loading…
Reference in New Issue