2015-07-18 06:04:20 +08:00
|
|
|
pub mod bookitem;
|
2016-12-23 16:15:32 +08:00
|
|
|
|
2015-07-19 06:08:38 +08:00
|
|
|
pub use self::bookitem::{BookItem, BookItems};
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::error::Error;
|
|
|
|
use std::io;
|
2017-05-19 05:52:38 +08:00
|
|
|
use std::io::{Read, Write};
|
2016-04-27 05:04:27 +08:00
|
|
|
use std::io::ErrorKind;
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
use {theme, parse, utils};
|
|
|
|
use renderer::{Renderer, HtmlHandlebars};
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
use config::{BookConfig, HtmlConfig};
|
|
|
|
use config::tomlconfig::TomlConfig;
|
2017-05-19 06:56:37 +08:00
|
|
|
use config::jsonconfig::JsonConfig;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
pub struct MDBook {
|
|
|
|
config: BookConfig,
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
pub content: Vec<BookItem>,
|
|
|
|
renderer: Box<Renderer>,
|
|
|
|
|
|
|
|
livereload: Option<String>,
|
2017-04-18 09:53:27 +08:00
|
|
|
|
|
|
|
/// Should `mdbook build` create files referenced from SUMMARY.md if they
|
|
|
|
/// don't exist
|
|
|
|
pub create_missing: bool,
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MDBook {
|
|
|
|
/// Create a new `MDBook` struct with root directory `root`
|
|
|
|
///
|
2017-03-30 20:09:14 +08:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # extern crate mdbook;
|
|
|
|
/// # use mdbook::MDBook;
|
|
|
|
/// # use std::path::Path;
|
|
|
|
/// # fn main() {
|
2017-05-19 06:56:37 +08:00
|
|
|
/// let book = MDBook::new(Path::new("root_dir"));
|
2017-03-30 20:09:14 +08:00
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
2017-05-19 19:04:37 +08:00
|
|
|
/// In this example, `root_dir` will be the root directory of our book
|
|
|
|
/// and is specified in function of the current working directory
|
|
|
|
/// by using a relative path instead of an
|
|
|
|
/// absolute path.
|
2017-03-30 20:09:14 +08:00
|
|
|
///
|
2016-12-07 22:22:32 +08:00
|
|
|
/// Default directory paths:
|
|
|
|
///
|
|
|
|
/// - source: `root/src`
|
|
|
|
/// - output: `root/book`
|
|
|
|
/// - theme: `root/theme`
|
2016-04-27 05:04:27 +08:00
|
|
|
///
|
2017-05-19 19:04:37 +08:00
|
|
|
/// They can both be changed by using [`set_src()`](#method.set_src) and
|
|
|
|
/// [`set_dest()`](#method.set_dest)
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
pub fn new(root: &Path) -> MDBook {
|
|
|
|
|
|
|
|
if !root.exists() || !root.is_dir() {
|
2016-08-14 21:40:08 +08:00
|
|
|
warn!("{:?} No directory with that name", root);
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MDBook {
|
2017-05-19 05:52:38 +08:00
|
|
|
config: BookConfig::new(root),
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
content: vec![],
|
|
|
|
renderer: Box::new(HtmlHandlebars::new()),
|
2016-05-09 03:51:34 +08:00
|
|
|
|
2016-04-27 05:04:27 +08:00
|
|
|
livereload: None,
|
2017-04-18 09:53:27 +08:00
|
|
|
create_missing: true,
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
/// Returns a flat depth-first iterator over the elements of the book,
|
|
|
|
/// it returns an [BookItem enum](bookitem.html):
|
2016-04-27 05:04:27 +08:00
|
|
|
/// `(section: String, bookitem: &BookItem)`
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # extern crate mdbook;
|
|
|
|
/// # use mdbook::MDBook;
|
|
|
|
/// # use mdbook::BookItem;
|
|
|
|
/// # use std::path::Path;
|
|
|
|
/// # fn main() {
|
|
|
|
/// # let mut book = MDBook::new(Path::new("mybook"));
|
|
|
|
/// for item in book.iter() {
|
|
|
|
/// match item {
|
|
|
|
/// &BookItem::Chapter(ref section, ref chapter) => {},
|
|
|
|
/// &BookItem::Affix(ref chapter) => {},
|
|
|
|
/// &BookItem::Spacer => {},
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // would print something like this:
|
|
|
|
/// // 1. Chapter 1
|
|
|
|
/// // 1.1 Sub Chapter
|
|
|
|
/// // 1.2 Sub Chapter
|
|
|
|
/// // 2. Chapter 2
|
|
|
|
/// //
|
|
|
|
/// // etc.
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
pub fn iter(&self) -> BookItems {
|
|
|
|
BookItems {
|
|
|
|
items: &self.content[..],
|
|
|
|
current_index: 0,
|
|
|
|
stack: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
/// `init()` creates some boilerplate files and directories
|
|
|
|
/// to get you started with your book.
|
2016-04-27 05:04:27 +08:00
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// book-test/
|
|
|
|
/// ├── book
|
|
|
|
/// └── src
|
|
|
|
/// ├── chapter_1.md
|
|
|
|
/// └── SUMMARY.md
|
|
|
|
/// ```
|
|
|
|
///
|
2017-05-19 19:04:37 +08:00
|
|
|
/// It uses the paths given as source and output directories
|
|
|
|
/// and adds a `SUMMARY.md` and a
|
2016-04-27 05:04:27 +08:00
|
|
|
/// `chapter_1.md` to the source directory.
|
|
|
|
|
|
|
|
pub fn init(&mut self) -> Result<(), Box<Error>> {
|
|
|
|
|
|
|
|
debug!("[fn]: init");
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
if !self.config.get_root().exists() {
|
|
|
|
fs::create_dir_all(&self.config.get_root()).unwrap();
|
|
|
|
info!("{:?} created", &self.config.get_root());
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
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())?;
|
|
|
|
}
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
2017-06-05 01:48:41 +08:00
|
|
|
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
if !self.config.get_source().exists() {
|
|
|
|
debug!("[*]: {:?} does not exist, trying to create directory", self.config.get_source());
|
|
|
|
fs::create_dir_all(self.config.get_source())?;
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
let summary = self.config.get_source().join("SUMMARY.md");
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
if !summary.exists() {
|
|
|
|
|
|
|
|
// Summary does not exist, create it
|
2017-05-19 05:52:38 +08:00
|
|
|
debug!("[*]: {:?} does not exist, trying to create SUMMARY.md", &summary);
|
|
|
|
let mut f = File::create(&summary)?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
debug!("[*]: Writing to SUMMARY.md");
|
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
writeln!(f, "# Summary")?;
|
|
|
|
writeln!(f, "")?;
|
|
|
|
writeln!(f, "- [Chapter 1](./chapter_1.md)")?;
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse SUMMARY.md, and create the missing item related file
|
2017-05-19 19:04:37 +08:00
|
|
|
self.parse_summary()?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
debug!("[*]: constructing paths for missing files");
|
|
|
|
for item in self.iter() {
|
|
|
|
debug!("[*]: item: {:?}", item);
|
2017-04-18 09:55:32 +08:00
|
|
|
let ch = match *item {
|
2016-04-27 05:04:27 +08:00
|
|
|
BookItem::Spacer => continue,
|
|
|
|
BookItem::Chapter(_, ref ch) |
|
2017-04-18 09:55:32 +08:00
|
|
|
BookItem::Affix(ref ch) => ch,
|
|
|
|
};
|
2017-04-27 21:16:19 +08:00
|
|
|
if !ch.path.as_os_str().is_empty() {
|
2017-05-19 05:52:38 +08:00
|
|
|
let path = self.config.get_source().join(&ch.path);
|
2017-04-18 09:55:32 +08:00
|
|
|
|
|
|
|
if !path.exists() {
|
|
|
|
if !self.create_missing {
|
2017-05-19 19:04:37 +08:00
|
|
|
return Err(format!("'{}' referenced from SUMMARY.md does not exist.", path.to_string_lossy())
|
|
|
|
.into());
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
2017-04-18 09:55:32 +08:00
|
|
|
debug!("[*]: {:?} does not exist, trying to create file", path);
|
2017-05-19 19:04:37 +08:00
|
|
|
::std::fs::create_dir_all(path.parent().unwrap())?;
|
|
|
|
let mut f = File::create(path)?;
|
2017-04-18 09:55:32 +08:00
|
|
|
|
|
|
|
// debug!("[*]: Writing to {:?}", path);
|
2017-05-19 19:04:37 +08:00
|
|
|
writeln!(f, "# {}", ch.name)?;
|
2017-04-18 09:55:32 +08:00
|
|
|
}
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("[*]: init done");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_gitignore(&self) {
|
|
|
|
let gitignore = self.get_gitignore();
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
// If the HTML renderer is not set, return
|
|
|
|
if self.config.get_html_config().is_none() { return; }
|
2017-06-05 01:48:41 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
let destination = self.config.get_html_config()
|
|
|
|
.expect("The HtmlConfig does exist, checked just before")
|
|
|
|
.get_destination();
|
2017-06-05 01:48:41 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
// Check that the gitignore does not extist and that the destination path begins with the root path
|
|
|
|
// We assume tha if it does begin with the root path it is contained within. This assumption
|
|
|
|
// will not hold true for paths containing double dots to go back up e.g. `root/../destination`
|
|
|
|
if !gitignore.exists() && destination.starts_with(self.config.get_root()) {
|
|
|
|
|
|
|
|
let relative = destination
|
|
|
|
.strip_prefix(self.config.get_root())
|
|
|
|
.expect("Could not strip the root prefix, path is not relative to root")
|
2017-05-19 19:04:37 +08:00
|
|
|
.to_str()
|
2017-05-19 05:52:38 +08:00
|
|
|
.expect("Could not convert to &str");
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
debug!("[*]: {:?} does not exist, trying to create .gitignore", gitignore);
|
|
|
|
|
|
|
|
let mut f = File::create(&gitignore).expect("Could not create file.");
|
|
|
|
|
|
|
|
debug!("[*]: Writing to .gitignore");
|
|
|
|
|
|
|
|
writeln!(f, "{}", relative).expect("Could not write to file.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
/// The `build()` method is the one where everything happens.
|
|
|
|
/// First it parses `SUMMARY.md` to construct the book's structure
|
|
|
|
/// in the form of a `Vec<BookItem>` and then calls `render()`
|
2016-04-27 05:04:27 +08:00
|
|
|
/// method of the current renderer.
|
|
|
|
///
|
|
|
|
/// It is the renderer who generates all the output files.
|
|
|
|
pub fn build(&mut self) -> Result<(), Box<Error>> {
|
|
|
|
debug!("[fn]: build");
|
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
self.init()?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
// Clean output directory
|
2017-05-19 05:52:38 +08:00
|
|
|
if let Some(htmlconfig) = self.config.get_html_config() {
|
|
|
|
utils::fs::remove_dir_content(htmlconfig.get_destination())?;
|
|
|
|
}
|
2017-06-05 01:48:41 +08:00
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
self.renderer.render(&self)?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_gitignore(&self) -> PathBuf {
|
2017-05-19 05:52:38 +08:00
|
|
|
self.config.get_root().join(".gitignore")
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn copy_theme(&self) -> Result<(), Box<Error>> {
|
|
|
|
debug!("[fn]: copy_theme");
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
if let Some(themedir) = self.config.get_html_config().and_then(HtmlConfig::get_theme) {
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
if !themedir.exists() {
|
|
|
|
debug!("[*]: {:?} does not exist, trying to create directory", themedir);
|
|
|
|
fs::create_dir(&themedir)?;
|
|
|
|
}
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
// index.hbs
|
|
|
|
let mut index = File::create(&themedir.join("index.hbs"))?;
|
|
|
|
index.write_all(theme::INDEX)?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
// book.css
|
|
|
|
let mut css = File::create(&themedir.join("book.css"))?;
|
|
|
|
css.write_all(theme::CSS)?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
// favicon.png
|
|
|
|
let mut favicon = File::create(&themedir.join("favicon.png"))?;
|
|
|
|
favicon.write_all(theme::FAVICON)?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
// book.js
|
|
|
|
let mut js = File::create(&themedir.join("book.js"))?;
|
|
|
|
js.write_all(theme::JS)?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
// highlight.css
|
|
|
|
let mut highlight_css = File::create(&themedir.join("highlight.css"))?;
|
|
|
|
highlight_css.write_all(theme::HIGHLIGHT_CSS)?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
// highlight.js
|
|
|
|
let mut highlight_js = File::create(&themedir.join("highlight.js"))?;
|
|
|
|
highlight_js.write_all(theme::HIGHLIGHT_JS)?;
|
|
|
|
}
|
2017-06-05 01:48:41 +08:00
|
|
|
|
2016-04-27 05:04:27 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-01 14:27:38 +08:00
|
|
|
pub fn write_file<P: AsRef<Path>>(&self, filename: P, content: &[u8]) -> Result<(), Box<Error>> {
|
2017-05-19 05:52:38 +08:00
|
|
|
let path = self.get_destination()
|
|
|
|
.ok_or(String::from("HtmlConfig not set, could not find a destination"))?
|
|
|
|
.join(filename);
|
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
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)))?;
|
2017-05-19 05:52:38 +08:00
|
|
|
|
2017-01-01 14:27:38 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
/// Parses the `book.json` file (if it exists) to extract
|
|
|
|
/// the configuration parameters.
|
2016-04-27 05:04:27 +08:00
|
|
|
/// 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`
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
pub fn read_config(mut self) -> Result<Self, Box<Error>> {
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
let toml = self.get_root().join("book.toml");
|
|
|
|
let json = self.get_root().join("book.json");
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
if toml.exists() {
|
|
|
|
let mut file = File::open(toml)?;
|
|
|
|
let mut content = String::new();
|
|
|
|
file.read_to_string(&mut content)?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
let parsed_config = TomlConfig::from_toml(&content)?;
|
|
|
|
self.config.fill_from_tomlconfig(parsed_config);
|
|
|
|
} else if json.exists() {
|
2017-05-19 06:56:37 +08:00
|
|
|
warn!("The JSON configuration file is deprecated, please use the TOML configuration.");
|
|
|
|
let mut file = File::open(json)?;
|
|
|
|
let mut content = String::new();
|
|
|
|
file.read_to_string(&mut content)?;
|
|
|
|
|
|
|
|
let parsed_config = JsonConfig::from_json(&content)?;
|
|
|
|
self.config.fill_from_jsonconfig(parsed_config);
|
2017-05-19 05:52:38 +08:00
|
|
|
}
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
Ok(self)
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
/// You can change the default renderer to another one
|
|
|
|
/// by using this method. The only requirement
|
|
|
|
/// is for your renderer to implement the
|
|
|
|
/// [Renderer trait](../../renderer/renderer/trait.Renderer.html)
|
2016-04-27 05:04:27 +08:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// extern crate mdbook;
|
|
|
|
/// use mdbook::MDBook;
|
|
|
|
/// use mdbook::renderer::HtmlHandlebars;
|
|
|
|
/// # use std::path::Path;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let mut book = MDBook::new(Path::new("mybook"))
|
|
|
|
/// .set_renderer(Box::new(HtmlHandlebars::new()));
|
|
|
|
///
|
2017-05-19 19:04:37 +08:00
|
|
|
/// // In this example we replace the default renderer
|
|
|
|
/// // by the default renderer...
|
|
|
|
/// // Don't forget to put your renderer in a Box
|
2016-04-27 05:04:27 +08:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2017-05-19 19:04:37 +08:00
|
|
|
/// **note:** Don't forget to put your renderer in a `Box`
|
|
|
|
/// before passing it to `set_renderer()`
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
pub fn set_renderer(mut self, renderer: Box<Renderer>) -> Self {
|
|
|
|
self.renderer = renderer;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test(&mut self) -> Result<(), Box<Error>> {
|
|
|
|
// read in the chapters
|
2017-05-19 19:04:37 +08:00
|
|
|
self.parse_summary()?;
|
2016-04-27 05:04:27 +08:00
|
|
|
for item in self.iter() {
|
|
|
|
|
2017-02-16 11:01:26 +08:00
|
|
|
if let BookItem::Chapter(_, ref ch) = *item {
|
|
|
|
if ch.path != PathBuf::new() {
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
let path = self.get_source().join(&ch.path);
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-02-16 11:01:26 +08:00
|
|
|
println!("[*]: Testing file: {:?}", path);
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
let output_result = Command::new("rustdoc").arg(&path).arg("--test").output();
|
|
|
|
let output = output_result?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-02-16 11:01:26 +08:00
|
|
|
if !output.status.success() {
|
2017-05-19 19:04:37 +08:00
|
|
|
return Err(Box::new(io::Error::new(ErrorKind::Other,
|
|
|
|
format!("{}\n{}",
|
|
|
|
String::from_utf8_lossy(&output.stdout),
|
|
|
|
String::from_utf8_lossy(&output.stderr)))) as
|
|
|
|
Box<Error>);
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
2017-02-16 11:01:26 +08:00
|
|
|
}
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_root(&self) -> &Path {
|
2017-05-19 05:52:38 +08:00
|
|
|
self.config.get_root()
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-06-05 01:48:41 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
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());
|
2017-02-16 11:01:26 +08:00
|
|
|
} else {
|
2017-05-19 05:52:38 +08:00
|
|
|
error!("There is no HTML renderer set...");
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2017-06-05 01:48:41 +08:00
|
|
|
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
pub fn get_destination(&self) -> Option<&Path> {
|
|
|
|
if let Some(htmlconfig) = self.config.get_html_config() {
|
|
|
|
return Some(htmlconfig.get_destination());
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_source<T: Into<PathBuf>>(mut self, source: T) -> Self {
|
|
|
|
self.config.set_source(source);
|
2016-04-27 05:04:27 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
pub fn get_source(&self) -> &Path {
|
|
|
|
self.config.get_source()
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
pub fn with_title<T: Into<String>>(mut self, title: T) -> Self {
|
|
|
|
self.config.set_title(title);
|
2016-04-27 05:04:27 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_title(&self) -> &str {
|
2017-05-19 05:52:38 +08:00
|
|
|
self.config.get_title()
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
pub fn with_description<T: Into<String>>(mut self, description: T) -> Self {
|
|
|
|
self.config.set_description(description);
|
2016-04-27 05:04:27 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_description(&self) -> &str {
|
2017-05-19 05:52:38 +08:00
|
|
|
self.config.get_description()
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_livereload(&mut self, livereload: String) -> &mut Self {
|
|
|
|
self.livereload = Some(livereload);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unset_livereload(&mut self) -> &Self {
|
|
|
|
self.livereload = None;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_livereload(&self) -> Option<&String> {
|
2017-02-16 11:01:26 +08:00
|
|
|
self.livereload.as_ref()
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
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());
|
2017-02-16 11:01:26 +08:00
|
|
|
} else {
|
2017-05-19 05:52:38 +08:00
|
|
|
error!("There is no HTML renderer set...");
|
|
|
|
}
|
2016-12-07 22:22:32 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
pub fn get_theme_path(&self) -> Option<&PathBuf> {
|
|
|
|
if let Some(htmlconfig) = self.config.get_html_config() {
|
|
|
|
return htmlconfig.get_theme();
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2016-12-07 22:22:32 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 07:13:45 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-20 19:56:01 +08:00
|
|
|
pub fn has_additional_css(&self) -> bool {
|
|
|
|
if let Some(htmlconfig) = self.config.get_html_config() {
|
|
|
|
return htmlconfig.has_additional_css();
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_additional_css(&self) -> &[PathBuf] {
|
|
|
|
if let Some(htmlconfig) = self.config.get_html_config() {
|
|
|
|
return htmlconfig.get_additional_css();
|
|
|
|
}
|
|
|
|
|
|
|
|
&[]
|
|
|
|
}
|
|
|
|
|
2016-04-27 05:04:27 +08:00
|
|
|
// Construct book
|
|
|
|
fn parse_summary(&mut self) -> Result<(), Box<Error>> {
|
|
|
|
// When append becomes stable, use self.content.append() ...
|
2017-05-19 05:52:38 +08:00
|
|
|
self.content = parse::construct_bookitems(&self.get_source().join("SUMMARY.md"))?;
|
2016-04-27 05:04:27 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|