1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::path::{PathBuf, Path};

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<String>,
    additional_css: Vec<PathBuf>,
    additional_js: Vec<PathBuf>,
    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<T: Into<PathBuf>>(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<T: Into<PathBuf>>(&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<T: Into<PathBuf>>(&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<T: Into<PathBuf>>(&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<String> {
        self.google_analytics.clone()
    }

    pub fn set_google_analytics_id(&mut self, id: Option<String>) -> &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
    }
}