HtmlConfig is no longer optional

`HtmlConfig` was both guaranteed to exist within `BookConfig`
and `expect`ed in few places.
This simplifies the API a little by representing the fact that
`HtmlConfig` is currently mandatory for proper mdBook binary operation.
This commit is contained in:
Michal Budzynski 2017-06-27 14:01:33 +02:00
parent 5a27207844
commit b1ca9cf5b5
11 changed files with 97 additions and 173 deletions

View File

@ -34,10 +34,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
book.build()?;
if let Some(d) = book.get_destination() {
if args.is_present("open") {
open(d.join("index.html"));
}
if args.is_present("open") {
open(book.get_destination().join("index.html"));
}
Ok(())

View File

@ -49,9 +49,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
}
// Because of `src/book/mdbook.rs#L37-L39`, `dest` will always start with `root`
let is_dest_inside_root = book.get_destination()
.map(|p| p.starts_with(book.get_root()))
.unwrap_or(false);
let is_dest_inside_root = book.get_destination().starts_with(book.get_root());
if !args.is_present("force") && is_dest_inside_root {
println!("\nDo you want a .gitignore to be created? (y/n)");

View File

@ -40,11 +40,6 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
None => book,
};
if let None = book.get_destination() {
println!("The HTML renderer is not set up, impossible to serve the files.");
std::process::exit(2);
}
if args.is_present("curly-quotes") {
book = book.with_curly_quotes(true);
}
@ -79,8 +74,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
book.build()?;
let mut chain = Chain::new(staticfile::Static::new(book.get_destination()
.expect("destination is present, checked before")));
let mut chain = Chain::new(staticfile::Static::new(book.get_destination()));
chain.link_after(ErrorRecover);
let _iron = Iron::new(chain).http(&*address).unwrap();

View File

@ -37,9 +37,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
if args.is_present("open") {
book.build()?;
if let Some(d) = book.get_destination() {
open(d.join("index.html"));
}
open(book.get_destination().join("index.html"));
}
trigger_on_change(&mut book, |path, book| {
@ -78,9 +76,7 @@ pub fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> ()
};
// Add the theme directory to the watcher
if let Some(t) = book.get_theme_path() {
watcher.watch(t, Recursive).unwrap_or_default();
}
watcher.watch(book.get_theme_path(), Recursive).unwrap_or_default();
// Add the book.{json,toml} file to the watcher if it exists, because it's not

View File

@ -138,11 +138,9 @@ impl MDBook {
{
if let Some(htmlconfig) = self.config.get_html_config() {
if !htmlconfig.get_destination().exists() {
debug!("[*]: {:?} does not exist, trying to create directory", htmlconfig.get_destination());
fs::create_dir_all(htmlconfig.get_destination())?;
}
if !self.get_destination().exists() {
debug!("[*]: {:?} does not exist, trying to create directory", self.get_destination());
fs::create_dir_all(self.get_destination())?;
}
@ -203,11 +201,7 @@ impl MDBook {
pub fn create_gitignore(&self) {
let gitignore = self.get_gitignore();
// If the HTML renderer is not set, return
if self.config.get_html_config().is_none() { return; }
let destination = self.config.get_html_config()
.expect("The HtmlConfig does exist, checked just before")
.get_destination();
// Check that the gitignore does not extist and that the destination path begins with the root path
@ -243,9 +237,7 @@ impl MDBook {
self.init()?;
// Clean output directory
if let Some(htmlconfig) = self.config.get_html_config() {
utils::fs::remove_dir_content(htmlconfig.get_destination())?;
}
utils::fs::remove_dir_content(self.config.get_html_config().get_destination())?;
self.renderer.render(self)
}
@ -258,44 +250,41 @@ impl MDBook {
pub fn copy_theme(&self) -> Result<()> {
debug!("[fn]: copy_theme");
if let Some(htmlconfig) = self.config.get_html_config() {
let themedir = htmlconfig.get_theme();
if !themedir.exists() {
debug!("[*]: {:?} does not exist, trying to create directory", themedir);
fs::create_dir(&themedir)?;
}
// index.hbs
let mut index = File::create(&themedir.join("index.hbs"))?;
index.write_all(theme::INDEX)?;
// book.css
let mut css = File::create(&themedir.join("book.css"))?;
css.write_all(theme::CSS)?;
// favicon.png
let mut favicon = File::create(&themedir.join("favicon.png"))?;
favicon.write_all(theme::FAVICON)?;
// book.js
let mut js = File::create(&themedir.join("book.js"))?;
js.write_all(theme::JS)?;
// highlight.css
let mut highlight_css = File::create(&themedir.join("highlight.css"))?;
highlight_css.write_all(theme::HIGHLIGHT_CSS)?;
// highlight.js
let mut highlight_js = File::create(&themedir.join("highlight.js"))?;
highlight_js.write_all(theme::HIGHLIGHT_JS)?;
let themedir = self.config.get_html_config().get_theme();
if !themedir.exists() {
debug!("[*]: {:?} does not exist, trying to create directory", themedir);
fs::create_dir(&themedir)?;
}
// index.hbs
let mut index = File::create(&themedir.join("index.hbs"))?;
index.write_all(theme::INDEX)?;
// book.css
let mut css = File::create(&themedir.join("book.css"))?;
css.write_all(theme::CSS)?;
// favicon.png
let mut favicon = File::create(&themedir.join("favicon.png"))?;
favicon.write_all(theme::FAVICON)?;
// book.js
let mut js = File::create(&themedir.join("book.js"))?;
js.write_all(theme::JS)?;
// highlight.css
let mut highlight_css = File::create(&themedir.join("highlight.css"))?;
highlight_css.write_all(theme::HIGHLIGHT_CSS)?;
// highlight.js
let mut highlight_js = File::create(&themedir.join("highlight.js"))?;
highlight_js.write_all(theme::HIGHLIGHT_JS)?;
Ok(())
}
pub fn write_file<P: AsRef<Path>>(&self, filename: P, content: &[u8]) -> Result<()> {
let path = self.get_destination().ok_or_else(|| String::from("HtmlConfig not set, could not find a destination"))?
let path = self.get_destination()
.join(filename);
utils::fs::create_file(&path)?
@ -392,22 +381,15 @@ impl MDBook {
pub fn with_destination<T: Into<PathBuf>>(mut self, destination: T) -> Self {
let root = self.config.get_root().to_owned();
if let Some(htmlconfig) = self.config.get_mut_html_config() {
htmlconfig.set_destination(&root, &destination.into());
} else {
error!("There is no HTML renderer set...");
}
self.config.get_mut_html_config()
.set_destination(&root, &destination.into());
self
}
pub fn get_destination(&self) -> Option<&Path> {
if let Some(htmlconfig) = self.config.get_html_config() {
return Some(htmlconfig.get_destination());
}
None
pub fn get_destination(&self) -> &Path {
self.config.get_html_config()
.get_destination()
}
pub fn with_source<T: Into<PathBuf>>(mut self, source: T) -> Self {
@ -453,94 +435,61 @@ impl MDBook {
pub fn with_theme_path<T: Into<PathBuf>>(mut self, theme_path: T) -> Self {
let root = self.config.get_root().to_owned();
if let Some(htmlconfig) = self.config.get_mut_html_config() {
htmlconfig.set_theme(&root, &theme_path.into());
} else {
error!("There is no HTML renderer set...");
}
self.config.get_mut_html_config()
.set_theme(&root, &theme_path.into());
self
}
pub fn get_theme_path(&self) -> Option<&Path> {
if let Some(htmlconfig) = self.config.get_html_config() {
return Some(htmlconfig.get_theme());
}
None
pub fn get_theme_path(&self) -> &Path {
self.config.get_html_config()
.get_theme()
}
pub fn with_curly_quotes(mut self, curly_quotes: bool) -> Self {
if let Some(htmlconfig) = self.config.get_mut_html_config() {
htmlconfig.set_curly_quotes(curly_quotes);
} else {
error!("There is no HTML renderer set...");
}
self.config.get_mut_html_config()
.set_curly_quotes(curly_quotes);
self
}
pub fn get_curly_quotes(&self) -> bool {
if let Some(htmlconfig) = self.config.get_html_config() {
return htmlconfig.get_curly_quotes();
}
false
self.config.get_html_config()
.get_curly_quotes()
}
pub fn with_mathjax_support(mut self, mathjax_support: bool) -> Self {
if let Some(htmlconfig) = self.config.get_mut_html_config() {
htmlconfig.set_mathjax_support(mathjax_support);
} else {
error!("There is no HTML renderer set...");
}
self.config.get_mut_html_config()
.set_mathjax_support(mathjax_support);
self
}
pub fn get_mathjax_support(&self) -> bool {
if let Some(htmlconfig) = self.config.get_html_config() {
return htmlconfig.get_mathjax_support();
}
false
self.config.get_html_config()
.get_mathjax_support()
}
pub fn get_google_analytics_id(&self) -> Option<String> {
if let Some(htmlconfig) = self.config.get_html_config() {
return htmlconfig.get_google_analytics_id();
}
None
self.config.get_html_config()
.get_google_analytics_id()
}
pub fn has_additional_js(&self) -> bool {
if let Some(htmlconfig) = self.config.get_html_config() {
return htmlconfig.has_additional_js();
}
false
self.config.get_html_config()
.has_additional_js()
}
pub fn get_additional_js(&self) -> &[PathBuf] {
if let Some(htmlconfig) = self.config.get_html_config() {
return htmlconfig.get_additional_js();
}
&[]
self.config.get_html_config()
.get_additional_js()
}
pub fn has_additional_css(&self) -> bool {
if let Some(htmlconfig) = self.config.get_html_config() {
return htmlconfig.has_additional_css();
}
false
self.config.get_html_config()
.has_additional_css()
}
pub fn get_additional_css(&self) -> &[PathBuf] {
if let Some(htmlconfig) = self.config.get_html_config() {
return htmlconfig.get_additional_css();
}
&[]
self.config.get_html_config()
.get_additional_css()
}
// Construct book

View File

@ -5,7 +5,7 @@ use super::tomlconfig::TomlConfig;
use super::jsonconfig::JsonConfig;
/// Configuration struct containing all the configuration options available in mdBook.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BookConfig {
root: PathBuf,
source: PathBuf,
@ -17,7 +17,7 @@ pub struct BookConfig {
multilingual: bool,
indent_spaces: i32,
html_config: Option<HtmlConfig>,
html_config: HtmlConfig,
}
impl BookConfig {
@ -33,7 +33,7 @@ impl BookConfig {
///
/// assert_eq!(config.get_root(), &root);
/// assert_eq!(config.get_source(), PathBuf::from("directory/to/my/book/src"));
/// assert_eq!(config.get_html_config(), Some(&HtmlConfig::new(PathBuf::from("directory/to/my/book"))));
/// assert_eq!(config.get_html_config(), &HtmlConfig::new(PathBuf::from("directory/to/my/book")));
/// ```
pub fn new<T: Into<PathBuf>>(root: T) -> Self {
let root: PathBuf = root.into();
@ -50,7 +50,7 @@ impl BookConfig {
multilingual: false,
indent_spaces: 4,
html_config: Some(htmlconfig),
html_config: htmlconfig,
}
}
@ -109,9 +109,8 @@ impl BookConfig {
if let Some(tomlhtmlconfig) = tomlconfig.output.and_then(|o| o.html) {
let root = self.root.clone();
if let Some(htmlconfig) = self.get_mut_html_config() {
htmlconfig.fill_from_tomlconfig(root, tomlhtmlconfig);
}
self.get_mut_html_config()
.fill_from_tomlconfig(root, tomlhtmlconfig);
}
self
@ -148,16 +147,14 @@ impl BookConfig {
if let Some(d) = jsonconfig.dest {
let root = self.get_root().to_owned();
if let Some(htmlconfig) = self.get_mut_html_config() {
htmlconfig.set_destination(&root, &d);
}
self.get_mut_html_config()
.set_destination(&root, &d);
}
if let Some(d) = jsonconfig.theme_path {
let root = self.get_root().to_owned();
if let Some(htmlconfig) = self.get_mut_html_config() {
htmlconfig.set_theme(&root, &d);
}
self.get_mut_html_config()
.set_theme(&root, &d);
}
self
@ -217,16 +214,16 @@ impl BookConfig {
}
pub fn set_html_config(&mut self, htmlconfig: HtmlConfig) -> &mut Self {
self.html_config = Some(htmlconfig);
self.html_config = htmlconfig;
self
}
/// Returns the configuration for the HTML renderer or None of there isn't any
pub fn get_html_config(&self) -> Option<&HtmlConfig> {
self.html_config.as_ref()
pub fn get_html_config(&self) -> &HtmlConfig {
&self.html_config
}
pub fn get_mut_html_config(&mut self) -> Option<&mut HtmlConfig> {
self.html_config.as_mut()
pub fn get_mut_html_config(&mut self) -> &mut HtmlConfig {
&mut self.html_config
}
}

View File

@ -110,10 +110,6 @@ impl HtmlHandlebars {
info!(
"[*] Creating index.html from {:?} ✓",
book.get_destination()
.expect(
"If the HTML renderer is called, one would assume the HtmlConfig is \
set... (4)",
)
.join(&ch.path.with_extension("html"))
);
@ -252,9 +248,7 @@ impl Renderer for HtmlHandlebars {
// Print version
let mut print_content = String::new();
let destination = book.get_destination().expect(
"If the HTML renderer is called, one would assume the HtmlConfig is set... (2)",
);
let destination = book.get_destination();
debug!("[*]: Check if destination directory exists");
if fs::create_dir_all(&destination).is_err() {

View File

@ -46,7 +46,7 @@ pub struct Theme {
}
impl Theme {
pub fn new(src: Option<&Path>) -> Self {
pub fn new(src: &Path) -> Self {
// Default theme
let mut theme = Theme {
@ -64,12 +64,10 @@ impl Theme {
};
// Check if the given path exists
if src.is_none() || !src.unwrap().exists() || !src.unwrap().is_dir() {
if !src.exists() || !src.is_dir() {
return theme;
}
let src = src.unwrap();
// Check for individual files if they exist
// index.hbs

View File

@ -20,14 +20,14 @@ fn do_not_overwrite_unspecified_config_values() {
assert_eq!(book.get_root(), dir.path());
assert_eq!(book.get_source(), dir.path().join("bar"));
assert_eq!(book.get_destination().unwrap(), dir.path().join("baz"));
assert_eq!(book.get_destination(), dir.path().join("baz"));
// Test when trying to read a config file that does not exist
let book = book.read_config().expect("Error reading the config file");
assert_eq!(book.get_root(), dir.path());
assert_eq!(book.get_source(), dir.path().join("bar"));
assert_eq!(book.get_destination().unwrap(), dir.path().join("baz"));
assert_eq!(book.get_destination(), dir.path().join("baz"));
assert_eq!(book.get_mathjax_support(), true);
// Try with a partial config file
@ -40,7 +40,7 @@ fn do_not_overwrite_unspecified_config_values() {
assert_eq!(book.get_root(), dir.path());
assert_eq!(book.get_source(), dir.path().join("barbaz"));
assert_eq!(book.get_destination().unwrap(), dir.path().join("baz"));
assert_eq!(book.get_destination(), dir.path().join("baz"));
assert_eq!(book.get_mathjax_support(), true);
}

View File

@ -66,7 +66,7 @@ fn from_json_destination() {
let parsed = JsonConfig::from_json(json).expect("This should parse");
let config = BookConfig::from_jsonconfig("root", parsed);
let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig");
let htmlconfig = config.get_html_config();
assert_eq!(htmlconfig.get_destination(), PathBuf::from("root/htmlbook"));
}
@ -81,7 +81,7 @@ fn from_json_output_html_theme() {
let parsed = JsonConfig::from_json(json).expect("This should parse");
let config = BookConfig::from_jsonconfig("root", parsed);
let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig");
let htmlconfig = config.get_html_config();
assert_eq!(htmlconfig.get_theme(), &PathBuf::from("root/theme"));
}

View File

@ -68,7 +68,7 @@ fn from_toml_output_html_destination() {
let parsed = TomlConfig::from_toml(toml).expect("This should parse");
let config = BookConfig::from_tomlconfig("root", parsed);
let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig");
let htmlconfig = config.get_html_config();
assert_eq!(htmlconfig.get_destination(), PathBuf::from("root/htmlbook"));
}
@ -82,7 +82,7 @@ fn from_toml_output_html_theme() {
let parsed = TomlConfig::from_toml(toml).expect("This should parse");
let config = BookConfig::from_tomlconfig("root", parsed);
let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig");
let htmlconfig = config.get_html_config();
assert_eq!(htmlconfig.get_theme(), &PathBuf::from("root/theme"));
}
@ -96,7 +96,7 @@ fn from_toml_output_html_curly_quotes() {
let parsed = TomlConfig::from_toml(toml).expect("This should parse");
let config = BookConfig::from_tomlconfig("root", parsed);
let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig");
let htmlconfig = config.get_html_config();
assert_eq!(htmlconfig.get_curly_quotes(), true);
}
@ -110,7 +110,7 @@ fn from_toml_output_html_mathjax_support() {
let parsed = TomlConfig::from_toml(toml).expect("This should parse");
let config = BookConfig::from_tomlconfig("root", parsed);
let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig");
let htmlconfig = config.get_html_config();
assert_eq!(htmlconfig.get_mathjax_support(), true);
}
@ -124,7 +124,7 @@ fn from_toml_output_html_google_analytics() {
let parsed = TomlConfig::from_toml(toml).expect("This should parse");
let config = BookConfig::from_tomlconfig("root", parsed);
let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig");
let htmlconfig = config.get_html_config();
assert_eq!(htmlconfig.get_google_analytics_id().expect("the google-analytics key was provided"), String::from("123456"));
}
@ -138,7 +138,7 @@ fn from_toml_output_html_additional_stylesheet() {
let parsed = TomlConfig::from_toml(toml).expect("This should parse");
let config = BookConfig::from_tomlconfig("root", parsed);
let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig");
let htmlconfig = config.get_html_config();
assert_eq!(htmlconfig.get_additional_css(), &[PathBuf::from("root/custom.css"), PathBuf::from("root/two/custom.css")]);
}
@ -152,7 +152,7 @@ fn from_toml_output_html_additional_scripts() {
let parsed = TomlConfig::from_toml(toml).expect("This should parse");
let config = BookConfig::from_tomlconfig("root", parsed);
let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig");
let htmlconfig = config.get_html_config();
assert_eq!(htmlconfig.get_additional_js(), &[PathBuf::from("root/custom.js"), PathBuf::from("root/two/custom.js")]);
}