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.

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