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.

241 lines
8.7KB

  1. use std::path::Path;
  2. use gutenberg::{Site, Page, Section, SectionFrontMatter, PageFrontMatter};
  3. use gutenberg::errors::Result;
  4. /// Finds the section that contains the page given if there is one
  5. pub fn find_parent_section<'a>(site: &'a Site, page: &Page) -> Option<&'a Section> {
  6. for section in site.sections.values() {
  7. if section.is_child_page(page) {
  8. return Some(section)
  9. }
  10. }
  11. None
  12. }
  13. #[derive(Debug, Clone, Copy, PartialEq)]
  14. enum PageChangesNeeded {
  15. /// Editing `tags`
  16. Tags,
  17. /// Editing `categories`
  18. Categories,
  19. /// Editing `date` or `order`
  20. Sort,
  21. /// Editing anything else
  22. Render,
  23. }
  24. // TODO: seems like editing sort_by/render do weird stuff
  25. #[derive(Debug, Clone, Copy, PartialEq)]
  26. enum SectionChangesNeeded {
  27. /// Editing `sort_by`
  28. Sort,
  29. /// Editing `title`, `description`, `extra`, `template` or setting `render` to true
  30. Render,
  31. /// Editing `paginate_by` or `paginate_path`
  32. RenderWithPages,
  33. /// Setting `render` to false
  34. Delete,
  35. }
  36. /// Evaluates all the params in the front matter that changed so we can do the smallest
  37. /// delta in the serve command
  38. fn find_section_front_matter_changes(current: &SectionFrontMatter, other: &SectionFrontMatter) -> Vec<SectionChangesNeeded> {
  39. let mut changes_needed = vec![];
  40. if current.sort_by != other.sort_by {
  41. changes_needed.push(SectionChangesNeeded::Sort);
  42. }
  43. if !current.should_render() && other.should_render() {
  44. changes_needed.push(SectionChangesNeeded::Delete);
  45. // Nothing else we can do
  46. return changes_needed;
  47. }
  48. if current.paginate_by != other.paginate_by || current.paginate_path != other.paginate_path {
  49. changes_needed.push(SectionChangesNeeded::RenderWithPages);
  50. // Nothing else we can do
  51. return changes_needed;
  52. }
  53. // Any other 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. fn find_page_front_matter_changes(current: &PageFrontMatter, other: &PageFrontMatter) -> Vec<PageChangesNeeded> {
  60. let mut changes_needed = vec![];
  61. if current.tags != other.tags {
  62. changes_needed.push(PageChangesNeeded::Tags);
  63. }
  64. if current.category != other.category {
  65. changes_needed.push(PageChangesNeeded::Categories);
  66. }
  67. if current.date != other.date || current.order != other.order {
  68. changes_needed.push(PageChangesNeeded::Sort);
  69. }
  70. changes_needed.push(PageChangesNeeded::Render);
  71. changes_needed
  72. }
  73. // What happens when a section or a page is changed
  74. pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> {
  75. let is_section = path.file_name().unwrap() == "_index.md";
  76. // A page or section got deleted
  77. if !path.exists() {
  78. if is_section {
  79. // A section was deleted, many things can be impacted:
  80. // - the pages of the section are becoming orphans
  81. // - any page that was referencing the section (index, etc)
  82. let relative_path = site.sections[path].file.relative.clone();
  83. // Remove the link to it and the section itself from the Site
  84. site.permalinks.remove(&relative_path);
  85. site.sections.remove(path);
  86. site.populate_sections();
  87. } else {
  88. // A page was deleted, many things can be impacted:
  89. // - the section the page is in
  90. // - any page that was referencing the section (index, etc)
  91. let relative_path = site.pages[path].file.relative.clone();
  92. site.permalinks.remove(&relative_path);
  93. if let Some(p) = site.pages.remove(path) {
  94. if p.meta.has_tags() || p.meta.category.is_some() {
  95. site.populate_tags_and_categories();
  96. }
  97. if find_parent_section(site, &p).is_some() {
  98. site.populate_sections();
  99. }
  100. };
  101. }
  102. // Deletion is something that doesn't happen all the time so we
  103. // don't need to optimise it too much
  104. return site.build();
  105. }
  106. // A section was edited
  107. if is_section {
  108. match site.add_section(path, true)? {
  109. Some(prev) => {
  110. // Updating a section
  111. let current_meta = site.sections[path].meta.clone();
  112. // Front matter didn't change, only content did
  113. // so we render only the section page, not its pages
  114. if current_meta == prev.meta {
  115. return site.render_section(&site.sections[path], false);
  116. }
  117. // Front matter changed
  118. for changes in find_section_front_matter_changes(&current_meta, &prev.meta) {
  119. // Sort always comes first if present so the rendering will be fine
  120. match changes {
  121. SectionChangesNeeded::Sort => site.sort_sections_pages(Some(path)),
  122. SectionChangesNeeded::Render => site.render_section(&site.sections[path], false)?,
  123. SectionChangesNeeded::RenderWithPages => site.render_section(&site.sections[path], true)?,
  124. // can't be arsed to make the Delete efficient, it's not a common enough operation
  125. SectionChangesNeeded::Delete => {
  126. site.populate_sections();
  127. site.build()?;
  128. },
  129. };
  130. }
  131. return Ok(());
  132. },
  133. None => {
  134. // New section, only render that one
  135. site.populate_sections();
  136. return site.render_section(&site.sections[path], true);
  137. }
  138. };
  139. }
  140. // A page was edited
  141. match site.add_page(path, true)? {
  142. Some(prev) => {
  143. // Updating a page
  144. let current = site.pages[path].clone();
  145. // Front matter didn't change, only content did
  146. // so we render only the section page, not its pages
  147. if current.meta == prev.meta {
  148. return site.render_page(&site.pages[path]);
  149. }
  150. // Front matter changed
  151. for changes in find_page_front_matter_changes(&current.meta, &prev.meta) {
  152. // Sort always comes first if present so the rendering will be fine
  153. match changes {
  154. PageChangesNeeded::Tags => {
  155. site.populate_tags_and_categories();
  156. site.render_tags()?;
  157. },
  158. PageChangesNeeded::Categories => {
  159. site.populate_tags_and_categories();
  160. site.render_categories()?;
  161. },
  162. PageChangesNeeded::Sort => {
  163. let section_path = match find_parent_section(site, &site.pages[path]) {
  164. Some(s) => s.file.path.clone(),
  165. None => continue // Do nothing if it's an orphan page
  166. };
  167. site.populate_sections();
  168. site.sort_sections_pages(Some(&section_path));
  169. site.render_index()?;
  170. },
  171. PageChangesNeeded::Render => {
  172. site.render_page(&site.pages[path])?;
  173. },
  174. };
  175. }
  176. return Ok(());
  177. },
  178. None => {
  179. // It's a new page!
  180. site.populate_sections();
  181. site.populate_tags_and_categories();
  182. // No need to optimise that yet, we can revisit if it becomes an issue
  183. site.build()?;
  184. }
  185. }
  186. Ok(())
  187. }
  188. /// What happens when a template is changed
  189. pub fn after_template_change(site: &mut Site, path: &Path) -> Result<()> {
  190. site.tera.full_reload()?;
  191. match path.file_name().unwrap().to_str().unwrap() {
  192. "sitemap.xml" => site.render_sitemap(),
  193. "rss.xml" => site.render_rss_feed(),
  194. "robots.txt" => site.render_robots(),
  195. "categories.html" | "category.html" => site.render_categories(),
  196. "tags.html" | "tag.html" => site.render_tags(),
  197. "page.html" => {
  198. site.render_sections()?;
  199. site.render_orphan_pages()
  200. },
  201. "section.html" => site.render_sections(),
  202. // Either the index or some unknown template changed
  203. // We can't really know what this change affects so rebuild all
  204. // the things
  205. _ => {
  206. site.render_sections()?;
  207. site.render_orphan_pages()?;
  208. site.render_categories()?;
  209. site.render_tags()
  210. },
  211. }
  212. }