Merge pull request #314 from budziq/fix_theme

Fixes missing the default "theme" dir location
This commit is contained in:
Mathieu David 2017-06-24 14:03:10 +02:00 committed by GitHub
commit d50486e337
5 changed files with 19 additions and 26 deletions

View File

@ -13,7 +13,7 @@ use std::process::Command;
use {theme, parse, utils}; use {theme, parse, utils};
use renderer::{Renderer, HtmlHandlebars}; use renderer::{Renderer, HtmlHandlebars};
use config::{BookConfig, HtmlConfig}; use config::BookConfig;
use config::tomlconfig::TomlConfig; use config::tomlconfig::TomlConfig;
use config::jsonconfig::JsonConfig; use config::jsonconfig::JsonConfig;
@ -262,8 +262,9 @@ impl MDBook {
pub fn copy_theme(&self) -> Result<(), Box<Error>> { pub fn copy_theme(&self) -> Result<(), Box<Error>> {
debug!("[fn]: copy_theme"); debug!("[fn]: copy_theme");
if let Some(themedir) = self.config.get_html_config().and_then(HtmlConfig::get_theme) { if let Some(htmlconfig) = self.config.get_html_config() {
let themedir = htmlconfig.get_theme();
if !themedir.exists() { if !themedir.exists() {
debug!("[*]: {:?} does not exist, trying to create directory", themedir); debug!("[*]: {:?} does not exist, trying to create directory", themedir);
fs::create_dir(&themedir)?; fs::create_dir(&themedir)?;
@ -472,9 +473,9 @@ impl MDBook {
self self
} }
pub fn get_theme_path(&self) -> Option<&PathBuf> { pub fn get_theme_path(&self) -> Option<&Path> {
if let Some(htmlconfig) = self.config.get_html_config() { if let Some(htmlconfig) = self.config.get_html_config() {
return htmlconfig.get_theme(); return Some(htmlconfig.get_theme());
} }
None None

View File

@ -5,7 +5,7 @@ use super::tomlconfig::TomlHtmlConfig;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct HtmlConfig { pub struct HtmlConfig {
destination: PathBuf, destination: PathBuf,
theme: Option<PathBuf>, theme: PathBuf,
curly_quotes: bool, curly_quotes: bool,
google_analytics: Option<String>, google_analytics: Option<String>,
additional_css: Vec<PathBuf>, additional_css: Vec<PathBuf>,
@ -25,9 +25,10 @@ impl HtmlConfig {
/// assert_eq!(config.get_destination(), &output); /// assert_eq!(config.get_destination(), &output);
/// ``` /// ```
pub fn new<T: Into<PathBuf>>(root: T) -> Self { pub fn new<T: Into<PathBuf>>(root: T) -> Self {
let root = root.into();
HtmlConfig { HtmlConfig {
destination: root.into().join("book"), destination: root.clone().join("book"),
theme: None, theme: root.join("theme"),
curly_quotes: false, curly_quotes: false,
google_analytics: None, google_analytics: None,
additional_css: Vec::new(), additional_css: Vec::new(),
@ -39,19 +40,11 @@ impl HtmlConfig {
let root = root.into(); let root = root.into();
if let Some(d) = tomlconfig.destination { if let Some(d) = tomlconfig.destination {
if d.is_relative() { self.set_destination(&root, &d);
self.destination = root.join(d);
} else {
self.destination = d;
}
} }
if let Some(t) = tomlconfig.theme { if let Some(t) = tomlconfig.theme {
if t.is_relative() { self.set_theme(&root, &t);
self.theme = Some(root.join(t));
} else {
self.theme = Some(t);
}
} }
if let Some(curly_quotes) = tomlconfig.curly_quotes { if let Some(curly_quotes) = tomlconfig.curly_quotes {
@ -100,17 +93,16 @@ impl HtmlConfig {
&self.destination &self.destination
} }
// FIXME: How to get a `Option<&Path>` ? pub fn get_theme(&self) -> &Path {
pub fn get_theme(&self) -> Option<&PathBuf> { &self.theme
self.theme.as_ref()
} }
pub fn set_theme<T: Into<PathBuf>>(&mut self, root: T, theme: T) -> &mut Self { pub fn set_theme<T: Into<PathBuf>>(&mut self, root: T, theme: T) -> &mut Self {
let d = theme.into(); let d = theme.into();
if d.is_relative() { if d.is_relative() {
self.theme = Some(root.into().join(d)); self.theme = root.into().join(d);
} else { } else {
self.theme = Some(d); self.theme = d;
} }
self self

View File

@ -1,4 +1,4 @@
use std::path::PathBuf; use std::path::Path;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
@ -46,7 +46,7 @@ pub struct Theme {
} }
impl Theme { impl Theme {
pub fn new(src: Option<&PathBuf>) -> Self { pub fn new(src: Option<&Path>) -> Self {
// Default theme // Default theme
let mut theme = Theme { let mut theme = Theme {

View File

@ -83,5 +83,5 @@ fn from_json_output_html_theme() {
let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig"); let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig");
assert_eq!(htmlconfig.get_theme().expect("the theme key was provided"), &PathBuf::from("root/theme")); assert_eq!(htmlconfig.get_theme(), &PathBuf::from("root/theme"));
} }

View File

@ -84,7 +84,7 @@ fn from_toml_output_html_theme() {
let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig"); let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig");
assert_eq!(htmlconfig.get_theme().expect("the theme key was provided"), &PathBuf::from("root/theme")); assert_eq!(htmlconfig.get_theme(), &PathBuf::from("root/theme"));
} }
// Tests that the `output.html.curly-quotes` key is correctly parsed in the TOML config // Tests that the `output.html.curly-quotes` key is correctly parsed in the TOML config