2016-12-06 06:55:17 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::path::Path;
|
2017-02-23 08:34:57 +00:00
|
|
|
use std::collections::HashMap;
|
2016-12-06 05:51:33 +00:00
|
|
|
|
2017-02-23 08:34:57 +00:00
|
|
|
use toml::{Value as Toml, self};
|
2016-12-06 05:51:33 +00:00
|
|
|
|
2017-02-23 08:34:57 +00:00
|
|
|
use errors::{Result, ResultExt};
|
2017-05-17 12:53:26 +00:00
|
|
|
use rendering::highlighting::THEME_SET;
|
2016-12-06 05:51:33 +00:00
|
|
|
|
2017-03-07 12:34:31 +00:00
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2016-12-06 05:51:33 +00:00
|
|
|
pub struct Config {
|
2017-02-23 08:34:57 +00:00
|
|
|
/// Title of the site
|
2016-12-06 05:51:33 +00:00
|
|
|
pub title: String,
|
2017-02-23 08:34:57 +00:00
|
|
|
/// Base URL of the site
|
2016-12-06 05:51:33 +00:00
|
|
|
pub base_url: String,
|
2017-03-10 11:39:58 +00:00
|
|
|
|
2017-03-10 08:28:17 +00:00
|
|
|
/// Whether to highlight all code blocks found in markdown files. Defaults to false
|
2017-03-03 08:12:40 +00:00
|
|
|
pub highlight_code: Option<bool>,
|
2017-03-20 08:30:50 +00:00
|
|
|
/// Which themes to use for code highlighting. See Readme for supported themes
|
|
|
|
pub highlight_theme: Option<String>,
|
2017-02-23 08:34:57 +00:00
|
|
|
/// Description of the site
|
|
|
|
pub description: Option<String>,
|
|
|
|
/// The language used in the site. Defaults to "en"
|
|
|
|
pub language_code: Option<String>,
|
2017-05-20 14:45:31 +00:00
|
|
|
/// Whether to generate RSS. Defaults to false
|
2017-03-12 03:59:28 +00:00
|
|
|
pub generate_rss: Option<bool>,
|
2017-05-20 14:45:31 +00:00
|
|
|
/// The number of articles to include in the RSS feed. Defaults to unlimited
|
|
|
|
pub rss_limit: Option<usize>,
|
2017-03-20 09:36:24 +00:00
|
|
|
/// Whether to generate tags and individual tag pages if some pages have them. Defaults to true
|
|
|
|
pub generate_tags_pages: Option<bool>,
|
|
|
|
/// Whether to generate categories and individual tag categories if some pages have them. Defaults to true
|
|
|
|
pub generate_categories_pages: Option<bool>,
|
2017-04-06 08:07:53 +00:00
|
|
|
/// Whether to insert a link for each header like in Github READMEs. Defaults to false
|
|
|
|
/// The default template can be overridden by creating a `anchor-link.html` template and CSS will need to be
|
|
|
|
/// written if you turn that on.
|
|
|
|
pub insert_anchor_links: Option<bool>,
|
2017-03-12 03:59:28 +00:00
|
|
|
|
2017-02-23 08:34:57 +00:00
|
|
|
/// All user params set in [extra] in the config
|
|
|
|
pub extra: Option<HashMap<String, Toml>>,
|
2016-12-06 06:55:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 09:36:24 +00:00
|
|
|
macro_rules! set_default {
|
|
|
|
($key: expr, $default: expr) => {
|
|
|
|
if $key.is_none() {
|
|
|
|
$key = Some($default);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 06:55:17 +00:00
|
|
|
impl Config {
|
2017-02-23 08:34:57 +00:00
|
|
|
/// Parses a string containing TOML to our Config struct
|
|
|
|
/// Any extra parameter will end up in the extra field
|
|
|
|
pub fn parse(content: &str) -> Result<Config> {
|
|
|
|
let mut config: Config = match toml::from_str(content) {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => bail!(e)
|
|
|
|
};
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2017-03-20 09:36:24 +00:00
|
|
|
set_default!(config.language_code, "en".to_string());
|
|
|
|
set_default!(config.highlight_code, false);
|
2017-05-12 13:32:35 +00:00
|
|
|
set_default!(config.generate_rss, false);
|
2017-05-20 14:45:31 +00:00
|
|
|
set_default!(config.rss_limit, <usize>::max_value());
|
2017-05-12 13:32:35 +00:00
|
|
|
set_default!(config.generate_tags_pages, false);
|
|
|
|
set_default!(config.generate_categories_pages, false);
|
|
|
|
set_default!(config.insert_anchor_links, false);
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2017-03-20 08:30:50 +00:00
|
|
|
match config.highlight_theme {
|
|
|
|
Some(ref t) => {
|
2017-05-17 12:53:26 +00:00
|
|
|
if !THEME_SET.themes.contains_key(t) {
|
2017-03-20 08:30:50 +00:00
|
|
|
bail!("Theme {} not available", t)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => config.highlight_theme = Some("base16-ocean-dark".to_string())
|
|
|
|
};
|
|
|
|
|
2017-02-23 08:34:57 +00:00
|
|
|
Ok(config)
|
2016-12-06 06:55:17 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 08:34:57 +00:00
|
|
|
/// Parses a config file from the given path
|
2016-12-06 06:55:17 +00:00
|
|
|
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config> {
|
|
|
|
let mut content = String::new();
|
2016-12-11 06:05:03 +00:00
|
|
|
File::open(path)
|
2017-02-23 08:34:57 +00:00
|
|
|
.chain_err(|| "No `config.toml` file found. Are you in the right directory?")?
|
2016-12-11 06:05:03 +00:00
|
|
|
.read_to_string(&mut content)?;
|
2016-12-06 05:51:33 +00:00
|
|
|
|
2017-02-23 08:34:57 +00:00
|
|
|
Config::parse(&content)
|
2016-12-06 06:55:17 +00:00
|
|
|
}
|
2017-03-20 03:42:43 +00:00
|
|
|
|
|
|
|
/// Makes a url, taking into account that the base url might have a trailing slash
|
|
|
|
pub fn make_permalink(&self, path: &str) -> String {
|
2017-06-10 17:52:39 +00:00
|
|
|
let trailing_bit = if path.ends_with('/') { "" } else { "/" };
|
|
|
|
|
|
|
|
// Index section with a base url that has a trailing slash
|
|
|
|
if self.base_url.ends_with('/') && path == "/" {
|
|
|
|
self.base_url.clone()
|
|
|
|
} else if path == "/" {
|
|
|
|
// index section with a base url that doesn't have a trailing slash
|
|
|
|
format!("{}/", self.base_url)
|
|
|
|
} else if self.base_url.ends_with('/') && path.starts_with('/') {
|
|
|
|
format!("{}{}{}", self.base_url, &path[1..], trailing_bit)
|
2017-05-15 07:56:16 +00:00
|
|
|
} else if self.base_url.ends_with('/') {
|
2017-06-10 17:52:39 +00:00
|
|
|
format!("{}{}{}", self.base_url, path, trailing_bit)
|
2017-03-20 03:42:43 +00:00
|
|
|
} else {
|
2017-06-10 17:52:39 +00:00
|
|
|
format!("{}/{}{}", self.base_url, path, trailing_bit)
|
2017-03-20 03:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-06 06:55:17 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 07:56:16 +00:00
|
|
|
/// Exists only for testing purposes
|
|
|
|
#[doc(hidden)]
|
2017-03-06 14:45:57 +00:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
|
|
|
title: "".to_string(),
|
|
|
|
base_url: "http://a-website.com/".to_string(),
|
|
|
|
highlight_code: Some(true),
|
2017-03-20 08:30:50 +00:00
|
|
|
highlight_theme: Some("base16-ocean-dark".to_string()),
|
2017-03-06 14:45:57 +00:00
|
|
|
description: None,
|
|
|
|
language_code: Some("en".to_string()),
|
2017-03-12 03:59:28 +00:00
|
|
|
generate_rss: Some(false),
|
2017-05-20 14:45:31 +00:00
|
|
|
rss_limit: Some(10000),
|
2017-03-20 09:36:24 +00:00
|
|
|
generate_tags_pages: Some(true),
|
|
|
|
generate_categories_pages: Some(true),
|
2017-04-06 08:07:53 +00:00
|
|
|
insert_anchor_links: Some(false),
|
2017-03-06 14:45:57 +00:00
|
|
|
extra: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
|
2017-03-03 08:12:40 +00:00
|
|
|
/// Get and parse the config.
|
|
|
|
/// If it doesn't succeed, exit
|
2017-03-25 04:18:15 +00:00
|
|
|
pub fn get_config(path: &Path, filename: &str) -> Config {
|
|
|
|
match Config::from_file(path.join(filename)) {
|
2017-03-03 08:12:40 +00:00
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => {
|
2017-03-25 04:18:15 +00:00
|
|
|
println!("Failed to load {}", filename);
|
2017-03-03 08:12:40 +00:00
|
|
|
println!("Error: {}", e);
|
|
|
|
::std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-06 06:55:17 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::{Config};
|
|
|
|
|
|
|
|
#[test]
|
2017-05-14 05:14:58 +00:00
|
|
|
fn can_import_valid_config() {
|
2016-12-06 06:55:17 +00:00
|
|
|
let config = r#"
|
|
|
|
title = "My site"
|
|
|
|
base_url = "https://replace-this-with-your-url.com"
|
|
|
|
"#;
|
|
|
|
|
2017-02-23 08:34:57 +00:00
|
|
|
let config = Config::parse(config).unwrap();
|
2016-12-06 06:55:17 +00:00
|
|
|
assert_eq!(config.title, "My site".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-05-14 05:14:58 +00:00
|
|
|
fn errors_when_invalid_type() {
|
2016-12-06 06:55:17 +00:00
|
|
|
let config = r#"
|
|
|
|
title = 1
|
|
|
|
base_url = "https://replace-this-with-your-url.com"
|
|
|
|
"#;
|
|
|
|
|
2017-02-23 08:34:57 +00:00
|
|
|
let config = Config::parse(config);
|
|
|
|
assert!(config.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-05-14 05:14:58 +00:00
|
|
|
fn errors_when_missing_required_field() {
|
2017-05-12 13:32:35 +00:00
|
|
|
// base_url is required
|
2017-02-23 08:34:57 +00:00
|
|
|
let config = r#"
|
|
|
|
title = ""
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let config = Config::parse(config);
|
2016-12-06 06:55:17 +00:00
|
|
|
assert!(config.is_err());
|
|
|
|
}
|
2017-02-23 08:34:57 +00:00
|
|
|
|
|
|
|
#[test]
|
2017-05-14 05:14:58 +00:00
|
|
|
fn can_add_extra_values() {
|
2017-02-23 08:34:57 +00:00
|
|
|
let config = r#"
|
|
|
|
title = "My site"
|
|
|
|
base_url = "https://replace-this-with-your-url.com"
|
|
|
|
|
|
|
|
[extra]
|
|
|
|
hello = "world"
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let config = Config::parse(config);
|
|
|
|
assert!(config.is_ok());
|
|
|
|
assert_eq!(config.unwrap().extra.unwrap().get("hello").unwrap().as_str().unwrap(), "world");
|
|
|
|
}
|
|
|
|
|
2017-05-15 07:56:16 +00:00
|
|
|
#[test]
|
|
|
|
fn can_make_url_with_non_trailing_slash_base_url() {
|
|
|
|
let mut config = Config::default();
|
|
|
|
config.base_url = "http://vincent.is".to_string();
|
2017-06-10 17:52:39 +00:00
|
|
|
assert_eq!(config.make_permalink("hello"), "http://vincent.is/hello/");
|
2017-05-15 07:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_make_url_with_trailing_slash_path() {
|
|
|
|
let mut config = Config::default();
|
|
|
|
config.base_url = "http://vincent.is/".to_string();
|
2017-06-10 17:52:39 +00:00
|
|
|
assert_eq!(config.make_permalink("/hello"), "http://vincent.is/hello/");
|
2017-05-15 07:56:16 +00:00
|
|
|
}
|
2016-12-06 06:55:17 +00:00
|
|
|
}
|