Skip to content

Fixes missing the default "theme" dir location #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::process::Command;
use {theme, parse, utils};
use renderer::{Renderer, HtmlHandlebars};

use config::{BookConfig, HtmlConfig};
use config::BookConfig;
use config::tomlconfig::TomlConfig;
use config::jsonconfig::JsonConfig;

Expand Down Expand Up @@ -262,8 +262,9 @@ impl MDBook {
pub fn copy_theme(&self) -> Result<(), Box<Error>> {
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() {
debug!("[*]: {:?} does not exist, trying to create directory", themedir);
fs::create_dir(&themedir)?;
Expand Down Expand Up @@ -472,9 +473,9 @@ impl MDBook {
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() {
return htmlconfig.get_theme();
return Some(htmlconfig.get_theme());
}

None
Expand Down
28 changes: 10 additions & 18 deletions src/config/htmlconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::tomlconfig::TomlHtmlConfig;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct HtmlConfig {
destination: PathBuf,
theme: Option<PathBuf>,
theme: PathBuf,
curly_quotes: bool,
google_analytics: Option<String>,
additional_css: Vec<PathBuf>,
Expand All @@ -25,9 +25,10 @@ impl HtmlConfig {
/// assert_eq!(config.get_destination(), &output);
/// ```
pub fn new<T: Into<PathBuf>>(root: T) -> Self {
let root = root.into();
HtmlConfig {
destination: root.into().join("book"),
theme: None,
destination: root.clone().join("book"),
theme: root.join("theme"),
curly_quotes: false,
google_analytics: None,
additional_css: Vec::new(),
Expand All @@ -39,19 +40,11 @@ impl HtmlConfig {
let root = root.into();

if let Some(d) = tomlconfig.destination {
if d.is_relative() {
self.destination = root.join(d);
} else {
self.destination = d;
}
self.set_destination(&root, &d);
}

if let Some(t) = tomlconfig.theme {
if t.is_relative() {
self.theme = Some(root.join(t));
} else {
self.theme = Some(t);
}
self.set_theme(&root, &t);
}

if let Some(curly_quotes) = tomlconfig.curly_quotes {
Expand Down Expand Up @@ -100,17 +93,16 @@ impl HtmlConfig {
&self.destination
}

// FIXME: How to get a `Option<&Path>` ?
pub fn get_theme(&self) -> Option<&PathBuf> {
self.theme.as_ref()
pub fn get_theme(&self) -> &Path {
&self.theme
}

pub fn set_theme<T: Into<PathBuf>>(&mut self, root: T, theme: T) -> &mut Self {
let d = theme.into();
if d.is_relative() {
self.theme = Some(root.into().join(d));
self.theme = root.into().join(d);
} else {
self.theme = Some(d);
self.theme = d;
}

self
Expand Down
4 changes: 2 additions & 2 deletions src/theme/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::path::Path;
use std::fs::File;
use std::io::Read;

Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct Theme {
}

impl Theme {
pub fn new(src: Option<&PathBuf>) -> Self {
pub fn new(src: Option<&Path>) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we can remove the option here too, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about it but but there is this problem that while in the context of HtmlConfig the theme is guaranteed, it is not in the context of MdBook as the HtmlConfig is optional itself

    pub fn get_theme_path(&self) -> Option<&Path> {
        if let Some(htmlconfig) = self.config.get_html_config() {
            return Some(htmlconfig.get_theme());
        }

        None
    }

So removing this option will make us jump through hoops here:

  --> src/renderer/html_handlebars/hbs_renderer.rs:36:39
   |
36 |         let theme = theme::Theme::new(book.get_theme_path());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, let's keep it like it is for now then. Maybe it will get refactored with all the planned changes anyway.


// Default theme
let mut theme = Theme {
Expand Down
2 changes: 1 addition & 1 deletion tests/jsonconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ fn from_json_output_html_theme() {

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"));
}
2 changes: 1 addition & 1 deletion tests/tomlconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn from_toml_output_html_theme() {

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
Expand Down