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.

503 lines
17KB

  1. /// A page, can be a blog post or a basic page
  2. use std::collections::HashMap;
  3. use std::path::{Path, PathBuf};
  4. use tera::{Tera, Context as TeraContext};
  5. use slug::slugify;
  6. use slotmap::{Key};
  7. use errors::{Result, ResultExt};
  8. use config::Config;
  9. use utils::fs::{read_file, find_related_assets};
  10. use utils::site::get_reading_analytics;
  11. use utils::templates::render_template;
  12. use front_matter::{PageFrontMatter, InsertAnchor, split_page_content};
  13. use rendering::{RenderContext, Header, render_content};
  14. use library::Library;
  15. use content::file_info::FileInfo;
  16. use content::ser::SerializingPage;
  17. #[derive(Clone, Debug, PartialEq)]
  18. pub struct Page {
  19. /// All info about the actual file
  20. pub file: FileInfo,
  21. /// The front matter meta-data
  22. pub meta: PageFrontMatter,
  23. /// The list of parent sections
  24. pub ancestors: Vec<Key>,
  25. /// The actual content of the page, in markdown
  26. pub raw_content: String,
  27. /// All the non-md files we found next to the .md file
  28. pub assets: Vec<PathBuf>,
  29. /// All the non-md files we found next to the .md file as string for use in templates
  30. pub serialized_assets: Vec<String>,
  31. /// The HTML rendered of the page
  32. pub content: String,
  33. /// The slug of that page.
  34. /// First tries to find the slug in the meta and defaults to filename otherwise
  35. pub slug: String,
  36. /// The URL path of the page
  37. pub path: String,
  38. /// The components of the path of the page
  39. pub components: Vec<String>,
  40. /// The full URL for that page
  41. pub permalink: String,
  42. /// The summary for the article, defaults to None
  43. /// When <!-- more --> is found in the text, will take the content up to that part
  44. /// as summary
  45. pub summary: Option<String>,
  46. /// The earlier page, for pages sorted by date
  47. pub earlier: Option<Key>,
  48. /// The later page, for pages sorted by date
  49. pub later: Option<Key>,
  50. /// The lighter page, for pages sorted by weight
  51. pub lighter: Option<Key>,
  52. /// The heavier page, for pages sorted by weight
  53. pub heavier: Option<Key>,
  54. /// Toc made from the headers of the markdown file
  55. pub toc: Vec<Header>,
  56. /// How many words in the raw content
  57. pub word_count: Option<usize>,
  58. /// How long would it take to read the raw content.
  59. /// See `get_reading_analytics` on how it is calculated
  60. pub reading_time: Option<usize>,
  61. }
  62. impl Page {
  63. pub fn new<P: AsRef<Path>>(file_path: P, meta: PageFrontMatter) -> Page {
  64. let file_path = file_path.as_ref();
  65. Page {
  66. file: FileInfo::new_page(file_path),
  67. meta,
  68. ancestors: vec![],
  69. raw_content: "".to_string(),
  70. assets: vec![],
  71. serialized_assets: vec![],
  72. content: "".to_string(),
  73. slug: "".to_string(),
  74. path: "".to_string(),
  75. components: vec![],
  76. permalink: "".to_string(),
  77. summary: None,
  78. earlier: None,
  79. later: None,
  80. lighter: None,
  81. heavier: None,
  82. toc: vec![],
  83. word_count: None,
  84. reading_time: None,
  85. }
  86. }
  87. pub fn is_draft(&self) -> bool {
  88. self.meta.draft
  89. }
  90. /// Parse a page given the content of the .md file
  91. /// Files without front matter or with invalid front matter are considered
  92. /// erroneous
  93. pub fn parse(file_path: &Path, content: &str, config: &Config) -> Result<Page> {
  94. let (meta, content) = split_page_content(file_path, content)?;
  95. let mut page = Page::new(file_path, meta);
  96. page.raw_content = content;
  97. let (word_count, reading_time) = get_reading_analytics(&page.raw_content);
  98. page.word_count = Some(word_count);
  99. page.reading_time = Some(reading_time);
  100. page.slug = {
  101. if let Some(ref slug) = page.meta.slug {
  102. slug.trim().to_string()
  103. } else if page.file.name == "index" {
  104. if let Some(parent) = page.file.path.parent() {
  105. slugify(parent.file_name().unwrap().to_str().unwrap())
  106. } else {
  107. slugify(page.file.name.clone())
  108. }
  109. } else {
  110. slugify(page.file.name.clone())
  111. }
  112. };
  113. if let Some(ref p) = page.meta.path {
  114. page.path = p.trim().trim_left_matches('/').to_string();
  115. } else {
  116. page.path = if page.file.components.is_empty() {
  117. page.slug.clone()
  118. } else {
  119. format!("{}/{}", page.file.components.join("/"), page.slug)
  120. };
  121. }
  122. if !page.path.ends_with('/') {
  123. page.path = format!("{}/", page.path);
  124. }
  125. page.components = page.path.split('/')
  126. .map(|p| p.to_string())
  127. .filter(|p| !p.is_empty())
  128. .collect::<Vec<_>>();
  129. page.permalink = config.make_permalink(&page.path);
  130. Ok(page)
  131. }
  132. /// Read and parse a .md file into a Page struct
  133. pub fn from_file<P: AsRef<Path>>(path: P, config: &Config) -> Result<Page> {
  134. let path = path.as_ref();
  135. let content = read_file(path)?;
  136. let mut page = Page::parse(path, &content, config)?;
  137. if page.file.name == "index" {
  138. let parent_dir = path.parent().unwrap();
  139. let assets = find_related_assets(parent_dir);
  140. if let Some(ref globset) = config.ignored_content_globset {
  141. // `find_related_assets` only scans the immediate directory (it is not recursive) so our
  142. // filtering only needs to work against the file_name component, not the full suffix. If
  143. // `find_related_assets` was changed to also return files in subdirectories, we could
  144. // use `PathBuf.strip_prefix` to remove the parent directory and then glob-filter
  145. // against the remaining path. Note that the current behaviour effectively means that
  146. // the `ignored_content` setting in the config file is limited to single-file glob
  147. // patterns (no "**" patterns).
  148. page.assets = assets.into_iter()
  149. .filter(|path|
  150. match path.file_name() {
  151. None => true,
  152. Some(file) => !globset.is_match(file)
  153. }
  154. ).collect();
  155. } else {
  156. page.assets = assets;
  157. }
  158. page.serialized_assets = page.serialize_assets();
  159. } else {
  160. page.assets = vec![];
  161. }
  162. Ok(page)
  163. }
  164. /// We need access to all pages url to render links relative to content
  165. /// so that can't happen at the same time as parsing
  166. pub fn render_markdown(
  167. &mut self,
  168. permalinks: &HashMap<String, String>,
  169. tera: &Tera,
  170. config: &Config,
  171. anchor_insert: InsertAnchor,
  172. ) -> Result<()> {
  173. let mut context = RenderContext::new(
  174. tera,
  175. config,
  176. &self.permalink,
  177. permalinks,
  178. anchor_insert,
  179. );
  180. context.tera_context.insert("page", &SerializingPage::from_page_basic(self, None));
  181. let res = render_content(&self.raw_content, &context)
  182. .chain_err(|| format!("Failed to render content of {}", self.file.path.display()))?;
  183. self.summary = res.summary_len.map(|l| res.body[0..l].to_owned());
  184. self.content = res.body;
  185. self.toc = res.toc;
  186. Ok(())
  187. }
  188. /// Renders the page using the default layout, unless specified in front-matter
  189. pub fn render_html(&self, tera: &Tera, config: &Config, library: &Library) -> Result<String> {
  190. let tpl_name = match self.meta.template {
  191. Some(ref l) => l.to_string(),
  192. None => "page.html".to_string()
  193. };
  194. let mut context = TeraContext::new();
  195. context.insert("config", config);
  196. context.insert("current_url", &self.permalink);
  197. context.insert("current_path", &self.path);
  198. context.insert("page", &self.to_serialized(library));
  199. render_template(&tpl_name, tera, &context, &config.theme)
  200. .chain_err(|| format!("Failed to render page '{}'", self.file.path.display()))
  201. }
  202. /// Creates a vectors of asset URLs.
  203. fn serialize_assets(&self) -> Vec<String> {
  204. self.assets.iter()
  205. .filter_map(|asset| asset.file_name())
  206. .filter_map(|filename| filename.to_str())
  207. .map(|filename| self.path.clone() + filename)
  208. .collect()
  209. }
  210. pub fn to_serialized<'a>(&'a self, library: &'a Library) -> SerializingPage<'a> {
  211. SerializingPage::from_page(self, library)
  212. }
  213. pub fn to_serialized_basic<'a>(&'a self, library: &'a Library) -> SerializingPage<'a> {
  214. SerializingPage::from_page_basic(self, Some(library))
  215. }
  216. }
  217. impl Default for Page {
  218. fn default() -> Page {
  219. Page {
  220. file: FileInfo::default(),
  221. meta: PageFrontMatter::default(),
  222. ancestors: vec![],
  223. raw_content: "".to_string(),
  224. assets: vec![],
  225. serialized_assets: vec![],
  226. content: "".to_string(),
  227. slug: "".to_string(),
  228. path: "".to_string(),
  229. components: vec![],
  230. permalink: "".to_string(),
  231. summary: None,
  232. earlier: None,
  233. later: None,
  234. lighter: None,
  235. heavier: None,
  236. toc: vec![],
  237. word_count: None,
  238. reading_time: None,
  239. }
  240. }
  241. }
  242. #[cfg(test)]
  243. mod tests {
  244. use std::collections::HashMap;
  245. use std::io::Write;
  246. use std::fs::{File, create_dir};
  247. use std::path::Path;
  248. use tera::Tera;
  249. use tempfile::tempdir;
  250. use globset::{Glob, GlobSetBuilder};
  251. use config::Config;
  252. use super::Page;
  253. use front_matter::InsertAnchor;
  254. #[test]
  255. fn test_can_parse_a_valid_page() {
  256. let content = r#"
  257. +++
  258. title = "Hello"
  259. description = "hey there"
  260. slug = "hello-world"
  261. +++
  262. Hello world"#;
  263. let res = Page::parse(Path::new("post.md"), content, &Config::default());
  264. assert!(res.is_ok());
  265. let mut page = res.unwrap();
  266. page.render_markdown(
  267. &HashMap::default(),
  268. &Tera::default(),
  269. &Config::default(),
  270. InsertAnchor::None,
  271. ).unwrap();
  272. assert_eq!(page.meta.title.unwrap(), "Hello".to_string());
  273. assert_eq!(page.meta.slug.unwrap(), "hello-world".to_string());
  274. assert_eq!(page.raw_content, "Hello world".to_string());
  275. assert_eq!(page.content, "<p>Hello world</p>\n".to_string());
  276. }
  277. #[test]
  278. fn test_can_make_url_from_sections_and_slug() {
  279. let content = r#"
  280. +++
  281. slug = "hello-world"
  282. +++
  283. Hello world"#;
  284. let mut conf = Config::default();
  285. conf.base_url = "http://hello.com/".to_string();
  286. let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &conf);
  287. assert!(res.is_ok());
  288. let page = res.unwrap();
  289. assert_eq!(page.path, "posts/intro/hello-world/");
  290. assert_eq!(page.components, vec!["posts", "intro", "hello-world"]);
  291. assert_eq!(page.permalink, "http://hello.com/posts/intro/hello-world/");
  292. }
  293. #[test]
  294. fn can_make_url_from_slug_only() {
  295. let content = r#"
  296. +++
  297. slug = "hello-world"
  298. +++
  299. Hello world"#;
  300. let config = Config::default();
  301. let res = Page::parse(Path::new("start.md"), content, &config);
  302. assert!(res.is_ok());
  303. let page = res.unwrap();
  304. assert_eq!(page.path, "hello-world/");
  305. assert_eq!(page.components, vec!["hello-world"]);
  306. assert_eq!(page.permalink, config.make_permalink("hello-world"));
  307. }
  308. #[test]
  309. fn can_make_url_from_path() {
  310. let content = r#"
  311. +++
  312. path = "hello-world"
  313. +++
  314. Hello world"#;
  315. let config = Config::default();
  316. let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &config);
  317. assert!(res.is_ok());
  318. let page = res.unwrap();
  319. assert_eq!(page.path, "hello-world/");
  320. assert_eq!(page.components, vec!["hello-world"]);
  321. assert_eq!(page.permalink, config.make_permalink("hello-world"));
  322. }
  323. #[test]
  324. fn can_make_url_from_path_starting_slash() {
  325. let content = r#"
  326. +++
  327. path = "/hello-world"
  328. +++
  329. Hello world"#;
  330. let config = Config::default();
  331. let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &config);
  332. assert!(res.is_ok());
  333. let page = res.unwrap();
  334. assert_eq!(page.path, "hello-world/");
  335. assert_eq!(page.permalink, config.make_permalink("hello-world"));
  336. }
  337. #[test]
  338. fn errors_on_invalid_front_matter_format() {
  339. // missing starting +++
  340. let content = r#"
  341. title = "Hello"
  342. description = "hey there"
  343. slug = "hello-world"
  344. +++
  345. Hello world"#;
  346. let res = Page::parse(Path::new("start.md"), content, &Config::default());
  347. assert!(res.is_err());
  348. }
  349. #[test]
  350. fn can_make_slug_from_non_slug_filename() {
  351. let config = Config::default();
  352. let res = Page::parse(Path::new(" file with space.md"), "+++\n+++", &config);
  353. assert!(res.is_ok());
  354. let page = res.unwrap();
  355. assert_eq!(page.slug, "file-with-space");
  356. assert_eq!(page.permalink, config.make_permalink(&page.slug));
  357. }
  358. #[test]
  359. fn can_specify_summary() {
  360. let config = Config::default();
  361. let content = r#"
  362. +++
  363. +++
  364. Hello world
  365. <!-- more -->"#.to_string();
  366. let res = Page::parse(Path::new("hello.md"), &content, &config);
  367. assert!(res.is_ok());
  368. let mut page = res.unwrap();
  369. page.render_markdown(
  370. &HashMap::default(),
  371. &Tera::default(),
  372. &config,
  373. InsertAnchor::None,
  374. ).unwrap();
  375. assert_eq!(page.summary, Some("<p>Hello world</p>\n".to_string()));
  376. }
  377. #[test]
  378. fn page_with_assets_gets_right_info() {
  379. let tmp_dir = tempdir().expect("create temp dir");
  380. let path = tmp_dir.path();
  381. create_dir(&path.join("content")).expect("create content temp dir");
  382. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  383. let nested_path = path.join("content").join("posts").join("with-assets");
  384. create_dir(&nested_path).expect("create nested temp dir");
  385. let mut f = File::create(nested_path.join("index.md")).unwrap();
  386. f.write_all(b"+++\n+++\n").unwrap();
  387. File::create(nested_path.join("example.js")).unwrap();
  388. File::create(nested_path.join("graph.jpg")).unwrap();
  389. File::create(nested_path.join("fail.png")).unwrap();
  390. let res = Page::from_file(
  391. nested_path.join("index.md").as_path(),
  392. &Config::default(),
  393. );
  394. assert!(res.is_ok());
  395. let page = res.unwrap();
  396. assert_eq!(page.file.parent, path.join("content").join("posts"));
  397. assert_eq!(page.slug, "with-assets");
  398. assert_eq!(page.assets.len(), 3);
  399. assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/");
  400. }
  401. #[test]
  402. fn page_with_assets_and_slug_overrides_path() {
  403. let tmp_dir = tempdir().expect("create temp dir");
  404. let path = tmp_dir.path();
  405. create_dir(&path.join("content")).expect("create content temp dir");
  406. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  407. let nested_path = path.join("content").join("posts").join("with-assets");
  408. create_dir(&nested_path).expect("create nested temp dir");
  409. let mut f = File::create(nested_path.join("index.md")).unwrap();
  410. f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
  411. File::create(nested_path.join("example.js")).unwrap();
  412. File::create(nested_path.join("graph.jpg")).unwrap();
  413. File::create(nested_path.join("fail.png")).unwrap();
  414. let res = Page::from_file(
  415. nested_path.join("index.md").as_path(),
  416. &Config::default(),
  417. );
  418. assert!(res.is_ok());
  419. let page = res.unwrap();
  420. assert_eq!(page.file.parent, path.join("content").join("posts"));
  421. assert_eq!(page.slug, "hey");
  422. assert_eq!(page.assets.len(), 3);
  423. assert_eq!(page.permalink, "http://a-website.com/posts/hey/");
  424. }
  425. #[test]
  426. fn page_with_ignored_assets_filters_out_correct_files() {
  427. let tmp_dir = tempdir().expect("create temp dir");
  428. let path = tmp_dir.path();
  429. create_dir(&path.join("content")).expect("create content temp dir");
  430. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  431. let nested_path = path.join("content").join("posts").join("with-assets");
  432. create_dir(&nested_path).expect("create nested temp dir");
  433. let mut f = File::create(nested_path.join("index.md")).unwrap();
  434. f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
  435. File::create(nested_path.join("example.js")).unwrap();
  436. File::create(nested_path.join("graph.jpg")).unwrap();
  437. File::create(nested_path.join("fail.png")).unwrap();
  438. let mut gsb = GlobSetBuilder::new();
  439. gsb.add(Glob::new("*.{js,png}").unwrap());
  440. let mut config = Config::default();
  441. config.ignored_content_globset = Some(gsb.build().unwrap());
  442. let res = Page::from_file(
  443. nested_path.join("index.md").as_path(),
  444. &config,
  445. );
  446. assert!(res.is_ok());
  447. let page = res.unwrap();
  448. assert_eq!(page.assets.len(), 1);
  449. assert_eq!(page.assets[0].file_name().unwrap().to_str(), Some("graph.jpg"));
  450. }
  451. }