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.

478 lines
15KB

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