You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.rs 14KB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. use std::collections::HashMap;
  2. use std::fs::File;
  3. use std::io::prelude::*;
  4. use std::path::{Path, PathBuf};
  5. use chrono::Utc;
  6. use globset::{Glob, GlobSet, GlobSetBuilder};
  7. use syntect::parsing::{SyntaxSet, SyntaxSetBuilder};
  8. use toml;
  9. use toml::Value as Toml;
  10. use errors::{Result, ResultExt};
  11. use highlighting::THEME_SET;
  12. use theme::Theme;
  13. // We want a default base url for tests
  14. static DEFAULT_BASE_URL: &'static str = "http://a-website.com";
  15. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
  16. #[serde(default)]
  17. pub struct Taxonomy {
  18. /// The name used in the URL, usually the plural
  19. pub name: String,
  20. /// If this is set, the list of individual taxonomy term page will be paginated
  21. /// by this much
  22. pub paginate_by: Option<usize>,
  23. pub paginate_path: Option<String>,
  24. /// Whether to generate a RSS feed only for each taxonomy term, defaults to false
  25. pub rss: bool,
  26. }
  27. impl Taxonomy {
  28. pub fn is_paginated(&self) -> bool {
  29. if let Some(paginate_by) = self.paginate_by {
  30. paginate_by > 0
  31. } else {
  32. false
  33. }
  34. }
  35. pub fn paginate_path(&self) -> &str {
  36. if let Some(ref path) = self.paginate_path {
  37. path
  38. } else {
  39. "page"
  40. }
  41. }
  42. }
  43. impl Default for Taxonomy {
  44. fn default() -> Taxonomy {
  45. Taxonomy { name: String::new(), paginate_by: None, paginate_path: None, rss: false }
  46. }
  47. }
  48. #[derive(Clone, Debug, Serialize, Deserialize)]
  49. #[serde(default)]
  50. pub struct Config {
  51. /// Base URL of the site, the only required config argument
  52. pub base_url: String,
  53. /// Theme to use
  54. pub theme: Option<String>,
  55. /// Title of the site. Defaults to None
  56. pub title: Option<String>,
  57. /// Description of the site
  58. pub description: Option<String>,
  59. /// The language used in the site. Defaults to "en"
  60. pub default_language: String,
  61. /// Languages list and translated strings
  62. pub translations: HashMap<String, Toml>,
  63. /// Whether to highlight all code blocks found in markdown files. Defaults to false
  64. pub highlight_code: bool,
  65. /// Which themes to use for code highlighting. See Readme for supported themes
  66. /// Defaults to "base16-ocean-dark"
  67. pub highlight_theme: String,
  68. /// Whether to generate RSS. Defaults to false
  69. pub generate_rss: bool,
  70. /// The number of articles to include in the RSS feed. Defaults to including all items.
  71. pub rss_limit: Option<usize>,
  72. pub taxonomies: Vec<Taxonomy>,
  73. /// Whether to compile the `sass` directory and output the css files into the static folder
  74. pub compile_sass: bool,
  75. /// Whether to build the search index for the content
  76. pub build_search_index: bool,
  77. /// A list of file glob patterns to ignore when processing the content folder. Defaults to none.
  78. /// Had to remove the PartialEq derive because GlobSet does not implement it. No impact
  79. /// because it's unused anyway (who wants to sort Configs?).
  80. pub ignored_content: Vec<String>,
  81. #[serde(skip_serializing, skip_deserializing)] // not a typo, 2 are needed
  82. pub ignored_content_globset: Option<GlobSet>,
  83. /// Whether to check all external links for validity
  84. pub check_external_links: bool,
  85. /// A list of directories to search for additional `.sublime-syntax` files in.
  86. pub extra_syntaxes: Vec<String>,
  87. /// The compiled extra syntaxes into a syntax set
  88. #[serde(skip_serializing, skip_deserializing)] // not a typo, 2 are need
  89. pub extra_syntax_set: Option<SyntaxSet>,
  90. /// All user params set in [extra] in the config
  91. pub extra: HashMap<String, Toml>,
  92. /// Set automatically when instantiating the config. Used for cachebusting
  93. pub build_timestamp: Option<i64>,
  94. }
  95. impl Config {
  96. /// Parses a string containing TOML to our Config struct
  97. /// Any extra parameter will end up in the extra field
  98. pub fn parse(content: &str) -> Result<Config> {
  99. let mut config: Config = match toml::from_str(content) {
  100. Ok(c) => c,
  101. Err(e) => bail!(e),
  102. };
  103. if config.base_url.is_empty() || config.base_url == DEFAULT_BASE_URL {
  104. bail!("A base URL is required in config.toml with key `base_url`");
  105. }
  106. if !THEME_SET.themes.contains_key(&config.highlight_theme) {
  107. bail!("Highlight theme {} not available", config.highlight_theme)
  108. }
  109. config.build_timestamp = Some(Utc::now().timestamp());
  110. if !config.ignored_content.is_empty() {
  111. // Convert the file glob strings into a compiled glob set matcher. We want to do this once,
  112. // at program initialization, rather than for every page, for example. We arrange for the
  113. // globset matcher to always exist (even though it has to be an inside an Option at the
  114. // moment because of the TOML serializer); if the glob set is empty the `is_match` function
  115. // of the globber always returns false.
  116. let mut glob_set_builder = GlobSetBuilder::new();
  117. for pat in &config.ignored_content {
  118. let glob = match Glob::new(pat) {
  119. Ok(g) => g,
  120. Err(e) => bail!("Invalid ignored_content glob pattern: {}, error = {}", pat, e),
  121. };
  122. glob_set_builder.add(glob);
  123. }
  124. config.ignored_content_globset =
  125. Some(glob_set_builder.build().expect("Bad ignored_content in config file."));
  126. }
  127. Ok(config)
  128. }
  129. /// Parses a config file from the given path
  130. pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config> {
  131. let mut content = String::new();
  132. let path = path.as_ref();
  133. let file_name = path.file_name().unwrap();
  134. File::open(path)
  135. .chain_err(|| {
  136. format!("No `{:?}` file found. Are you in the right directory?", file_name)
  137. })?
  138. .read_to_string(&mut content)?;
  139. Config::parse(&content)
  140. }
  141. /// Attempt to load any extra syntax found in the extra syntaxes of the config
  142. pub fn load_extra_syntaxes(&mut self, base_path: &Path) -> Result<()> {
  143. if self.extra_syntaxes.is_empty() {
  144. return Ok(());
  145. }
  146. let mut ss = SyntaxSetBuilder::new();
  147. for dir in &self.extra_syntaxes {
  148. ss.add_from_folder(base_path.join(dir), true)?;
  149. }
  150. self.extra_syntax_set = Some(ss.build());
  151. Ok(())
  152. }
  153. /// Makes a url, taking into account that the base url might have a trailing slash
  154. pub fn make_permalink(&self, path: &str) -> String {
  155. let trailing_bit = if path.ends_with('/') || path.ends_with("rss.xml") || path.is_empty() {
  156. ""
  157. } else {
  158. "/"
  159. };
  160. // Index section with a base url that has a trailing slash
  161. if self.base_url.ends_with('/') && path == "/" {
  162. self.base_url.clone()
  163. } else if path == "/" {
  164. // index section with a base url that doesn't have a trailing slash
  165. format!("{}/", self.base_url)
  166. } else if self.base_url.ends_with('/') && path.starts_with('/') {
  167. format!("{}{}{}", self.base_url, &path[1..], trailing_bit)
  168. } else if self.base_url.ends_with('/') || path.starts_with('/') {
  169. format!("{}{}{}", self.base_url, path, trailing_bit)
  170. } else {
  171. format!("{}/{}{}", self.base_url, path, trailing_bit)
  172. }
  173. }
  174. /// Merges the extra data from the theme with the config extra data
  175. fn add_theme_extra(&mut self, theme: &Theme) -> Result<()> {
  176. // 3 pass merging
  177. // 1. save config to preserve user
  178. let original = self.extra.clone();
  179. // 2. inject theme extra values
  180. for (key, val) in &theme.extra {
  181. self.extra.entry(key.to_string()).or_insert_with(|| val.clone());
  182. }
  183. // 3. overwrite with original config
  184. for (key, val) in &original {
  185. self.extra.entry(key.to_string()).or_insert_with(|| val.clone());
  186. }
  187. Ok(())
  188. }
  189. /// Parse the theme.toml file and merges the extra data from the theme
  190. /// with the config extra data
  191. pub fn merge_with_theme(&mut self, path: &PathBuf) -> Result<()> {
  192. let theme = Theme::from_file(path)?;
  193. self.add_theme_extra(&theme)
  194. }
  195. }
  196. impl Default for Config {
  197. fn default() -> Config {
  198. Config {
  199. base_url: DEFAULT_BASE_URL.to_string(),
  200. title: None,
  201. description: None,
  202. theme: None,
  203. highlight_code: false,
  204. highlight_theme: "base16-ocean-dark".to_string(),
  205. default_language: "en".to_string(),
  206. generate_rss: false,
  207. rss_limit: None,
  208. taxonomies: Vec::new(),
  209. compile_sass: false,
  210. check_external_links: false,
  211. build_search_index: false,
  212. ignored_content: Vec::new(),
  213. ignored_content_globset: None,
  214. translations: HashMap::new(),
  215. extra_syntaxes: Vec::new(),
  216. extra_syntax_set: None,
  217. extra: HashMap::new(),
  218. build_timestamp: Some(1),
  219. }
  220. }
  221. }
  222. #[cfg(test)]
  223. mod tests {
  224. use super::{Config, Theme};
  225. #[test]
  226. fn can_import_valid_config() {
  227. let config = r#"
  228. title = "My site"
  229. base_url = "https://replace-this-with-your-url.com"
  230. "#;
  231. let config = Config::parse(config).unwrap();
  232. assert_eq!(config.title.unwrap(), "My site".to_string());
  233. }
  234. #[test]
  235. fn errors_when_invalid_type() {
  236. let config = r#"
  237. title = 1
  238. base_url = "https://replace-this-with-your-url.com"
  239. "#;
  240. let config = Config::parse(config);
  241. assert!(config.is_err());
  242. }
  243. #[test]
  244. fn errors_when_missing_required_field() {
  245. // base_url is required
  246. let config = r#"
  247. title = ""
  248. "#;
  249. let config = Config::parse(config);
  250. assert!(config.is_err());
  251. }
  252. #[test]
  253. fn can_add_extra_values() {
  254. let config = r#"
  255. title = "My site"
  256. base_url = "https://replace-this-with-your-url.com"
  257. [extra]
  258. hello = "world"
  259. "#;
  260. let config = Config::parse(config);
  261. assert!(config.is_ok());
  262. assert_eq!(config.unwrap().extra.get("hello").unwrap().as_str().unwrap(), "world");
  263. }
  264. #[test]
  265. fn can_make_url_index_page_with_non_trailing_slash_url() {
  266. let mut config = Config::default();
  267. config.base_url = "http://vincent.is".to_string();
  268. assert_eq!(config.make_permalink(""), "http://vincent.is/");
  269. }
  270. #[test]
  271. fn can_make_url_index_page_with_railing_slash_url() {
  272. let mut config = Config::default();
  273. config.base_url = "http://vincent.is/".to_string();
  274. assert_eq!(config.make_permalink(""), "http://vincent.is/");
  275. }
  276. #[test]
  277. fn can_make_url_with_non_trailing_slash_base_url() {
  278. let mut config = Config::default();
  279. config.base_url = "http://vincent.is".to_string();
  280. assert_eq!(config.make_permalink("hello"), "http://vincent.is/hello/");
  281. }
  282. #[test]
  283. fn can_make_url_with_trailing_slash_path() {
  284. let mut config = Config::default();
  285. config.base_url = "http://vincent.is/".to_string();
  286. assert_eq!(config.make_permalink("/hello"), "http://vincent.is/hello/");
  287. }
  288. #[test]
  289. fn can_make_url_with_localhost() {
  290. let mut config = Config::default();
  291. config.base_url = "http://127.0.0.1:1111".to_string();
  292. assert_eq!(config.make_permalink("/tags/rust"), "http://127.0.0.1:1111/tags/rust/");
  293. }
  294. // https://github.com/Keats/gutenberg/issues/486
  295. #[test]
  296. fn doesnt_add_trailing_slash_to_rss() {
  297. let mut config = Config::default();
  298. config.base_url = "http://vincent.is/".to_string();
  299. assert_eq!(config.make_permalink("rss.xml"), "http://vincent.is/rss.xml");
  300. }
  301. #[test]
  302. fn can_merge_with_theme_data_and_preserve_config_value() {
  303. let config_str = r#"
  304. title = "My site"
  305. base_url = "https://replace-this-with-your-url.com"
  306. [extra]
  307. hello = "world"
  308. "#;
  309. let mut config = Config::parse(config_str).unwrap();
  310. let theme_str = r#"
  311. [extra]
  312. hello = "foo"
  313. a_value = 10
  314. "#;
  315. let theme = Theme::parse(theme_str).unwrap();
  316. assert!(config.add_theme_extra(&theme).is_ok());
  317. let extra = config.extra;
  318. assert_eq!(extra["hello"].as_str().unwrap(), "world".to_string());
  319. assert_eq!(extra["a_value"].as_integer().unwrap(), 10);
  320. }
  321. #[test]
  322. fn can_use_language_configuration() {
  323. let config = r#"
  324. base_url = "https://remplace-par-ton-url.fr"
  325. default_language = "fr"
  326. [translations]
  327. [translations.fr]
  328. title = "Un titre"
  329. [translations.en]
  330. title = "A title"
  331. "#;
  332. let config = Config::parse(config);
  333. assert!(config.is_ok());
  334. let translations = config.unwrap().translations;
  335. assert_eq!(translations["fr"]["title"].as_str().unwrap(), "Un titre");
  336. assert_eq!(translations["en"]["title"].as_str().unwrap(), "A title");
  337. }
  338. #[test]
  339. fn missing_ignored_content_results_in_empty_vector_and_empty_globset() {
  340. let config_str = r#"
  341. title = "My site"
  342. base_url = "example.com"
  343. "#;
  344. let config = Config::parse(config_str).unwrap();
  345. let v = config.ignored_content;
  346. assert_eq!(v.len(), 0);
  347. assert!(config.ignored_content_globset.is_none());
  348. }
  349. #[test]
  350. fn empty_ignored_content_results_in_empty_vector_and_empty_globset() {
  351. let config_str = r#"
  352. title = "My site"
  353. base_url = "example.com"
  354. ignored_content = []
  355. "#;
  356. let config = Config::parse(config_str).unwrap();
  357. assert_eq!(config.ignored_content.len(), 0);
  358. assert!(config.ignored_content_globset.is_none());
  359. }
  360. #[test]
  361. fn non_empty_ignored_content_results_in_vector_of_patterns_and_configured_globset() {
  362. let config_str = r#"
  363. title = "My site"
  364. base_url = "example.com"
  365. ignored_content = ["*.{graphml,iso}", "*.py?"]
  366. "#;
  367. let config = Config::parse(config_str).unwrap();
  368. let v = config.ignored_content;
  369. assert_eq!(v, vec!["*.{graphml,iso}", "*.py?"]);
  370. let g = config.ignored_content_globset.unwrap();
  371. assert_eq!(g.len(), 2);
  372. assert!(g.is_match("foo.graphml"));
  373. assert!(g.is_match("foo.iso"));
  374. assert!(!g.is_match("foo.png"));
  375. assert!(g.is_match("foo.py2"));
  376. assert!(g.is_match("foo.py3"));
  377. assert!(!g.is_match("foo.py"));
  378. }
  379. }