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.

438 lines
14KB

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