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.

367 lines
13KB

  1. extern crate site;
  2. #[macro_use]
  3. extern crate errors;
  4. extern crate front_matter;
  5. extern crate library;
  6. use std::path::{Component, Path};
  7. use errors::Result;
  8. use front_matter::{PageFrontMatter, SectionFrontMatter};
  9. use library::{Page, Section};
  10. use site::Site;
  11. #[derive(Debug, Clone, Copy, PartialEq)]
  12. pub enum PageChangesNeeded {
  13. /// Editing `taxonomies`
  14. Taxonomies,
  15. /// Editing `date`, `order` or `weight`
  16. Sort,
  17. /// Editing anything causes a re-render of the page
  18. Render,
  19. }
  20. #[derive(Debug, Clone, Copy, PartialEq)]
  21. pub enum SectionChangesNeeded {
  22. /// Editing `sort_by`
  23. Sort,
  24. /// Editing `title`, `description`, `extra`, `template` or setting `render` to true
  25. Render,
  26. /// Editing `paginate_by`, `paginate_path` or `insert_anchor_links`
  27. RenderWithPages,
  28. /// Setting `render` to false
  29. Delete,
  30. }
  31. /// Evaluates all the params in the front matter that changed so we can do the smallest
  32. /// delta in the serve command
  33. /// Order matters as the actions will be done in insertion order
  34. fn find_section_front_matter_changes(
  35. current: &SectionFrontMatter,
  36. new: &SectionFrontMatter,
  37. ) -> Vec<SectionChangesNeeded> {
  38. let mut changes_needed = vec![];
  39. if current.sort_by != new.sort_by {
  40. changes_needed.push(SectionChangesNeeded::Sort);
  41. }
  42. // We want to hide the section
  43. // TODO: what to do on redirect_path change?
  44. if current.render && !new.render {
  45. changes_needed.push(SectionChangesNeeded::Delete);
  46. // Nothing else we can do
  47. return changes_needed;
  48. }
  49. if current.paginate_by != new.paginate_by
  50. || current.paginate_path != new.paginate_path
  51. || current.insert_anchor_links != new.insert_anchor_links
  52. {
  53. changes_needed.push(SectionChangesNeeded::RenderWithPages);
  54. // Nothing else we can do
  55. return changes_needed;
  56. }
  57. // Any new change will trigger a re-rendering of the section page only
  58. changes_needed.push(SectionChangesNeeded::Render);
  59. changes_needed
  60. }
  61. /// Evaluates all the params in the front matter that changed so we can do the smallest
  62. /// delta in the serve command
  63. /// Order matters as the actions will be done in insertion order
  64. fn find_page_front_matter_changes(
  65. current: &PageFrontMatter,
  66. other: &PageFrontMatter,
  67. ) -> Vec<PageChangesNeeded> {
  68. let mut changes_needed = vec![];
  69. if current.taxonomies != other.taxonomies {
  70. changes_needed.push(PageChangesNeeded::Taxonomies);
  71. }
  72. if current.date != other.date || current.order != other.order || current.weight != other.weight
  73. {
  74. changes_needed.push(PageChangesNeeded::Sort);
  75. }
  76. changes_needed.push(PageChangesNeeded::Render);
  77. changes_needed
  78. }
  79. /// Handles a path deletion: could be a page, a section, a folder
  80. fn delete_element(site: &mut Site, path: &Path, is_section: bool) -> Result<()> {
  81. // Ignore the event if this path was not known
  82. if !site.library.contains_section(&path.to_path_buf())
  83. && !site.library.contains_page(&path.to_path_buf())
  84. {
  85. return Ok(());
  86. }
  87. if is_section {
  88. if let Some(s) = site.library.remove_section(&path.to_path_buf()) {
  89. site.permalinks.remove(&s.file.relative);
  90. }
  91. } else if let Some(p) = site.library.remove_page(&path.to_path_buf()) {
  92. site.permalinks.remove(&p.file.relative);
  93. if !p.meta.taxonomies.is_empty() {
  94. site.populate_taxonomies()?;
  95. }
  96. }
  97. site.populate_sections();
  98. // Ensure we have our fn updated so it doesn't contain the permalink(s)/section/page deleted
  99. site.register_early_global_fns();
  100. site.register_tera_global_fns();
  101. // Deletion is something that doesn't happen all the time so we
  102. // don't need to optimise it too much
  103. site.build()
  104. }
  105. /// Handles a `_index.md` (a section) being edited in some ways
  106. fn handle_section_editing(site: &mut Site, path: &Path) -> Result<()> {
  107. let section = Section::from_file(path, &site.config)?;
  108. let pathbuf = path.to_path_buf();
  109. match site.add_section(section, true)? {
  110. // Updating a section
  111. Some(prev) => {
  112. site.populate_sections();
  113. if site.library.get_section(&pathbuf).unwrap().meta == prev.meta {
  114. // Front matter didn't change, only content did
  115. // so we render only the section page, not its pages
  116. return site.render_section(&site.library.get_section(&pathbuf).unwrap(), false);
  117. }
  118. // Front matter changed
  119. for changes in find_section_front_matter_changes(
  120. &site.library.get_section(&pathbuf).unwrap().meta,
  121. &prev.meta,
  122. ) {
  123. // Sort always comes first if present so the rendering will be fine
  124. match changes {
  125. SectionChangesNeeded::Sort => {
  126. site.register_tera_global_fns();
  127. }
  128. SectionChangesNeeded::Render => {
  129. site.render_section(&site.library.get_section(&pathbuf).unwrap(), false)?
  130. }
  131. SectionChangesNeeded::RenderWithPages => {
  132. site.render_section(&site.library.get_section(&pathbuf).unwrap(), true)?
  133. }
  134. // not a common enough operation to make it worth optimizing
  135. SectionChangesNeeded::Delete => {
  136. site.build()?;
  137. }
  138. };
  139. }
  140. Ok(())
  141. }
  142. // New section, only render that one
  143. None => {
  144. site.populate_sections();
  145. site.register_tera_global_fns();
  146. site.render_section(&site.library.get_section(&pathbuf).unwrap(), true)
  147. }
  148. }
  149. }
  150. macro_rules! render_parent_section {
  151. ($site: expr, $path: expr) => {
  152. if let Some(s) = $site.library.find_parent_section($path) {
  153. $site.render_section(s, false)?;
  154. };
  155. };
  156. }
  157. /// Handles a page being edited in some ways
  158. fn handle_page_editing(site: &mut Site, path: &Path) -> Result<()> {
  159. let page = Page::from_file(path, &site.config)?;
  160. let pathbuf = path.to_path_buf();
  161. match site.add_page(page, true)? {
  162. // Updating a page
  163. Some(prev) => {
  164. site.populate_sections();
  165. // Front matter didn't change, only content did
  166. if site.library.get_page(&pathbuf).unwrap().meta == prev.meta {
  167. // Other than the page itself, the summary might be seen
  168. // on a paginated list for a blog for example
  169. if site.library.get_page(&pathbuf).unwrap().summary.is_some() {
  170. render_parent_section!(site, path);
  171. }
  172. site.register_tera_global_fns();
  173. return site.render_page(&site.library.get_page(&pathbuf).unwrap());
  174. }
  175. // Front matter changed
  176. for changes in find_page_front_matter_changes(
  177. &site.library.get_page(&pathbuf).unwrap().meta,
  178. &prev.meta,
  179. ) {
  180. site.register_tera_global_fns();
  181. // Sort always comes first if present so the rendering will be fine
  182. match changes {
  183. PageChangesNeeded::Taxonomies => {
  184. site.populate_taxonomies()?;
  185. site.render_taxonomies()?;
  186. }
  187. PageChangesNeeded::Sort => {
  188. site.render_index()?;
  189. }
  190. PageChangesNeeded::Render => {
  191. render_parent_section!(site, path);
  192. site.render_page(&site.library.get_page(&path.to_path_buf()).unwrap())?;
  193. }
  194. };
  195. }
  196. Ok(())
  197. }
  198. // It's a new page!
  199. None => {
  200. site.populate_sections();
  201. site.populate_taxonomies()?;
  202. site.register_early_global_fns();
  203. site.register_tera_global_fns();
  204. // No need to optimise that yet, we can revisit if it becomes an issue
  205. site.build()
  206. }
  207. }
  208. }
  209. /// What happens when a section or a page is changed
  210. pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> {
  211. let is_section = path.file_name().unwrap() == "_index.md";
  212. let is_md = path.extension().unwrap() == "md";
  213. let index = path.parent().unwrap().join("index.md");
  214. // A few situations can happen:
  215. // 1. Change on .md files
  216. // a. Is there an `index.md`? Return an error if it's something other than delete
  217. // b. Deleted? remove the element
  218. // c. Edited?
  219. // 1. filename is `_index.md`, this is a section
  220. // 1. it's a page otherwise
  221. // 2. Change on non .md files
  222. // a. Try to find a corresponding `_index.md`
  223. // 1. Nothing? Return Ok
  224. // 2. Something? Update the page
  225. if is_md {
  226. // only delete if it was able to be added in the first place
  227. if !index.exists() && !path.exists() {
  228. delete_element(site, path, is_section)?;
  229. }
  230. // Added another .md in a assets directory
  231. if index.exists() && path.exists() && path != index {
  232. bail!(
  233. "Change on {:?} detected but there is already an `index.md` in the same folder",
  234. path.display()
  235. );
  236. } else if index.exists() && !path.exists() {
  237. // deleted the wrong .md, do nothing
  238. return Ok(());
  239. }
  240. if is_section {
  241. handle_section_editing(site, path)
  242. } else {
  243. handle_page_editing(site, path)
  244. }
  245. } else if index.exists() {
  246. handle_page_editing(site, &index)
  247. } else {
  248. Ok(())
  249. }
  250. }
  251. /// What happens when a template is changed
  252. pub fn after_template_change(site: &mut Site, path: &Path) -> Result<()> {
  253. site.tera.full_reload()?;
  254. let filename = path.file_name().unwrap().to_str().unwrap();
  255. match filename {
  256. "sitemap.xml" => site.render_sitemap(),
  257. "rss.xml" => site.render_rss_feed(site.library.pages_values(), None),
  258. "robots.txt" => site.render_robots(),
  259. "single.html" | "list.html" => site.render_taxonomies(),
  260. "page.html" => {
  261. site.render_sections()?;
  262. site.render_orphan_pages()
  263. }
  264. "section.html" => site.render_sections(),
  265. // Either the index or some unknown template changed
  266. // We can't really know what this change affects so rebuild all
  267. // the things
  268. _ => {
  269. // If we are updating a shortcode, re-render the markdown of all pages/site
  270. // because we have no clue which one needs rebuilding
  271. // TODO: look if there the shortcode is used in the markdown instead of re-rendering
  272. // everything
  273. if path.components().any(|x| x == Component::Normal("shortcodes".as_ref())) {
  274. site.render_markdown()?;
  275. }
  276. site.populate_sections();
  277. site.render_sections()?;
  278. site.render_orphan_pages()?;
  279. site.render_taxonomies()
  280. }
  281. }
  282. }
  283. #[cfg(test)]
  284. mod tests {
  285. use std::collections::HashMap;
  286. use super::{
  287. find_page_front_matter_changes, find_section_front_matter_changes, PageChangesNeeded,
  288. SectionChangesNeeded,
  289. };
  290. use front_matter::{PageFrontMatter, SectionFrontMatter, SortBy};
  291. #[test]
  292. fn can_find_taxonomy_changes_in_page_frontmatter() {
  293. let mut taxonomies = HashMap::new();
  294. taxonomies.insert("tags".to_string(), vec!["a tag".to_string()]);
  295. let new = PageFrontMatter { taxonomies, ..PageFrontMatter::default() };
  296. let changes = find_page_front_matter_changes(&PageFrontMatter::default(), &new);
  297. assert_eq!(changes, vec![PageChangesNeeded::Taxonomies, PageChangesNeeded::Render]);
  298. }
  299. #[test]
  300. fn can_find_multiple_changes_in_page_frontmatter() {
  301. let mut taxonomies = HashMap::new();
  302. taxonomies.insert("categories".to_string(), vec!["a category".to_string()]);
  303. let current = PageFrontMatter { taxonomies, order: Some(1), ..PageFrontMatter::default() };
  304. let changes = find_page_front_matter_changes(&current, &PageFrontMatter::default());
  305. assert_eq!(
  306. changes,
  307. vec![PageChangesNeeded::Taxonomies, PageChangesNeeded::Sort, PageChangesNeeded::Render]
  308. );
  309. }
  310. #[test]
  311. fn can_find_sort_changes_in_section_frontmatter() {
  312. let new = SectionFrontMatter { sort_by: SortBy::Date, ..SectionFrontMatter::default() };
  313. let changes = find_section_front_matter_changes(&SectionFrontMatter::default(), &new);
  314. assert_eq!(changes, vec![SectionChangesNeeded::Sort, SectionChangesNeeded::Render]);
  315. }
  316. #[test]
  317. fn can_find_render_changes_in_section_frontmatter() {
  318. let new = SectionFrontMatter { render: false, ..SectionFrontMatter::default() };
  319. let changes = find_section_front_matter_changes(&SectionFrontMatter::default(), &new);
  320. assert_eq!(changes, vec![SectionChangesNeeded::Delete]);
  321. }
  322. #[test]
  323. fn can_find_paginate_by_changes_in_section_frontmatter() {
  324. let new = SectionFrontMatter { paginate_by: Some(10), ..SectionFrontMatter::default() };
  325. let changes = find_section_front_matter_changes(&SectionFrontMatter::default(), &new);
  326. assert_eq!(changes, vec![SectionChangesNeeded::RenderWithPages]);
  327. }
  328. }