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.

469 lines
15KB

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