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.

lib.rs 14KB

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