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.

355 lines
13KB

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