2018-01-14 02:38:43 +08:00
|
|
|
//! Mdbook's configuration system.
|
2018-03-07 21:02:06 +08:00
|
|
|
//!
|
2018-01-21 22:35:11 +08:00
|
|
|
//! The main entrypoint of the `config` module is the `Config` struct. This acts
|
|
|
|
//! essentially as a bag of configuration information, with a couple
|
2021-01-11 06:51:30 +08:00
|
|
|
//! pre-determined tables ([`BookConfig`] and [`BuildConfig`]) as well as support
|
2019-05-20 04:16:10 +08:00
|
|
|
//! for arbitrary data which is exposed to plugins and alternative backends.
|
2018-03-07 21:02:06 +08:00
|
|
|
//!
|
|
|
|
//!
|
2018-01-21 22:35:11 +08:00
|
|
|
//! # Examples
|
2018-03-07 21:02:06 +08:00
|
|
|
//!
|
2018-01-21 22:35:11 +08:00
|
|
|
//! ```rust
|
|
|
|
//! # use mdbook::errors::*;
|
|
|
|
//! use std::path::PathBuf;
|
2019-05-07 02:20:58 +08:00
|
|
|
//! use std::str::FromStr;
|
2018-01-21 22:35:11 +08:00
|
|
|
//! use mdbook::Config;
|
|
|
|
//! use toml::Value;
|
2018-03-07 21:02:06 +08:00
|
|
|
//!
|
2018-01-21 22:35:11 +08:00
|
|
|
//! # fn run() -> Result<()> {
|
|
|
|
//! let src = r#"
|
|
|
|
//! [book]
|
|
|
|
//! title = "My Book"
|
|
|
|
//! authors = ["Michael-F-Bryan"]
|
2018-03-07 21:02:06 +08:00
|
|
|
//!
|
2018-01-21 22:35:11 +08:00
|
|
|
//! [build]
|
|
|
|
//! src = "out"
|
2018-03-07 21:02:06 +08:00
|
|
|
//!
|
2018-01-21 22:35:11 +08:00
|
|
|
//! [other-table.foo]
|
|
|
|
//! bar = 123
|
|
|
|
//! "#;
|
2018-03-07 21:02:06 +08:00
|
|
|
//!
|
2018-01-21 22:35:11 +08:00
|
|
|
//! // load the `Config` from a toml string
|
|
|
|
//! let mut cfg = Config::from_str(src)?;
|
2018-03-07 21:02:06 +08:00
|
|
|
//!
|
2018-01-21 22:35:11 +08:00
|
|
|
//! // retrieve a nested value
|
|
|
|
//! let bar = cfg.get("other-table.foo.bar").cloned();
|
|
|
|
//! assert_eq!(bar, Some(Value::Integer(123)));
|
2018-03-07 21:02:06 +08:00
|
|
|
//!
|
2018-01-21 22:35:11 +08:00
|
|
|
//! // Set the `output.html.theme` directory
|
|
|
|
//! assert!(cfg.get("output.html").is_none());
|
|
|
|
//! cfg.set("output.html.theme", "./themes");
|
2018-03-07 21:02:06 +08:00
|
|
|
//!
|
2018-01-21 22:35:11 +08:00
|
|
|
//! // then load it again, automatically deserializing to a `PathBuf`.
|
2019-10-06 06:33:50 +08:00
|
|
|
//! let got: Option<PathBuf> = cfg.get_deserialized_opt("output.html.theme")?;
|
|
|
|
//! assert_eq!(got, Some(PathBuf::from("./themes")));
|
2018-01-21 22:35:11 +08:00
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
2020-05-10 23:29:50 +08:00
|
|
|
//! # run().unwrap()
|
2018-01-21 22:35:11 +08:00
|
|
|
//! ```
|
|
|
|
|
|
|
|
#![deny(missing_docs)]
|
2018-01-14 02:38:43 +08:00
|
|
|
|
2018-07-24 01:45:01 +08:00
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
2020-05-27 02:04:12 +08:00
|
|
|
use std::collections::HashMap;
|
2018-07-24 01:45:01 +08:00
|
|
|
use std::env;
|
2017-09-30 20:11:24 +08:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
2018-07-24 01:45:01 +08:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-05-07 02:20:58 +08:00
|
|
|
use std::str::FromStr;
|
2017-11-12 02:03:28 +08:00
|
|
|
use toml::value::Table;
|
2018-07-24 01:45:01 +08:00
|
|
|
use toml::{self, Value};
|
2017-09-30 20:11:24 +08:00
|
|
|
|
2019-05-26 02:50:41 +08:00
|
|
|
use crate::errors::*;
|
2020-05-21 05:32:00 +08:00
|
|
|
use crate::utils::{self, toml_ext::TomlExt};
|
2017-09-30 20:11:24 +08:00
|
|
|
|
2018-01-21 22:35:11 +08:00
|
|
|
/// The overall configuration object for MDBook, essentially an in-memory
|
|
|
|
/// representation of `book.toml`.
|
2018-01-14 02:38:43 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2017-09-30 20:11:24 +08:00
|
|
|
pub struct Config {
|
|
|
|
/// Metadata about the book.
|
2017-09-30 21:04:05 +08:00
|
|
|
pub book: BookConfig,
|
2018-01-21 22:35:11 +08:00
|
|
|
/// Information about the build environment.
|
2017-11-30 12:02:58 +08:00
|
|
|
pub build: BuildConfig,
|
2020-04-22 03:21:56 +08:00
|
|
|
/// Information about Rust language support.
|
2020-03-09 23:02:54 +08:00
|
|
|
pub rust: RustConfig,
|
2018-01-14 02:38:43 +08:00
|
|
|
rest: Value,
|
2017-09-30 20:11:24 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 02:20:58 +08:00
|
|
|
impl FromStr for Config {
|
|
|
|
type Err = Error;
|
|
|
|
|
2017-09-30 20:11:24 +08:00
|
|
|
/// Load a `Config` from some string.
|
2019-05-07 02:20:58 +08:00
|
|
|
fn from_str(src: &str) -> Result<Self> {
|
2020-05-21 05:32:00 +08:00
|
|
|
toml::from_str(src).with_context(|| "Invalid configuration file")
|
2017-09-30 20:11:24 +08:00
|
|
|
}
|
2019-05-07 02:20:58 +08:00
|
|
|
}
|
2017-09-30 20:11:24 +08:00
|
|
|
|
2019-05-07 02:20:58 +08:00
|
|
|
impl Config {
|
2017-09-30 20:11:24 +08:00
|
|
|
/// Load the configuration file from disk.
|
|
|
|
pub fn from_disk<P: AsRef<Path>>(config_file: P) -> Result<Config> {
|
|
|
|
let mut buffer = String::new();
|
2018-01-07 22:10:48 +08:00
|
|
|
File::open(config_file)
|
2020-05-21 05:32:00 +08:00
|
|
|
.with_context(|| "Unable to open the configuration file")?
|
2018-01-07 22:10:48 +08:00
|
|
|
.read_to_string(&mut buffer)
|
2020-05-21 05:32:00 +08:00
|
|
|
.with_context(|| "Couldn't read the file")?;
|
2017-09-30 20:11:24 +08:00
|
|
|
|
|
|
|
Config::from_str(&buffer)
|
|
|
|
}
|
|
|
|
|
2018-01-14 02:38:43 +08:00
|
|
|
/// Updates the `Config` from the available environment variables.
|
|
|
|
///
|
|
|
|
/// Variables starting with `MDBOOK_` are used for configuration. The key is
|
|
|
|
/// created by removing the `MDBOOK_` prefix and turning the resulting
|
|
|
|
/// string into `kebab-case`. Double underscores (`__`) separate nested
|
|
|
|
/// keys, while a single underscore (`_`) is replaced with a dash (`-`).
|
|
|
|
///
|
|
|
|
/// For example:
|
|
|
|
///
|
|
|
|
/// - `MDBOOK_foo` -> `foo`
|
|
|
|
/// - `MDBOOK_FOO` -> `foo`
|
|
|
|
/// - `MDBOOK_FOO__BAR` -> `foo.bar`
|
|
|
|
/// - `MDBOOK_FOO_BAR` -> `foo-bar`
|
|
|
|
/// - `MDBOOK_FOO_bar__baz` -> `foo-bar.baz`
|
|
|
|
///
|
|
|
|
/// So by setting the `MDBOOK_BOOK__TITLE` environment variable you can
|
|
|
|
/// override the book's title without needing to touch your `book.toml`.
|
|
|
|
///
|
|
|
|
/// > **Note:** To facilitate setting more complex config items, the value
|
|
|
|
/// > of an environment variable is first parsed as JSON, falling back to a
|
|
|
|
/// > string if the parse fails.
|
|
|
|
/// >
|
|
|
|
/// > This means, if you so desired, you could override all book metadata
|
|
|
|
/// > when building the book with something like
|
|
|
|
/// >
|
|
|
|
/// > ```text
|
2020-05-03 17:58:41 +08:00
|
|
|
/// > $ export MDBOOK_BOOK='{"title": "My Awesome Book", "authors": ["Michael-F-Bryan"]}'
|
2018-01-14 02:38:43 +08:00
|
|
|
/// > $ mdbook build
|
|
|
|
/// > ```
|
|
|
|
///
|
|
|
|
/// The latter case may be useful in situations where `mdbook` is invoked
|
|
|
|
/// from a script or CI, where it sometimes isn't possible to update the
|
|
|
|
/// `book.toml` before building.
|
|
|
|
pub fn update_from_env(&mut self) {
|
|
|
|
debug!("Updating the config from environment variables");
|
|
|
|
|
2019-06-20 20:18:17 +08:00
|
|
|
let overrides =
|
|
|
|
env::vars().filter_map(|(key, value)| parse_env(&key).map(|index| (index, value)));
|
2018-01-14 02:38:43 +08:00
|
|
|
|
|
|
|
for (key, value) in overrides {
|
|
|
|
trace!("{} => {}", key, value);
|
|
|
|
let parsed_value = serde_json::from_str(&value)
|
|
|
|
.unwrap_or_else(|_| serde_json::Value::String(value.to_string()));
|
|
|
|
|
2020-05-03 17:16:44 +08:00
|
|
|
if key == "book" || key == "build" {
|
2020-05-03 16:54:17 +08:00
|
|
|
if let serde_json::Value::Object(ref map) = parsed_value {
|
|
|
|
// To `set` each `key`, we wrap them as `prefix.key`
|
|
|
|
for (k, v) in map {
|
2020-05-08 18:56:41 +08:00
|
|
|
let full_key = format!("{}.{}", key, k);
|
|
|
|
self.set(&full_key, v).expect("unreachable");
|
2020-05-03 16:54:17 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 02:38:43 +08:00
|
|
|
self.set(key, parsed_value).expect("unreachable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 02:03:28 +08:00
|
|
|
/// Fetch an arbitrary item from the `Config` as a `toml::Value`.
|
|
|
|
///
|
|
|
|
/// You can use dotted indices to access nested items (e.g.
|
2020-06-22 22:34:25 +08:00
|
|
|
/// `output.html.playground` will fetch the "playground" out of the html output
|
2017-11-12 02:03:28 +08:00
|
|
|
/// table).
|
|
|
|
pub fn get(&self, key: &str) -> Option<&Value> {
|
2020-05-21 05:32:00 +08:00
|
|
|
self.rest.read(key)
|
2017-11-12 02:03:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch a value from the `Config` so you can mutate it.
|
2019-06-20 20:18:27 +08:00
|
|
|
pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
|
2020-05-21 05:32:00 +08:00
|
|
|
self.rest.read_mut(key)
|
2017-11-12 02:03:28 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 20:11:24 +08:00
|
|
|
/// Convenience method for getting the html renderer's configuration.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
|
|
|
/// This is for compatibility only. It will be removed completely once the
|
2018-01-07 22:10:48 +08:00
|
|
|
/// HTML renderer is refactored to be less coupled to `mdbook` internals.
|
|
|
|
#[doc(hidden)]
|
2017-09-30 21:04:05 +08:00
|
|
|
pub fn html_config(&self) -> Option<HtmlConfig> {
|
2020-05-21 05:32:00 +08:00
|
|
|
match self
|
|
|
|
.get_deserialized_opt("output.html")
|
|
|
|
.with_context(|| "Parsing configuration [output.html]")
|
|
|
|
{
|
2019-10-06 06:33:50 +08:00
|
|
|
Ok(Some(config)) => Some(config),
|
|
|
|
Ok(None) => None,
|
2019-09-22 20:27:14 +08:00
|
|
|
Err(e) => {
|
2020-05-21 05:32:00 +08:00
|
|
|
utils::log_backtrace(&e);
|
2019-09-22 20:27:14 +08:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2017-09-30 20:34:27 +08:00
|
|
|
}
|
|
|
|
|
2019-10-06 06:33:50 +08:00
|
|
|
/// Deprecated, use get_deserialized_opt instead.
|
|
|
|
#[deprecated = "use get_deserialized_opt instead"]
|
2017-11-12 02:03:28 +08:00
|
|
|
pub fn get_deserialized<'de, T: Deserialize<'de>, S: AsRef<str>>(&self, name: S) -> Result<T> {
|
|
|
|
let name = name.as_ref();
|
2019-10-06 06:33:50 +08:00
|
|
|
match self.get_deserialized_opt(name)? {
|
|
|
|
Some(value) => Ok(value),
|
|
|
|
None => bail!("Key not found, {:?}", name),
|
2017-11-12 02:03:28 +08:00
|
|
|
}
|
2017-09-30 20:34:27 +08:00
|
|
|
}
|
2017-11-12 21:00:18 +08:00
|
|
|
|
2019-10-06 06:33:50 +08:00
|
|
|
/// Convenience function to fetch a value from the config and deserialize it
|
|
|
|
/// into some arbitrary type.
|
|
|
|
pub fn get_deserialized_opt<'de, T: Deserialize<'de>, S: AsRef<str>>(
|
|
|
|
&self,
|
|
|
|
name: S,
|
|
|
|
) -> Result<Option<T>> {
|
|
|
|
let name = name.as_ref();
|
|
|
|
self.get(name)
|
|
|
|
.map(|value| {
|
|
|
|
value
|
|
|
|
.clone()
|
|
|
|
.try_into()
|
2020-05-21 05:32:00 +08:00
|
|
|
.with_context(|| "Couldn't deserialize the value")
|
2019-10-06 06:33:50 +08:00
|
|
|
})
|
|
|
|
.transpose()
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
/// Set a config key, clobbering any existing values along the way.
|
|
|
|
///
|
|
|
|
/// The only way this can fail is if we can't serialize `value` into a
|
|
|
|
/// `toml::Value`.
|
|
|
|
pub fn set<S: Serialize, I: AsRef<str>>(&mut self, index: I, value: S) -> Result<()> {
|
2018-01-14 02:38:43 +08:00
|
|
|
let index = index.as_ref();
|
|
|
|
|
2020-05-21 05:32:00 +08:00
|
|
|
let value = Value::try_from(value)
|
|
|
|
.with_context(|| "Unable to represent the item as a JSON Value")?;
|
2018-01-14 02:38:43 +08:00
|
|
|
|
|
|
|
if index.starts_with("book.") {
|
|
|
|
self.book.update_value(&index[5..], value);
|
|
|
|
} else if index.starts_with("build.") {
|
|
|
|
self.build.update_value(&index[6..], value);
|
|
|
|
} else {
|
2020-05-21 05:32:00 +08:00
|
|
|
self.rest.insert(index, value);
|
2018-01-14 02:38:43 +08:00
|
|
|
}
|
2018-01-07 22:10:48 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
/// Get the table associated with a particular renderer.
|
|
|
|
pub fn get_renderer<I: AsRef<str>>(&self, index: I) -> Option<&Table> {
|
|
|
|
let key = format!("output.{}", index.as_ref());
|
2019-05-07 02:20:58 +08:00
|
|
|
self.get(&key).and_then(Value::as_table)
|
2018-09-10 18:55:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the table associated with a particular preprocessor.
|
|
|
|
pub fn get_preprocessor<I: AsRef<str>>(&self, index: I) -> Option<&Table> {
|
|
|
|
let key = format!("preprocessor.{}", index.as_ref());
|
2019-05-07 02:20:58 +08:00
|
|
|
self.get(&key).and_then(Value::as_table)
|
2018-09-10 18:55:58 +08:00
|
|
|
}
|
|
|
|
|
2018-01-14 04:54:11 +08:00
|
|
|
fn from_legacy(mut table: Value) -> Config {
|
2017-11-12 21:00:18 +08:00
|
|
|
let mut cfg = Config::default();
|
|
|
|
|
|
|
|
// we use a macro here instead of a normal loop because the $out
|
|
|
|
// variable can be different types. This way we can make type inference
|
|
|
|
// figure out what try_into() deserializes to.
|
|
|
|
macro_rules! get_and_insert {
|
|
|
|
($table:expr, $key:expr => $out:expr) => {
|
2018-07-24 01:45:01 +08:00
|
|
|
let got = $table
|
|
|
|
.as_table_mut()
|
|
|
|
.and_then(|t| t.remove($key))
|
|
|
|
.and_then(|v| v.try_into().ok());
|
2018-01-14 04:54:11 +08:00
|
|
|
if let Some(value) = got {
|
2017-11-12 21:00:18 +08:00
|
|
|
$out = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get_and_insert!(table, "title" => cfg.book.title);
|
|
|
|
get_and_insert!(table, "authors" => cfg.book.authors);
|
|
|
|
get_and_insert!(table, "source" => cfg.book.src);
|
|
|
|
get_and_insert!(table, "description" => cfg.book.description);
|
|
|
|
|
2020-05-21 05:32:00 +08:00
|
|
|
if let Some(dest) = table.delete("output.html.destination") {
|
2018-01-14 04:54:11 +08:00
|
|
|
if let Ok(destination) = dest.try_into() {
|
|
|
|
cfg.build.build_dir = destination;
|
|
|
|
}
|
2017-11-12 21:00:18 +08:00
|
|
|
}
|
|
|
|
|
2018-01-14 04:54:11 +08:00
|
|
|
cfg.rest = table;
|
2017-11-12 21:00:18 +08:00
|
|
|
cfg
|
|
|
|
}
|
2017-11-12 02:03:28 +08:00
|
|
|
}
|
2017-09-30 20:34:27 +08:00
|
|
|
|
2018-01-14 02:38:43 +08:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
|
|
|
book: BookConfig::default(),
|
|
|
|
build: BuildConfig::default(),
|
2020-03-09 23:02:54 +08:00
|
|
|
rust: RustConfig::default(),
|
2018-01-14 02:38:43 +08:00
|
|
|
rest: Value::Table(Table::default()),
|
2018-01-07 22:10:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-12 02:03:28 +08:00
|
|
|
impl<'de> Deserialize<'de> for Config {
|
2019-05-31 00:12:33 +08:00
|
|
|
fn deserialize<D: Deserializer<'de>>(de: D) -> std::result::Result<Self, D::Error> {
|
2017-11-12 02:03:28 +08:00
|
|
|
let raw = Value::deserialize(de)?;
|
2017-11-12 21:00:18 +08:00
|
|
|
|
2018-01-14 04:54:11 +08:00
|
|
|
if is_legacy_format(&raw) {
|
2017-11-12 21:00:18 +08:00
|
|
|
warn!("It looks like you are using the legacy book.toml format.");
|
|
|
|
warn!("We'll parse it for now, but you should probably convert to the new format.");
|
2017-11-12 21:26:59 +08:00
|
|
|
warn!("See the mdbook documentation for more details, although as a rule of thumb");
|
2017-11-30 23:26:30 +08:00
|
|
|
warn!("just move all top level configuration entries like `title`, `author` and");
|
|
|
|
warn!("`description` under a table called `[book]`, move the `destination` entry");
|
|
|
|
warn!("from `[output.html]`, renamed to `build-dir`, under a table called");
|
|
|
|
warn!("`[build]`, and it should all work.");
|
2019-10-29 21:04:16 +08:00
|
|
|
warn!("Documentation: http://rust-lang.github.io/mdBook/format/config.html");
|
2018-01-14 04:54:11 +08:00
|
|
|
return Ok(Config::from_legacy(raw));
|
2017-10-16 20:47:22 +08:00
|
|
|
}
|
2017-11-12 21:00:18 +08:00
|
|
|
|
2018-01-14 04:54:11 +08:00
|
|
|
let mut table = match raw {
|
|
|
|
Value::Table(t) => t,
|
|
|
|
_ => {
|
|
|
|
use serde::de::Error;
|
|
|
|
return Err(D::Error::custom(
|
|
|
|
"A config file should always be a toml table",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
let book: BookConfig = table
|
|
|
|
.remove("book")
|
|
|
|
.and_then(|value| value.try_into().ok())
|
|
|
|
.unwrap_or_default();
|
2017-11-30 12:02:58 +08:00
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
let build: BuildConfig = table
|
|
|
|
.remove("build")
|
|
|
|
.and_then(|value| value.try_into().ok())
|
|
|
|
.unwrap_or_default();
|
2017-11-30 12:02:58 +08:00
|
|
|
|
2020-03-09 23:02:54 +08:00
|
|
|
let rust: RustConfig = table
|
|
|
|
.remove("rust")
|
|
|
|
.and_then(|value| value.try_into().ok())
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
2017-11-12 21:00:18 +08:00
|
|
|
Ok(Config {
|
2018-12-04 07:10:09 +08:00
|
|
|
book,
|
|
|
|
build,
|
2020-03-09 23:02:54 +08:00
|
|
|
rust,
|
2018-01-14 02:38:43 +08:00
|
|
|
rest: Value::Table(table),
|
2017-11-12 21:00:18 +08:00
|
|
|
})
|
2017-09-30 20:11:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 21:22:30 +08:00
|
|
|
impl Serialize for Config {
|
2019-05-31 00:12:33 +08:00
|
|
|
fn serialize<S: Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
|
2020-04-22 03:21:56 +08:00
|
|
|
// TODO: This should probably be removed and use a derive instead.
|
2017-11-18 21:22:30 +08:00
|
|
|
let mut table = self.rest.clone();
|
|
|
|
|
2020-06-22 22:36:37 +08:00
|
|
|
let book_config = Value::try_from(&self.book).expect("should always be serializable");
|
2020-05-21 05:32:00 +08:00
|
|
|
table.insert("book", book_config);
|
2020-06-22 22:36:37 +08:00
|
|
|
|
2020-11-11 03:45:36 +08:00
|
|
|
if self.build != BuildConfig::default() {
|
|
|
|
let build_config = Value::try_from(&self.build).expect("should always be serializable");
|
|
|
|
table.insert("build", build_config);
|
|
|
|
}
|
|
|
|
|
2020-06-22 22:36:37 +08:00
|
|
|
if self.rust != RustConfig::default() {
|
|
|
|
let rust_config = Value::try_from(&self.rust).expect("should always be serializable");
|
|
|
|
table.insert("rust", rust_config);
|
|
|
|
}
|
|
|
|
|
2018-01-14 02:38:43 +08:00
|
|
|
table.serialize(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_env(key: &str) -> Option<String> {
|
|
|
|
const PREFIX: &str = "MDBOOK_";
|
2018-01-07 22:10:48 +08:00
|
|
|
|
2018-01-14 02:38:43 +08:00
|
|
|
if key.starts_with(PREFIX) {
|
|
|
|
let key = &key[PREFIX.len()..];
|
|
|
|
|
|
|
|
Some(key.to_lowercase().replace("__", ".").replace("_", "-"))
|
|
|
|
} else {
|
|
|
|
None
|
2017-11-18 21:22:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 04:54:11 +08:00
|
|
|
fn is_legacy_format(table: &Value) -> bool {
|
|
|
|
let legacy_items = [
|
|
|
|
"title",
|
|
|
|
"authors",
|
|
|
|
"source",
|
|
|
|
"description",
|
|
|
|
"output.html.destination",
|
|
|
|
];
|
|
|
|
|
|
|
|
for item in &legacy_items {
|
2020-05-21 05:32:00 +08:00
|
|
|
if table.read(item).is_some() {
|
2018-01-14 04:54:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-11-12 21:00:18 +08:00
|
|
|
|
2018-01-14 04:54:11 +08:00
|
|
|
false
|
2017-11-12 21:00:18 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 20:11:24 +08:00
|
|
|
/// Configuration options which are specific to the book and required for
|
|
|
|
/// loading it from disk.
|
2017-09-30 20:34:27 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
2017-09-30 20:11:24 +08:00
|
|
|
#[serde(default, rename_all = "kebab-case")]
|
2017-09-30 21:04:05 +08:00
|
|
|
pub struct BookConfig {
|
2017-09-30 20:11:24 +08:00
|
|
|
/// The book's title.
|
|
|
|
pub title: Option<String>,
|
|
|
|
/// The book's authors.
|
|
|
|
pub authors: Vec<String>,
|
|
|
|
/// An optional description for the book.
|
|
|
|
pub description: Option<String>,
|
2017-11-12 02:03:28 +08:00
|
|
|
/// Location of the book source relative to the book's root directory.
|
2017-09-30 20:11:24 +08:00
|
|
|
pub src: PathBuf,
|
|
|
|
/// Does this book support more than one language?
|
|
|
|
pub multilingual: bool,
|
2019-05-30 10:53:49 +08:00
|
|
|
/// The main language of the book.
|
|
|
|
pub language: Option<String>,
|
2017-09-30 20:11:24 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 21:04:05 +08:00
|
|
|
impl Default for BookConfig {
|
|
|
|
fn default() -> BookConfig {
|
|
|
|
BookConfig {
|
2017-09-30 20:34:27 +08:00
|
|
|
title: None,
|
|
|
|
authors: Vec::new(),
|
|
|
|
description: None,
|
|
|
|
src: PathBuf::from("src"),
|
|
|
|
multilingual: false,
|
2019-05-30 10:53:49 +08:00
|
|
|
language: Some(String::from("en")),
|
2017-09-30 20:34:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 23:02:54 +08:00
|
|
|
/// Configuration for the build procedure.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(default, rename_all = "kebab-case")]
|
|
|
|
pub struct BuildConfig {
|
|
|
|
/// Where to put built artefacts relative to the book's root directory.
|
|
|
|
pub build_dir: PathBuf,
|
|
|
|
/// Should non-existent markdown files specified in `SUMMARY.md` be created
|
|
|
|
/// if they don't exist?
|
|
|
|
pub create_missing: bool,
|
|
|
|
/// Should the default preprocessors always be used when they are
|
|
|
|
/// compatible with the renderer?
|
|
|
|
pub use_default_preprocessors: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for BuildConfig {
|
|
|
|
fn default() -> BuildConfig {
|
|
|
|
BuildConfig {
|
|
|
|
build_dir: PathBuf::from("book"),
|
|
|
|
create_missing: true,
|
|
|
|
use_default_preprocessors: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 22:34:25 +08:00
|
|
|
/// Configuration for the Rust compiler(e.g., for playground)
|
2020-03-09 23:02:54 +08:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(default, rename_all = "kebab-case")]
|
|
|
|
pub struct RustConfig {
|
2020-06-22 22:34:25 +08:00
|
|
|
/// Rust edition used in playground
|
2020-03-09 23:02:54 +08:00
|
|
|
pub edition: Option<RustEdition>,
|
|
|
|
}
|
|
|
|
|
2020-04-22 03:21:56 +08:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
|
2019-11-18 02:36:10 +08:00
|
|
|
/// Rust edition to use for the code.
|
|
|
|
pub enum RustEdition {
|
|
|
|
/// The 2018 edition of Rust
|
2020-04-22 03:21:56 +08:00
|
|
|
#[serde(rename = "2018")]
|
2019-11-18 02:36:10 +08:00
|
|
|
E2018,
|
|
|
|
/// The 2015 edition of Rust
|
2020-04-22 03:21:56 +08:00
|
|
|
#[serde(rename = "2015")]
|
2019-11-18 02:36:10 +08:00
|
|
|
E2015,
|
|
|
|
}
|
|
|
|
|
2018-01-21 22:35:11 +08:00
|
|
|
/// Configuration for the HTML renderer.
|
2020-05-19 14:09:25 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
2017-09-30 20:11:24 +08:00
|
|
|
#[serde(default, rename_all = "kebab-case")]
|
2017-09-30 21:04:05 +08:00
|
|
|
pub struct HtmlConfig {
|
2018-01-21 22:35:11 +08:00
|
|
|
/// The theme directory, if specified.
|
2017-09-30 20:11:24 +08:00
|
|
|
pub theme: Option<PathBuf>,
|
2018-10-13 02:57:59 +08:00
|
|
|
/// The default theme to use, defaults to 'light'
|
|
|
|
pub default_theme: Option<String>,
|
2019-10-05 07:32:03 +08:00
|
|
|
/// The theme to use if the browser requests the dark version of the site.
|
2020-04-22 01:18:44 +08:00
|
|
|
/// Defaults to 'navy'.
|
2019-09-26 06:23:54 +08:00
|
|
|
pub preferred_dark_theme: Option<String>,
|
2018-01-21 22:35:11 +08:00
|
|
|
/// Use "smart quotes" instead of the usual `"` character.
|
2017-09-30 20:11:24 +08:00
|
|
|
pub curly_quotes: bool,
|
2018-01-21 22:35:11 +08:00
|
|
|
/// Should mathjax be enabled?
|
2017-09-30 20:11:24 +08:00
|
|
|
pub mathjax_support: bool,
|
2020-05-19 14:09:25 +08:00
|
|
|
/// Whether to fonts.css and respective font files to the output directory.
|
|
|
|
pub copy_fonts: bool,
|
2018-01-21 22:35:11 +08:00
|
|
|
/// An optional google analytics code.
|
2017-09-30 20:11:24 +08:00
|
|
|
pub google_analytics: Option<String>,
|
2018-01-21 22:35:11 +08:00
|
|
|
/// Additional CSS stylesheets to include in the rendered page's `<head>`.
|
2017-09-30 20:11:24 +08:00
|
|
|
pub additional_css: Vec<PathBuf>,
|
2018-03-07 21:02:06 +08:00
|
|
|
/// Additional JS scripts to include at the bottom of the rendered page's
|
2018-01-21 22:35:11 +08:00
|
|
|
/// `<body>`.
|
2017-09-30 20:11:24 +08:00
|
|
|
pub additional_js: Vec<PathBuf>,
|
2019-10-19 15:56:08 +08:00
|
|
|
/// Fold settings.
|
|
|
|
pub fold: Fold,
|
2020-06-22 22:34:25 +08:00
|
|
|
/// Playground settings.
|
|
|
|
#[serde(alias = "playpen")]
|
|
|
|
pub playground: Playground,
|
2020-03-30 16:38:37 +08:00
|
|
|
/// Print settings.
|
|
|
|
pub print: Print,
|
2018-10-23 09:34:14 +08:00
|
|
|
/// Don't render section labels.
|
2018-01-08 00:31:46 +08:00
|
|
|
pub no_section_label: bool,
|
2018-03-07 21:02:06 +08:00
|
|
|
/// Search settings. If `None`, the default will be used.
|
|
|
|
pub search: Option<Search>,
|
2018-10-16 02:48:54 +08:00
|
|
|
/// Git repository url. If `None`, the git button will not be shown.
|
2018-10-13 19:17:33 +08:00
|
|
|
pub git_repository_url: Option<String>,
|
2018-12-04 07:10:09 +08:00
|
|
|
/// FontAwesome icon class to use for the Git repository link.
|
2018-10-16 02:48:54 +08:00
|
|
|
/// Defaults to `fa-github` if `None`.
|
|
|
|
pub git_repository_icon: Option<String>,
|
2020-06-10 18:31:34 +08:00
|
|
|
/// Input path for the 404 file, defaults to 404.md, set to "" to disable 404 file output
|
2020-05-13 20:45:35 +08:00
|
|
|
pub input_404: Option<String>,
|
2020-06-07 20:14:35 +08:00
|
|
|
/// Absolute url to site, used to emit correct paths for the 404 page, which might be accessed in a deeply nested directory
|
|
|
|
pub site_url: Option<String>,
|
2020-09-03 02:24:48 +08:00
|
|
|
/// The DNS subdomain or apex domain at which your book will be hosted. This
|
|
|
|
/// string will be written to a file named CNAME in the root of your site,
|
|
|
|
/// as required by GitHub Pages (see [*Managing a custom domain for your
|
|
|
|
/// GitHub Pages site*][custom domain]).
|
|
|
|
///
|
|
|
|
/// [custom domain]: https://docs.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site
|
|
|
|
pub cname: Option<String>,
|
2019-10-19 15:56:08 +08:00
|
|
|
/// This is used as a bit of a workaround for the `mdbook serve` command.
|
|
|
|
/// Basically, because you set the websocket port from the command line, the
|
|
|
|
/// `mdbook serve` command needs a way to let the HTML renderer know where
|
|
|
|
/// to point livereloading at, if it has been enabled.
|
|
|
|
///
|
|
|
|
/// This config item *should not be edited* by the end user.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub livereload_url: Option<String>,
|
2020-05-27 02:04:12 +08:00
|
|
|
/// The mapping from old pages to new pages/URLs to use when generating
|
|
|
|
/// redirects.
|
2020-05-27 03:12:57 +08:00
|
|
|
pub redirect: HashMap<String, String>,
|
2017-09-30 20:11:24 +08:00
|
|
|
}
|
|
|
|
|
2020-05-19 14:09:25 +08:00
|
|
|
impl Default for HtmlConfig {
|
|
|
|
fn default() -> HtmlConfig {
|
|
|
|
HtmlConfig {
|
|
|
|
theme: None,
|
|
|
|
default_theme: None,
|
|
|
|
preferred_dark_theme: None,
|
|
|
|
curly_quotes: false,
|
|
|
|
mathjax_support: false,
|
|
|
|
copy_fonts: true,
|
|
|
|
google_analytics: None,
|
|
|
|
additional_css: Vec::new(),
|
|
|
|
additional_js: Vec::new(),
|
|
|
|
fold: Fold::default(),
|
2020-06-22 22:34:25 +08:00
|
|
|
playground: Playground::default(),
|
2020-03-30 16:38:37 +08:00
|
|
|
print: Print::default(),
|
2020-05-19 14:09:25 +08:00
|
|
|
no_section_label: false,
|
|
|
|
search: None,
|
|
|
|
git_repository_url: None,
|
|
|
|
git_repository_icon: None,
|
2020-05-13 20:45:35 +08:00
|
|
|
input_404: None,
|
2020-06-07 20:14:35 +08:00
|
|
|
site_url: None,
|
2020-09-03 02:24:48 +08:00
|
|
|
cname: None,
|
2020-05-19 14:09:25 +08:00
|
|
|
livereload_url: None,
|
2020-05-27 02:23:36 +08:00
|
|
|
redirect: HashMap::new(),
|
2020-05-19 14:09:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 23:27:56 +08:00
|
|
|
impl HtmlConfig {
|
|
|
|
/// Returns the directory of theme from the provided root directory. If the
|
|
|
|
/// directory is not present it will append the default directory of "theme"
|
|
|
|
pub fn theme_dir(&self, root: &PathBuf) -> PathBuf {
|
|
|
|
match self.theme {
|
|
|
|
Some(ref d) => root.join(d),
|
|
|
|
None => root.join("theme"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:38:37 +08:00
|
|
|
/// Configuration for how to render the print icon, print.html, and print.css.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
pub struct Print {
|
|
|
|
/// Whether print support is enabled.
|
|
|
|
pub enable: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Print {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { enable: true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-19 15:56:08 +08:00
|
|
|
/// Configuration for how to fold chapters of sidebar.
|
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(default, rename_all = "kebab-case")]
|
|
|
|
pub struct Fold {
|
|
|
|
/// When off, all folds are open. Default: `false`.
|
|
|
|
pub enable: bool,
|
|
|
|
/// The higher the more folded regions are open. When level is 0, all folds
|
|
|
|
/// are closed.
|
|
|
|
/// Default: `0`.
|
|
|
|
pub level: u8,
|
|
|
|
}
|
|
|
|
|
2020-06-22 22:34:25 +08:00
|
|
|
/// Configuration for tweaking how the the HTML renderer handles the playground.
|
2017-12-21 13:18:12 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(default, rename_all = "kebab-case")]
|
2020-06-22 22:34:25 +08:00
|
|
|
pub struct Playground {
|
|
|
|
/// Should playground snippets be editable? Default: `false`.
|
2017-09-30 21:36:03 +08:00
|
|
|
pub editable: bool,
|
2019-10-17 18:44:54 +08:00
|
|
|
/// Display the copy button. Default: `true`.
|
|
|
|
pub copyable: bool,
|
2018-03-07 21:02:06 +08:00
|
|
|
/// Copy JavaScript files for the editor to the output directory?
|
|
|
|
/// Default: `true`.
|
|
|
|
pub copy_js: bool,
|
2020-06-22 22:34:25 +08:00
|
|
|
/// Display line numbers on playground snippets. Default: `false`.
|
2019-09-24 21:27:02 +08:00
|
|
|
pub line_numbers: bool,
|
2017-09-30 21:36:03 +08:00
|
|
|
}
|
2017-09-30 20:34:27 +08:00
|
|
|
|
2020-06-22 22:34:25 +08:00
|
|
|
impl Default for Playground {
|
|
|
|
fn default() -> Playground {
|
|
|
|
Playground {
|
2017-12-21 13:18:12 +08:00
|
|
|
editable: false,
|
2019-10-17 18:44:54 +08:00
|
|
|
copyable: true,
|
2018-03-07 21:02:06 +08:00
|
|
|
copy_js: true,
|
2019-09-24 21:27:02 +08:00
|
|
|
line_numbers: false,
|
2017-12-21 13:18:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
/// Configuration of the search functionality of the HTML renderer.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(default, rename_all = "kebab-case")]
|
|
|
|
pub struct Search {
|
2018-06-14 02:11:25 +08:00
|
|
|
/// Enable the search feature. Default: `true`.
|
|
|
|
pub enable: bool,
|
2018-03-07 21:02:06 +08:00
|
|
|
/// Maximum number of visible results. Default: `30`.
|
|
|
|
pub limit_results: u32,
|
2018-06-14 02:11:25 +08:00
|
|
|
/// The number of words used for a search result teaser. Default: `30`.
|
2018-03-07 21:02:06 +08:00
|
|
|
pub teaser_word_count: u32,
|
|
|
|
/// Define the logical link between multiple search words.
|
2019-10-03 10:35:42 +08:00
|
|
|
/// If true, all search words must appear in each result. Default: `false`.
|
2018-03-07 21:02:06 +08:00
|
|
|
pub use_boolean_and: bool,
|
|
|
|
/// Boost factor for the search result score if a search word appears in the header.
|
|
|
|
/// Default: `2`.
|
|
|
|
pub boost_title: u8,
|
|
|
|
/// Boost factor for the search result score if a search word appears in the hierarchy.
|
|
|
|
/// The hierarchy contains all titles of the parent documents and all parent headings.
|
|
|
|
/// Default: `1`.
|
|
|
|
pub boost_hierarchy: u8,
|
|
|
|
/// Boost factor for the search result score if a search word appears in the text.
|
|
|
|
/// Default: `1`.
|
|
|
|
pub boost_paragraph: u8,
|
|
|
|
/// True if the searchword `micro` should match `microwave`. Default: `true`.
|
2018-03-14 23:27:56 +08:00
|
|
|
pub expand: bool,
|
2018-03-07 21:02:06 +08:00
|
|
|
/// Documents are split into smaller parts, seperated by headings. This defines, until which
|
|
|
|
/// level of heading documents should be split. Default: `3`. (`### This is a level 3 heading`)
|
|
|
|
pub heading_split_level: u8,
|
|
|
|
/// Copy JavaScript files for the search functionality to the output directory?
|
|
|
|
/// Default: `true`.
|
|
|
|
pub copy_js: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Search {
|
|
|
|
fn default() -> Search {
|
|
|
|
// Please update the documentation of `Search` when changing values!
|
|
|
|
Search {
|
2018-06-14 02:11:25 +08:00
|
|
|
enable: true,
|
2018-03-07 21:02:06 +08:00
|
|
|
limit_results: 30,
|
|
|
|
teaser_word_count: 30,
|
|
|
|
use_boolean_and: false,
|
|
|
|
boost_title: 2,
|
|
|
|
boost_hierarchy: 1,
|
|
|
|
boost_paragraph: 1,
|
|
|
|
expand: true,
|
|
|
|
heading_split_level: 3,
|
|
|
|
copy_js: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 02:38:43 +08:00
|
|
|
/// Allows you to "update" any arbitrary field in a struct by round-tripping via
|
|
|
|
/// a `toml::Value`.
|
|
|
|
///
|
|
|
|
/// This is definitely not the most performant way to do things, which means you
|
|
|
|
/// should probably keep it away from tight loops...
|
|
|
|
trait Updateable<'de>: Serialize + Deserialize<'de> {
|
|
|
|
fn update_value<S: Serialize>(&mut self, key: &str, value: S) {
|
|
|
|
let mut raw = Value::try_from(&self).expect("unreachable");
|
|
|
|
|
2019-06-20 20:29:14 +08:00
|
|
|
if let Ok(value) = Value::try_from(value) {
|
|
|
|
let _ = raw.insert(key, value);
|
|
|
|
} else {
|
|
|
|
return;
|
2018-01-14 02:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(updated) = raw.try_into() {
|
|
|
|
*self = updated;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 09:22:49 +08:00
|
|
|
impl<'de, T> Updateable<'de> for T where T: Serialize + Deserialize<'de> {}
|
2018-01-14 02:38:43 +08:00
|
|
|
|
2017-09-30 20:34:27 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-06-10 18:31:34 +08:00
|
|
|
use crate::utils::fs::get_404_output_file;
|
2017-09-30 20:34:27 +08:00
|
|
|
|
2019-05-07 02:20:58 +08:00
|
|
|
const COMPLEX_CONFIG: &str = r#"
|
2017-09-30 20:34:27 +08:00
|
|
|
[book]
|
|
|
|
title = "Some Book"
|
|
|
|
authors = ["Michael-F-Bryan <michaelfbryan@gmail.com>"]
|
|
|
|
description = "A completely useless book"
|
|
|
|
multilingual = true
|
|
|
|
src = "source"
|
2019-05-30 10:53:49 +08:00
|
|
|
language = "ja"
|
2017-09-30 20:34:27 +08:00
|
|
|
|
2017-11-30 12:02:58 +08:00
|
|
|
[build]
|
2017-11-30 23:26:30 +08:00
|
|
|
build-dir = "outputs"
|
2017-11-30 12:02:58 +08:00
|
|
|
create-missing = false
|
2018-09-10 18:55:58 +08:00
|
|
|
use-default-preprocessors = true
|
2017-11-30 12:02:58 +08:00
|
|
|
|
2017-09-30 20:34:27 +08:00
|
|
|
[output.html]
|
2017-09-30 21:54:25 +08:00
|
|
|
theme = "./themedir"
|
2018-10-16 04:40:59 +08:00
|
|
|
default-theme = "rust"
|
2017-09-30 20:34:27 +08:00
|
|
|
curly-quotes = true
|
|
|
|
google-analytics = "123456"
|
|
|
|
additional-css = ["./foo/bar/baz.css"]
|
2018-10-16 02:48:54 +08:00
|
|
|
git-repository-url = "https://foo.com/"
|
|
|
|
git-repository-icon = "fa-code-fork"
|
2017-09-30 21:54:25 +08:00
|
|
|
|
2020-06-22 22:34:25 +08:00
|
|
|
[output.html.playground]
|
2017-09-30 21:54:25 +08:00
|
|
|
editable = true
|
|
|
|
editor = "ace"
|
2018-09-10 18:55:58 +08:00
|
|
|
|
2020-05-27 02:04:12 +08:00
|
|
|
[output.html.redirect]
|
|
|
|
"index.html" = "overview.html"
|
|
|
|
"nexted/page.md" = "https://rust-lang.org/"
|
|
|
|
|
2019-05-19 06:05:57 +08:00
|
|
|
[preprocessor.first]
|
2018-09-10 18:55:58 +08:00
|
|
|
|
2019-05-19 06:05:57 +08:00
|
|
|
[preprocessor.second]
|
2017-09-30 20:34:27 +08:00
|
|
|
"#;
|
|
|
|
|
2017-11-12 02:03:28 +08:00
|
|
|
#[test]
|
|
|
|
fn load_a_complex_config_file() {
|
|
|
|
let src = COMPLEX_CONFIG;
|
|
|
|
|
2017-09-30 21:54:25 +08:00
|
|
|
let book_should_be = BookConfig {
|
2017-09-30 20:34:27 +08:00
|
|
|
title: Some(String::from("Some Book")),
|
|
|
|
authors: vec![String::from("Michael-F-Bryan <michaelfbryan@gmail.com>")],
|
|
|
|
description: Some(String::from("A completely useless book")),
|
|
|
|
multilingual: true,
|
|
|
|
src: PathBuf::from("source"),
|
2019-05-30 10:53:49 +08:00
|
|
|
language: Some(String::from("ja")),
|
2017-09-30 20:34:27 +08:00
|
|
|
};
|
2017-11-30 12:02:58 +08:00
|
|
|
let build_should_be = BuildConfig {
|
2017-11-30 23:26:30 +08:00
|
|
|
build_dir: PathBuf::from("outputs"),
|
2017-11-30 12:02:58 +08:00
|
|
|
create_missing: false,
|
2018-09-10 18:55:58 +08:00
|
|
|
use_default_preprocessors: true,
|
2017-11-30 12:02:58 +08:00
|
|
|
};
|
2020-03-09 23:02:54 +08:00
|
|
|
let rust_should_be = RustConfig { edition: None };
|
2020-06-22 22:34:25 +08:00
|
|
|
let playground_should_be = Playground {
|
2017-09-30 21:54:25 +08:00
|
|
|
editable: true,
|
2019-10-17 18:44:54 +08:00
|
|
|
copyable: true,
|
2018-03-07 21:02:06 +08:00
|
|
|
copy_js: true,
|
2019-09-24 21:27:02 +08:00
|
|
|
line_numbers: false,
|
2017-09-30 21:54:25 +08:00
|
|
|
};
|
|
|
|
let html_should_be = HtmlConfig {
|
2017-09-30 20:34:27 +08:00
|
|
|
curly_quotes: true,
|
|
|
|
google_analytics: Some(String::from("123456")),
|
|
|
|
additional_css: vec![PathBuf::from("./foo/bar/baz.css")],
|
2017-09-30 21:54:25 +08:00
|
|
|
theme: Some(PathBuf::from("./themedir")),
|
2018-10-16 04:40:59 +08:00
|
|
|
default_theme: Some(String::from("rust")),
|
2020-06-22 22:34:25 +08:00
|
|
|
playground: playground_should_be,
|
2018-10-16 02:48:54 +08:00
|
|
|
git_repository_url: Some(String::from("https://foo.com/")),
|
|
|
|
git_repository_icon: Some(String::from("fa-code-fork")),
|
2020-05-27 02:04:12 +08:00
|
|
|
redirect: vec![
|
2020-05-27 03:12:57 +08:00
|
|
|
(String::from("index.html"), String::from("overview.html")),
|
2020-05-27 02:04:12 +08:00
|
|
|
(
|
2020-05-27 03:12:57 +08:00
|
|
|
String::from("nexted/page.md"),
|
2020-05-27 02:04:12 +08:00
|
|
|
String::from("https://rust-lang.org/"),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.collect(),
|
2017-09-30 20:34:27 +08:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let got = Config::from_str(src).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(got.book, book_should_be);
|
2017-11-30 12:02:58 +08:00
|
|
|
assert_eq!(got.build, build_should_be);
|
2020-03-09 23:02:54 +08:00
|
|
|
assert_eq!(got.rust, rust_should_be);
|
2017-09-30 20:34:27 +08:00
|
|
|
assert_eq!(got.html_config().unwrap(), html_should_be);
|
2017-09-30 20:11:24 +08:00
|
|
|
}
|
|
|
|
|
2019-11-18 02:36:10 +08:00
|
|
|
#[test]
|
|
|
|
fn edition_2015() {
|
|
|
|
let src = r#"
|
|
|
|
[book]
|
|
|
|
title = "mdBook Documentation"
|
|
|
|
description = "Create book from markdown files. Like Gitbook but implemented in Rust"
|
|
|
|
authors = ["Mathieu David"]
|
|
|
|
src = "./source"
|
2020-03-09 23:02:54 +08:00
|
|
|
[rust]
|
2019-11-18 02:36:10 +08:00
|
|
|
edition = "2015"
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let book_should_be = BookConfig {
|
|
|
|
title: Some(String::from("mdBook Documentation")),
|
|
|
|
description: Some(String::from(
|
|
|
|
"Create book from markdown files. Like Gitbook but implemented in Rust",
|
|
|
|
)),
|
|
|
|
authors: vec![String::from("Mathieu David")],
|
|
|
|
src: PathBuf::from("./source"),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let got = Config::from_str(src).unwrap();
|
|
|
|
assert_eq!(got.book, book_should_be);
|
2020-03-09 23:02:54 +08:00
|
|
|
|
|
|
|
let rust_should_be = RustConfig {
|
|
|
|
edition: Some(RustEdition::E2015),
|
|
|
|
};
|
|
|
|
let got = Config::from_str(src).unwrap();
|
|
|
|
assert_eq!(got.rust, rust_should_be);
|
2019-11-18 02:36:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn edition_2018() {
|
|
|
|
let src = r#"
|
|
|
|
[book]
|
|
|
|
title = "mdBook Documentation"
|
|
|
|
description = "Create book from markdown files. Like Gitbook but implemented in Rust"
|
|
|
|
authors = ["Mathieu David"]
|
|
|
|
src = "./source"
|
2020-03-09 23:02:54 +08:00
|
|
|
[rust]
|
2019-11-18 02:36:10 +08:00
|
|
|
edition = "2018"
|
|
|
|
"#;
|
|
|
|
|
2020-03-09 23:02:54 +08:00
|
|
|
let rust_should_be = RustConfig {
|
2019-11-18 02:36:10 +08:00
|
|
|
edition: Some(RustEdition::E2018),
|
|
|
|
};
|
|
|
|
|
|
|
|
let got = Config::from_str(src).unwrap();
|
2020-03-09 23:02:54 +08:00
|
|
|
assert_eq!(got.rust, rust_should_be);
|
2019-11-18 02:36:10 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 20:34:27 +08:00
|
|
|
#[test]
|
|
|
|
fn load_arbitrary_output_type() {
|
|
|
|
#[derive(Debug, Deserialize, PartialEq)]
|
|
|
|
struct RandomOutput {
|
|
|
|
foo: u32,
|
|
|
|
bar: String,
|
|
|
|
baz: Vec<bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let src = r#"
|
|
|
|
[output.random]
|
|
|
|
foo = 5
|
|
|
|
bar = "Hello World"
|
|
|
|
baz = [true, true, false]
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let should_be = RandomOutput {
|
|
|
|
foo: 5,
|
|
|
|
bar: String::from("Hello World"),
|
|
|
|
baz: vec![true, true, false],
|
|
|
|
};
|
|
|
|
|
|
|
|
let cfg = Config::from_str(src).unwrap();
|
2019-10-06 06:33:50 +08:00
|
|
|
let got: RandomOutput = cfg.get_deserialized_opt("output.random").unwrap().unwrap();
|
2017-09-30 20:34:27 +08:00
|
|
|
|
|
|
|
assert_eq!(got, should_be);
|
2017-11-12 02:03:28 +08:00
|
|
|
|
2019-10-06 06:33:50 +08:00
|
|
|
let got_baz: Vec<bool> = cfg
|
|
|
|
.get_deserialized_opt("output.random.baz")
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2017-11-12 02:03:28 +08:00
|
|
|
let baz_should_be = vec![true, true, false];
|
|
|
|
|
2019-05-07 02:20:58 +08:00
|
|
|
assert_eq!(got_baz, baz_should_be);
|
2017-09-30 20:34:27 +08:00
|
|
|
}
|
2017-11-12 02:03:28 +08:00
|
|
|
|
2017-11-12 21:00:18 +08:00
|
|
|
#[test]
|
|
|
|
fn mutate_some_stuff() {
|
|
|
|
// really this is just a sanity check to make sure the borrow checker
|
|
|
|
// is happy...
|
|
|
|
let src = COMPLEX_CONFIG;
|
|
|
|
let mut config = Config::from_str(src).unwrap();
|
2020-06-22 22:34:25 +08:00
|
|
|
let key = "output.html.playground.editable";
|
2017-11-12 21:00:18 +08:00
|
|
|
|
|
|
|
assert_eq!(config.get(key).unwrap(), &Value::Boolean(true));
|
|
|
|
*config.get_mut(key).unwrap() = Value::Boolean(false);
|
|
|
|
assert_eq!(config.get(key).unwrap(), &Value::Boolean(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The config file format has slightly changed (metadata stuff is now under
|
|
|
|
/// the `book` table instead of being at the top level) so we're adding a
|
|
|
|
/// **temporary** compatibility check. You should be able to still load the
|
|
|
|
/// old format, emitting a warning.
|
|
|
|
#[test]
|
|
|
|
fn can_still_load_the_previous_format() {
|
|
|
|
let src = r#"
|
|
|
|
title = "mdBook Documentation"
|
|
|
|
description = "Create book from markdown files. Like Gitbook but implemented in Rust"
|
|
|
|
authors = ["Mathieu David"]
|
|
|
|
source = "./source"
|
|
|
|
|
|
|
|
[output.html]
|
|
|
|
destination = "my-book" # the output files will be generated in `root/my-book` instead of `root/book`
|
|
|
|
theme = "my-theme"
|
|
|
|
curly-quotes = true
|
|
|
|
google-analytics = "123456"
|
|
|
|
additional-css = ["custom.css", "custom2.css"]
|
|
|
|
additional-js = ["custom.js"]
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let book_should_be = BookConfig {
|
|
|
|
title: Some(String::from("mdBook Documentation")),
|
|
|
|
description: Some(String::from(
|
|
|
|
"Create book from markdown files. Like Gitbook but implemented in Rust",
|
|
|
|
)),
|
|
|
|
authors: vec![String::from("Mathieu David")],
|
|
|
|
src: PathBuf::from("./source"),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2017-11-30 23:26:30 +08:00
|
|
|
let build_should_be = BuildConfig {
|
|
|
|
build_dir: PathBuf::from("my-book"),
|
|
|
|
create_missing: true,
|
2018-09-10 18:55:58 +08:00
|
|
|
use_default_preprocessors: true,
|
2017-11-30 23:26:30 +08:00
|
|
|
};
|
|
|
|
|
2017-11-12 21:00:18 +08:00
|
|
|
let html_should_be = HtmlConfig {
|
|
|
|
theme: Some(PathBuf::from("my-theme")),
|
|
|
|
curly_quotes: true,
|
|
|
|
google_analytics: Some(String::from("123456")),
|
|
|
|
additional_css: vec![PathBuf::from("custom.css"), PathBuf::from("custom2.css")],
|
|
|
|
additional_js: vec![PathBuf::from("custom.js")],
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let got = Config::from_str(src).unwrap();
|
|
|
|
assert_eq!(got.book, book_should_be);
|
2017-11-30 23:26:30 +08:00
|
|
|
assert_eq!(got.build, build_should_be);
|
2017-11-12 21:00:18 +08:00
|
|
|
assert_eq!(got.html_config().unwrap(), html_should_be);
|
|
|
|
}
|
2018-01-07 22:10:48 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_a_config_item() {
|
|
|
|
let mut cfg = Config::default();
|
|
|
|
let key = "foo.bar.baz";
|
|
|
|
let value = "Something Interesting";
|
|
|
|
|
|
|
|
assert!(cfg.get(key).is_none());
|
|
|
|
cfg.set(key, value).unwrap();
|
|
|
|
|
2019-10-06 06:33:50 +08:00
|
|
|
let got: String = cfg.get_deserialized_opt(key).unwrap().unwrap();
|
2018-01-07 22:10:48 +08:00
|
|
|
assert_eq!(got, value);
|
|
|
|
}
|
2018-01-14 02:38:43 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_env_vars() {
|
|
|
|
let inputs = vec![
|
|
|
|
("FOO", None),
|
|
|
|
("MDBOOK_foo", Some("foo")),
|
|
|
|
("MDBOOK_FOO__bar__baz", Some("foo.bar.baz")),
|
|
|
|
("MDBOOK_FOO_bar__baz", Some("foo-bar.baz")),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (src, should_be) in inputs {
|
|
|
|
let got = parse_env(src);
|
2019-05-07 02:20:58 +08:00
|
|
|
let should_be = should_be.map(ToString::to_string);
|
2018-01-14 02:38:43 +08:00
|
|
|
|
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_env_var(key: &str) -> String {
|
|
|
|
format!(
|
|
|
|
"MDBOOK_{}",
|
|
|
|
key.to_uppercase().replace('.', "__").replace("-", "_")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_config_using_env_var() {
|
|
|
|
let mut cfg = Config::default();
|
|
|
|
let key = "foo.bar";
|
|
|
|
let value = "baz";
|
|
|
|
|
|
|
|
assert!(cfg.get(key).is_none());
|
|
|
|
|
|
|
|
let encoded_key = encode_env_var(key);
|
|
|
|
env::set_var(encoded_key, value);
|
|
|
|
|
|
|
|
cfg.update_from_env();
|
|
|
|
|
2019-10-06 06:33:50 +08:00
|
|
|
assert_eq!(
|
|
|
|
cfg.get_deserialized_opt::<String, _>(key).unwrap().unwrap(),
|
|
|
|
value
|
|
|
|
);
|
2018-01-14 02:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-05-07 02:20:58 +08:00
|
|
|
#[allow(clippy::approx_constant)]
|
2018-01-14 02:38:43 +08:00
|
|
|
fn update_config_using_env_var_and_complex_value() {
|
|
|
|
let mut cfg = Config::default();
|
|
|
|
let key = "foo-bar.baz";
|
|
|
|
let value = json!({"array": [1, 2, 3], "number": 3.14});
|
|
|
|
let value_str = serde_json::to_string(&value).unwrap();
|
|
|
|
|
|
|
|
assert!(cfg.get(key).is_none());
|
|
|
|
|
|
|
|
let encoded_key = encode_env_var(key);
|
|
|
|
env::set_var(encoded_key, value_str);
|
|
|
|
|
|
|
|
cfg.update_from_env();
|
|
|
|
|
|
|
|
assert_eq!(
|
2019-10-06 06:33:50 +08:00
|
|
|
cfg.get_deserialized_opt::<serde_json::Value, _>(key)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap(),
|
2018-01-14 02:38:43 +08:00
|
|
|
value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_book_title_via_env() {
|
|
|
|
let mut cfg = Config::default();
|
|
|
|
let should_be = "Something else".to_string();
|
|
|
|
|
|
|
|
assert_ne!(cfg.book.title, Some(should_be.clone()));
|
|
|
|
|
|
|
|
env::set_var("MDBOOK_BOOK__TITLE", &should_be);
|
|
|
|
cfg.update_from_env();
|
|
|
|
|
|
|
|
assert_eq!(cfg.book.title, Some(should_be));
|
|
|
|
}
|
2020-05-13 20:45:35 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_404_default() {
|
|
|
|
let src = r#"
|
|
|
|
[output.html]
|
|
|
|
destination = "my-book"
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let got = Config::from_str(src).unwrap();
|
|
|
|
let html_config = got.html_config().unwrap();
|
|
|
|
assert_eq!(html_config.input_404, None);
|
2020-06-10 21:33:09 +08:00
|
|
|
assert_eq!(&get_404_output_file(&html_config.input_404), "404.html");
|
2020-05-13 20:45:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_404_custom() {
|
|
|
|
let src = r#"
|
|
|
|
[output.html]
|
|
|
|
input-404= "missing.md"
|
|
|
|
output-404= "missing.html"
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let got = Config::from_str(src).unwrap();
|
|
|
|
let html_config = got.html_config().unwrap();
|
|
|
|
assert_eq!(html_config.input_404, Some("missing.md".to_string()));
|
2020-06-10 21:33:09 +08:00
|
|
|
assert_eq!(&get_404_output_file(&html_config.input_404), "missing.html");
|
2020-05-13 20:45:35 +08:00
|
|
|
}
|
2017-09-30 20:34:27 +08:00
|
|
|
}
|