From 1743f2a39fb55a46654a66379b5ffdb57e651304 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Sat, 30 Sep 2017 21:56:22 +0800 Subject: [PATCH] Removed the now redundant config files --- src/{config/mod.rs => config.rs} | 12 -- src/config/bookconfig.rs | 226 ------------------------------- src/config/htmlconfig.rs | 177 ------------------------ src/config/jsonconfig.rs | 41 ------ src/config/playpenconfig.rs | 71 ---------- src/config/tomlconfig.rs | 60 -------- 6 files changed, 587 deletions(-) rename src/{config/mod.rs => config.rs} (95%) delete mode 100644 src/config/bookconfig.rs delete mode 100644 src/config/htmlconfig.rs delete mode 100644 src/config/jsonconfig.rs delete mode 100644 src/config/playpenconfig.rs delete mode 100644 src/config/tomlconfig.rs diff --git a/src/config/mod.rs b/src/config.rs similarity index 95% rename from src/config/mod.rs rename to src/config.rs index ceff068f..851f4591 100644 --- a/src/config/mod.rs +++ b/src/config.rs @@ -7,18 +7,6 @@ use serde::Deserialize; use errors::*; -// pub mod bookconfig; -// pub mod htmlconfig; -// pub mod playpenconfig; -// pub mod tomlconfig; -// pub mod jsonconfig; - -// Re-export the config structs -// pub use self::bookconfig::BookConfig; -// pub use self::htmlconfig::HtmlConfig; -// pub use self::playpenconfig::PlaypenConfig; -// pub use self::tomlconfig::TomlConfig; - /// The overall configuration object for MDBook. #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] diff --git a/src/config/bookconfig.rs b/src/config/bookconfig.rs deleted file mode 100644 index 05bbfbc6..00000000 --- a/src/config/bookconfig.rs +++ /dev/null @@ -1,226 +0,0 @@ -use std::path::{Path, PathBuf}; - -use super::HtmlConfig; -use super::tomlconfig::TomlConfig; -use super::jsonconfig::JsonConfig; - -/// Configuration struct containing all the configuration options available in mdBook. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct BookConfig { - root: PathBuf, - source: PathBuf, - - title: String, - authors: Vec, - description: String, - - multilingual: bool, - indent_spaces: i32, - - html_config: HtmlConfig, -} - -impl BookConfig { - /// Creates a new `BookConfig` struct with as root path the path given as parameter. - /// The source directory is `root/src` and the destination for the rendered book is `root/book`. - /// - /// ``` - /// # use std::path::PathBuf; - /// # use mdbook::config::{BookConfig, HtmlConfig}; - /// # - /// let root = PathBuf::from("directory/to/my/book"); - /// let config = BookConfig::new(&root); - /// - /// assert_eq!(config.get_root(), &root); - /// assert_eq!(config.get_source(), PathBuf::from("directory/to/my/book/src")); - /// assert_eq!(config.get_html_config(), - /// &HtmlConfig::new(PathBuf::from("directory/to/my/book"))); - /// ``` - pub fn new>(root: T) -> Self { - let root: PathBuf = root.into(); - let htmlconfig = HtmlConfig::new(&root); - - BookConfig { - root: root.clone(), - source: root.join("src"), - - title: String::new(), - authors: Vec::new(), - description: String::new(), - - multilingual: false, - indent_spaces: 4, - - html_config: htmlconfig, - } - } - - /// Builder method to set the source directory - pub fn with_source>(mut self, source: T) -> Self { - self.source = source.into(); - self - } - - /// Builder method to set the book's title - pub fn with_title>(mut self, title: T) -> Self { - self.title = title.into(); - self - } - - /// Builder method to set the book's description - pub fn with_description>(mut self, description: T) -> Self { - self.description = description.into(); - self - } - - /// Builder method to set the book's authors - pub fn with_authors>>(mut self, authors: T) -> Self { - self.authors = authors.into(); - self - } - - pub fn from_tomlconfig>(root: T, tomlconfig: TomlConfig) -> Self { - let root = root.into(); - let mut config = BookConfig::new(&root); - config.fill_from_tomlconfig(tomlconfig); - config - } - - pub fn fill_from_tomlconfig(&mut self, tomlconfig: TomlConfig) -> &mut Self { - if let Some(s) = tomlconfig.source { - self.set_source(s); - } - - if let Some(t) = tomlconfig.title { - self.set_title(t); - } - - if let Some(d) = tomlconfig.description { - self.set_description(d); - } - - if let Some(a) = tomlconfig.authors { - self.set_authors(a); - } - - if let Some(a) = tomlconfig.author { - self.set_authors(vec![a]); - } - - if let Some(tomlhtmlconfig) = tomlconfig.output.and_then(|o| o.html) { - let root = self.root.clone(); - self.get_mut_html_config() - .fill_from_tomlconfig(root, tomlhtmlconfig); - } - - self - } - - /// The JSON configuration file is **deprecated** and should not be used anymore. - /// Please, migrate to the TOML configuration file. - pub fn from_jsonconfig>(root: T, jsonconfig: JsonConfig) -> Self { - let root = root.into(); - let mut config = BookConfig::new(&root); - config.fill_from_jsonconfig(jsonconfig); - config - } - - /// The JSON configuration file is **deprecated** and should not be used anymore. - /// Please, migrate to the TOML configuration file. - pub fn fill_from_jsonconfig(&mut self, jsonconfig: JsonConfig) -> &mut Self { - if let Some(s) = jsonconfig.src { - self.set_source(s); - } - - if let Some(t) = jsonconfig.title { - self.set_title(t); - } - - if let Some(d) = jsonconfig.description { - self.set_description(d); - } - - if let Some(a) = jsonconfig.author { - self.set_authors(vec![a]); - } - - if let Some(d) = jsonconfig.dest { - let root = self.get_root().to_owned(); - self.get_mut_html_config().set_destination(&root, &d); - } - - if let Some(d) = jsonconfig.theme_path { - let root = self.get_root().to_owned(); - self.get_mut_html_config().set_theme(&root, &d); - } - - self - } - - pub fn set_root>(&mut self, root: T) -> &mut Self { - self.root = root.into(); - self - } - - pub fn get_root(&self) -> &Path { - &self.root - } - - pub fn set_source>(&mut self, source: T) -> &mut Self { - let mut source = source.into(); - - // If the source path is relative, start with the root path - if source.is_relative() { - source = self.root.join(source); - } - - self.source = source; - self - } - - pub fn get_source(&self) -> &Path { - &self.source - } - - pub fn set_title>(&mut self, title: T) -> &mut Self { - self.title = title.into(); - self - } - - pub fn get_title(&self) -> &str { - &self.title - } - - pub fn set_description>(&mut self, description: T) -> &mut Self { - self.description = description.into(); - self - } - - pub fn get_description(&self) -> &str { - &self.description - } - - pub fn set_authors>>(&mut self, authors: T) -> &mut Self { - self.authors = authors.into(); - self - } - - /// Returns the authors of the book as specified in the configuration file - pub fn get_authors(&self) -> &[String] { - self.authors.as_slice() - } - - pub fn set_html_config(&mut self, htmlconfig: HtmlConfig) -> &mut Self { - 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) -> &HtmlConfig { - &self.html_config - } - - pub fn get_mut_html_config(&mut self) -> &mut HtmlConfig { - &mut self.html_config - } -} diff --git a/src/config/htmlconfig.rs b/src/config/htmlconfig.rs deleted file mode 100644 index f2f9c42b..00000000 --- a/src/config/htmlconfig.rs +++ /dev/null @@ -1,177 +0,0 @@ -use std::path::{Path, PathBuf}; - -use super::tomlconfig::TomlHtmlConfig; -use super::playpenconfig::PlaypenConfig; - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct HtmlConfig { - destination: PathBuf, - theme: PathBuf, - curly_quotes: bool, - mathjax_support: bool, - google_analytics: Option, - additional_css: Vec, - additional_js: Vec, - playpen: PlaypenConfig, -} - -impl HtmlConfig { - /// Creates a new `HtmlConfig` struct containing - /// the configuration parameters for the HTML renderer. - /// - /// ``` - /// # use std::path::PathBuf; - /// # use mdbook::config::HtmlConfig; - /// # - /// let output = PathBuf::from("root/book"); - /// let config = HtmlConfig::new(PathBuf::from("root")); - /// - /// assert_eq!(config.get_destination(), &output); - /// ``` - pub fn new>(root: T) -> Self { - let root = root.into(); - let theme = root.join("theme"); - HtmlConfig { - destination: root.clone().join("book"), - theme: theme.clone(), - curly_quotes: false, - mathjax_support: false, - google_analytics: None, - additional_css: Vec::new(), - additional_js: Vec::new(), - playpen: PlaypenConfig::new(theme), - } - } - - pub fn fill_from_tomlconfig>(&mut self, - root: T, - tomlconfig: TomlHtmlConfig) - -> &mut Self { - let root = root.into(); - - if let Some(d) = tomlconfig.destination { - self.set_destination(&root, &d); - } - - if let Some(t) = tomlconfig.theme { - self.set_theme(&root, &t); - } - - if let Some(curly_quotes) = tomlconfig.curly_quotes { - self.curly_quotes = curly_quotes; - } - - if let Some(mathjax_support) = tomlconfig.mathjax_support { - self.mathjax_support = mathjax_support; - } - - if tomlconfig.google_analytics.is_some() { - self.google_analytics = tomlconfig.google_analytics; - } - - if let Some(stylepaths) = tomlconfig.additional_css { - for path in stylepaths { - if path.is_relative() { - self.additional_css.push(root.join(path)); - } else { - self.additional_css.push(path); - } - } - } - - if let Some(scriptpaths) = tomlconfig.additional_js { - for path in scriptpaths { - if path.is_relative() { - self.additional_js.push(root.join(path)); - } else { - self.additional_js.push(path); - } - } - } - - if let Some(playpen) = tomlconfig.playpen { - self.playpen.fill_from_tomlconfig(&self.theme, playpen); - } - - self - } - - pub fn set_destination>(&mut self, root: T, destination: T) -> &mut Self { - let d = destination.into(); - if d.is_relative() { - self.destination = root.into().join(d); - } else { - self.destination = d; - } - - self - } - - pub fn get_destination(&self) -> &Path { - &self.destination - } - - pub fn get_theme(&self) -> &Path { - &self.theme - } - - pub fn set_theme>(&mut self, root: T, theme: T) -> &mut Self { - let d = theme.into(); - if d.is_relative() { - self.theme = root.into().join(d); - } else { - self.theme = d; - } - - self - } - - pub fn get_curly_quotes(&self) -> bool { - self.curly_quotes - } - - pub fn set_curly_quotes(&mut self, curly_quotes: bool) { - self.curly_quotes = curly_quotes; - } - - pub fn get_mathjax_support(&self) -> bool { - self.mathjax_support - } - - pub fn set_mathjax_support(&mut self, mathjax_support: bool) { - self.mathjax_support = mathjax_support; - } - - pub fn get_google_analytics_id(&self) -> Option { - self.google_analytics.clone() - } - - pub fn set_google_analytics_id(&mut self, id: Option) -> &mut Self { - self.google_analytics = id; - self - } - - pub fn has_additional_css(&self) -> bool { - !self.additional_css.is_empty() - } - - pub fn get_additional_css(&self) -> &[PathBuf] { - &self.additional_css - } - - pub fn has_additional_js(&self) -> bool { - !self.additional_js.is_empty() - } - - pub fn get_additional_js(&self) -> &[PathBuf] { - &self.additional_js - } - - pub fn get_playpen_config(&self) -> &PlaypenConfig { - &self.playpen - } - - pub fn get_mut_playpen_config(&mut self) -> &mut PlaypenConfig { - &mut self.playpen - } -} diff --git a/src/config/jsonconfig.rs b/src/config/jsonconfig.rs deleted file mode 100644 index 5822bc19..00000000 --- a/src/config/jsonconfig.rs +++ /dev/null @@ -1,41 +0,0 @@ -extern crate serde_json; -use std::path::PathBuf; -use errors::*; - -/// The JSON configuration is **deprecated** and will be removed in the near future. -/// Please migrate to the TOML configuration. -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct JsonConfig { - pub src: Option, - pub dest: Option, - - pub title: Option, - pub author: Option, - pub description: Option, - - pub theme_path: Option, - pub google_analytics: Option, -} - - -/// Returns a `JsonConfig` from a JSON string -/// -/// ``` -/// # use mdbook::config::jsonconfig::JsonConfig; -/// # use std::path::PathBuf; -/// let json = r#"{ -/// "title": "Some title", -/// "dest": "htmlbook" -/// }"#; -/// -/// let config = JsonConfig::from_json(&json).expect("Should parse correctly"); -/// assert_eq!(config.title, Some(String::from("Some title"))); -/// assert_eq!(config.dest, Some(PathBuf::from("htmlbook"))); -/// ``` -impl JsonConfig { - pub fn from_json(input: &str) -> Result { - let config: JsonConfig = serde_json::from_str(input).chain_err(|| "Could not parse JSON")?; - - Ok(config) - } -} diff --git a/src/config/playpenconfig.rs b/src/config/playpenconfig.rs deleted file mode 100644 index db2ee5c2..00000000 --- a/src/config/playpenconfig.rs +++ /dev/null @@ -1,71 +0,0 @@ -use std::path::{Path, PathBuf}; - -use super::tomlconfig::TomlPlaypenConfig; - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct PlaypenConfig { - editor: PathBuf, - editable: bool, -} - -impl PlaypenConfig { - /// Creates a new `PlaypenConfig` for playpen configuration. - /// - /// ``` - /// # use std::path::PathBuf; - /// # use mdbook::config::PlaypenConfig; - /// # - /// let editor = PathBuf::from("root/editor"); - /// let config = PlaypenConfig::new(PathBuf::from("root")); - /// - /// assert_eq!(config.get_editor(), &editor); - /// assert_eq!(config.is_editable(), false); - /// ``` - pub fn new>(root: T) -> Self { - PlaypenConfig { - editor: root.into().join("editor"), - editable: false, - } - } - - pub fn fill_from_tomlconfig>(&mut self, - root: T, - tomlplaypenconfig: TomlPlaypenConfig) - -> &mut Self { - let root = root.into(); - - if let Some(editor) = tomlplaypenconfig.editor { - if editor.is_relative() { - self.editor = root.join(editor); - } else { - self.editor = editor; - } - } - - if let Some(editable) = tomlplaypenconfig.editable { - self.editable = editable; - } - - self - } - - pub fn is_editable(&self) -> bool { - self.editable - } - - pub fn get_editor(&self) -> &Path { - &self.editor - } - - pub fn set_editor>(&mut self, root: T, editor: T) -> &mut Self { - let editor = editor.into(); - - if editor.is_relative() { - self.editor = root.into().join(editor); - } else { - self.editor = editor; - } - - self - } -} diff --git a/src/config/tomlconfig.rs b/src/config/tomlconfig.rs deleted file mode 100644 index 4bb11e30..00000000 --- a/src/config/tomlconfig.rs +++ /dev/null @@ -1,60 +0,0 @@ -extern crate toml; -use std::path::PathBuf; -use errors::*; - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct TomlConfig { - pub source: Option, - - pub title: Option, - pub author: Option, - pub authors: Option>, - pub description: Option, - - pub output: Option, -} - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct TomlOutputConfig { - pub html: Option, -} - -#[serde(rename_all = "kebab-case")] -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct TomlHtmlConfig { - pub destination: Option, - pub theme: Option, - pub google_analytics: Option, - pub curly_quotes: Option, - pub mathjax_support: Option, - pub additional_css: Option>, - pub additional_js: Option>, - pub playpen: Option, -} - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct TomlPlaypenConfig { - pub editor: Option, - pub editable: Option, -} - -/// Returns a `TomlConfig` from a TOML string -/// -/// ``` -/// # use mdbook::config::tomlconfig::TomlConfig; -/// # use std::path::PathBuf; -/// let toml = r#"title="Some title" -/// [output.html] -/// destination = "htmlbook" "#; -/// -/// let config = TomlConfig::from_toml(&toml).expect("Should parse correctly"); -/// assert_eq!(config.title, Some(String::from("Some title"))); -/// assert_eq!(config.output.unwrap().html.unwrap().destination, Some(PathBuf::from("htmlbook"))); -/// ``` -impl TomlConfig { - pub fn from_toml(input: &str) -> Result { - let config: TomlConfig = toml::from_str(input).chain_err(|| "Could not parse TOML")?; - - Ok(config) - } -}