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.

248 lines
9.1KB

  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.file.path) {
  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`, `paginate_path` or `insert_anchor`
  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
  49. || current.paginate_path != other.paginate_path
  50. || current.insert_anchor != other.insert_anchor {
  51. changes_needed.push(SectionChangesNeeded::RenderWithPages);
  52. // Nothing else we can do
  53. return changes_needed;
  54. }
  55. // Any other change will trigger a re-rendering of the section page only
  56. changes_needed.push(SectionChangesNeeded::Render);
  57. changes_needed
  58. }
  59. /// Evaluates all the params in the front matter that changed so we can do the smallest
  60. /// delta in the serve command
  61. fn find_page_front_matter_changes(current: &PageFrontMatter, other: &PageFrontMatter) -> Vec<PageChangesNeeded> {
  62. let mut changes_needed = vec![];
  63. if current.tags != other.tags {
  64. changes_needed.push(PageChangesNeeded::Tags);
  65. }
  66. if current.category != other.category {
  67. changes_needed.push(PageChangesNeeded::Categories);
  68. }
  69. if current.date != other.date || current.order != other.order {
  70. changes_needed.push(PageChangesNeeded::Sort);
  71. }
  72. changes_needed.push(PageChangesNeeded::Render);
  73. changes_needed
  74. }
  75. // What happens when a section or a page is changed
  76. pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> {
  77. let is_section = path.file_name().unwrap() == "_index.md";
  78. // A page or section got deleted
  79. if !path.exists() {
  80. if is_section {
  81. // A section was deleted, many things can be impacted:
  82. // - the pages of the section are becoming orphans
  83. // - any page that was referencing the section (index, etc)
  84. let relative_path = site.sections[path].file.relative.clone();
  85. // Remove the link to it and the section itself from the Site
  86. site.permalinks.remove(&relative_path);
  87. site.sections.remove(path);
  88. site.populate_sections();
  89. } else {
  90. // A page was deleted, many things can be impacted:
  91. // - the section the page is in
  92. // - any page that was referencing the section (index, etc)
  93. let relative_path = site.pages[path].file.relative.clone();
  94. site.permalinks.remove(&relative_path);
  95. if let Some(p) = site.pages.remove(path) {
  96. if p.meta.has_tags() || p.meta.category.is_some() {
  97. site.populate_tags_and_categories();
  98. }
  99. if find_parent_section(site, &p).is_some() {
  100. site.populate_sections();
  101. }
  102. };
  103. }
  104. // Ensure we have our fn updated so it doesn't contain the permalinks deleted
  105. site.register_get_url_fn();
  106. // Deletion is something that doesn't happen all the time so we
  107. // don't need to optimise it too much
  108. return site.build();
  109. }
  110. // A section was edited
  111. if is_section {
  112. match site.add_section(path, true)? {
  113. Some(prev) => {
  114. // Updating a section
  115. let current_meta = site.sections[path].meta.clone();
  116. // Front matter didn't change, only content did
  117. // so we render only the section page, not its pages
  118. if current_meta == prev.meta {
  119. return site.render_section(&site.sections[path], false);
  120. }
  121. // Front matter changed
  122. for changes in find_section_front_matter_changes(&current_meta, &prev.meta) {
  123. // Sort always comes first if present so the rendering will be fine
  124. match changes {
  125. SectionChangesNeeded::Sort => site.sort_sections_pages(Some(path)),
  126. SectionChangesNeeded::Render => site.render_section(&site.sections[path], false)?,
  127. SectionChangesNeeded::RenderWithPages => site.render_section(&site.sections[path], true)?,
  128. // can't be arsed to make the Delete efficient, it's not a common enough operation
  129. SectionChangesNeeded::Delete => {
  130. site.populate_sections();
  131. site.build()?;
  132. },
  133. };
  134. }
  135. return Ok(());
  136. },
  137. None => {
  138. site.register_get_url_fn();
  139. // New section, only render that one
  140. site.populate_sections();
  141. return site.render_section(&site.sections[path], true);
  142. }
  143. };
  144. }
  145. // A page was edited
  146. match site.add_page(path, true)? {
  147. Some(prev) => {
  148. site.register_get_url_fn();
  149. // Updating a page
  150. let current = site.pages[path].clone();
  151. // Front matter didn't change, only content did
  152. // so we render only the section page, not its content
  153. if current.meta == prev.meta {
  154. return site.render_page(&current, find_parent_section(site, &current));
  155. }
  156. // Front matter changed
  157. for changes in find_page_front_matter_changes(&current.meta, &prev.meta) {
  158. // Sort always comes first if present so the rendering will be fine
  159. match changes {
  160. PageChangesNeeded::Tags => {
  161. site.populate_tags_and_categories();
  162. site.render_tags()?;
  163. },
  164. PageChangesNeeded::Categories => {
  165. site.populate_tags_and_categories();
  166. site.render_categories()?;
  167. },
  168. PageChangesNeeded::Sort => {
  169. let section_path = match find_parent_section(site, &site.pages[path]) {
  170. Some(s) => s.file.path.clone(),
  171. None => continue // Do nothing if it's an orphan page
  172. };
  173. site.populate_sections();
  174. site.sort_sections_pages(Some(&section_path));
  175. site.render_index()?;
  176. },
  177. PageChangesNeeded::Render => {
  178. site.render_page(&site.pages[path], find_parent_section(site, &current))?;
  179. },
  180. };
  181. }
  182. return Ok(());
  183. },
  184. None => {
  185. site.register_get_url_fn();
  186. // It's a new page!
  187. site.populate_sections();
  188. site.populate_tags_and_categories();
  189. // No need to optimise that yet, we can revisit if it becomes an issue
  190. site.build()?;
  191. }
  192. }
  193. Ok(())
  194. }
  195. /// What happens when a template is changed
  196. pub fn after_template_change(site: &mut Site, path: &Path) -> Result<()> {
  197. site.tera.full_reload()?;
  198. match path.file_name().unwrap().to_str().unwrap() {
  199. "sitemap.xml" => site.render_sitemap(),
  200. "rss.xml" => site.render_rss_feed(),
  201. "robots.txt" => site.render_robots(),
  202. "categories.html" | "category.html" => site.render_categories(),
  203. "tags.html" | "tag.html" => site.render_tags(),
  204. "page.html" => {
  205. site.render_sections()?;
  206. site.render_orphan_pages()
  207. },
  208. "section.html" => site.render_sections(),
  209. // Either the index or some unknown template changed
  210. // We can't really know what this change affects so rebuild all
  211. // the things
  212. _ => {
  213. site.render_sections()?;
  214. site.render_orphan_pages()?;
  215. site.render_categories()?;
  216. site.render_tags()
  217. },
  218. }
  219. }