2017-05-19 01:32:08 +08:00
|
|
|
use std::path::{PathBuf, Path};
|
|
|
|
|
|
|
|
use super::tomlconfig::TomlHtmlConfig;
|
2017-06-29 12:35:20 +08:00
|
|
|
use super::playpenconfig::PlaypenConfig;
|
2017-05-19 01:32:08 +08:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
|
|
pub struct HtmlConfig {
|
|
|
|
destination: PathBuf,
|
2017-06-23 18:57:58 +08:00
|
|
|
theme: PathBuf,
|
2017-06-01 13:28:08 +08:00
|
|
|
curly_quotes: bool,
|
2017-06-25 06:32:26 +08:00
|
|
|
mathjax_support: bool,
|
2017-05-19 01:32:08 +08:00
|
|
|
google_analytics: Option<String>,
|
2017-05-20 19:56:01 +08:00
|
|
|
additional_css: Vec<PathBuf>,
|
2017-06-07 02:34:57 +08:00
|
|
|
additional_js: Vec<PathBuf>,
|
2017-06-29 12:35:20 +08:00
|
|
|
playpen: PlaypenConfig,
|
2017-05-19 01:32:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-06-23 18:57:58 +08:00
|
|
|
let root = root.into();
|
2017-06-29 12:35:20 +08:00
|
|
|
let theme = root.join("theme");
|
2017-05-19 01:32:08 +08:00
|
|
|
HtmlConfig {
|
2017-06-23 18:57:58 +08:00
|
|
|
destination: root.clone().join("book"),
|
2017-06-29 12:35:20 +08:00
|
|
|
theme: theme.clone(),
|
2017-06-01 13:28:08 +08:00
|
|
|
curly_quotes: false,
|
2017-06-25 06:32:26 +08:00
|
|
|
mathjax_support: false,
|
2017-05-19 01:32:08 +08:00
|
|
|
google_analytics: None,
|
2017-05-20 19:56:01 +08:00
|
|
|
additional_css: Vec::new(),
|
2017-06-07 02:34:57 +08:00
|
|
|
additional_js: Vec::new(),
|
2017-06-29 12:35:20 +08:00
|
|
|
playpen: PlaypenConfig::new(theme),
|
2017-05-19 01:32:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
pub fn fill_from_tomlconfig<T: Into<PathBuf>>(&mut self, root: T, tomlconfig: TomlHtmlConfig) -> &mut Self {
|
|
|
|
let root = root.into();
|
|
|
|
|
2017-05-19 01:32:08 +08:00
|
|
|
if let Some(d) = tomlconfig.destination {
|
2017-06-23 23:01:11 +08:00
|
|
|
self.set_destination(&root, &d);
|
2017-05-19 01:32:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(t) = tomlconfig.theme {
|
2017-06-23 23:01:11 +08:00
|
|
|
self.set_theme(&root, &t);
|
2017-05-19 01:32:08 +08:00
|
|
|
}
|
|
|
|
|
2017-06-01 13:28:08 +08:00
|
|
|
if let Some(curly_quotes) = tomlconfig.curly_quotes {
|
|
|
|
self.curly_quotes = curly_quotes;
|
|
|
|
}
|
|
|
|
|
2017-06-25 06:32:26 +08:00
|
|
|
if let Some(mathjax_support) = tomlconfig.mathjax_support {
|
|
|
|
self.mathjax_support = mathjax_support;
|
|
|
|
}
|
|
|
|
|
2017-05-19 01:32:08 +08:00
|
|
|
if tomlconfig.google_analytics.is_some() {
|
|
|
|
self.google_analytics = tomlconfig.google_analytics;
|
|
|
|
}
|
|
|
|
|
2017-05-20 19:56:01 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 02:34:57 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 12:35:20 +08:00
|
|
|
if let Some(playpen) = tomlconfig.playpen {
|
|
|
|
self.playpen.fill_from_tomlconfig(&self.theme, playpen);
|
|
|
|
}
|
|
|
|
|
2017-05-19 01:32:08 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-19 01:32:08 +08:00
|
|
|
pub fn get_destination(&self) -> &Path {
|
|
|
|
&self.destination
|
|
|
|
}
|
|
|
|
|
2017-06-23 18:57:58 +08:00
|
|
|
pub fn get_theme(&self) -> &Path {
|
|
|
|
&self.theme
|
2017-05-19 01:32:08 +08:00
|
|
|
}
|
2017-05-19 05:52:38 +08:00
|
|
|
|
|
|
|
pub fn set_theme<T: Into<PathBuf>>(&mut self, root: T, theme: T) -> &mut Self {
|
|
|
|
let d = theme.into();
|
|
|
|
if d.is_relative() {
|
2017-06-23 18:57:58 +08:00
|
|
|
self.theme = root.into().join(d);
|
2017-05-19 05:52:38 +08:00
|
|
|
} else {
|
2017-06-23 18:57:58 +08:00
|
|
|
self.theme = d;
|
2017-05-19 05:52:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2017-05-19 06:56:37 +08:00
|
|
|
|
2017-06-01 13:28:08 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-06-25 06:32:26 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-19 06:56:37 +08:00
|
|
|
pub fn get_google_analytics_id(&self) -> Option<String> {
|
|
|
|
self.google_analytics.clone()
|
|
|
|
}
|
2017-05-20 19:56:01 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-06-07 02:34:57 +08:00
|
|
|
|
|
|
|
pub fn has_additional_js(&self) -> bool {
|
|
|
|
!self.additional_js.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_additional_js(&self) -> &[PathBuf] {
|
|
|
|
&self.additional_js
|
|
|
|
}
|
2017-06-29 12:35:20 +08:00
|
|
|
|
|
|
|
pub fn get_playpen_config(&self) -> &PlaypenConfig {
|
|
|
|
&self.playpen
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_mut_playpen_config(&mut self) -> &mut PlaypenConfig {
|
|
|
|
&mut self.playpen
|
|
|
|
}
|
2017-05-19 01:32:08 +08:00
|
|
|
}
|