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.

246 lines
9.0KB

  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. // Ensure we have our fn updated so it doesn't contain the permalinks deleted
  103. site.register_get_url_fn();
  104. // Deletion is something that doesn't happen all the time so we
  105. // don't need to optimise it too much
  106. return site.build();
  107. }
  108. // A section was edited
  109. if is_section {
  110. match site.add_section(path, true)? {
  111. Some(prev) => {
  112. // Updating a section
  113. let current_meta = site.sections[path].meta.clone();
  114. // Front matter didn't change, only content did
  115. // so we render only the section page, not its pages
  116. if current_meta == prev.meta {
  117. return site.render_section(&site.sections[path], false);
  118. }
  119. // Front matter changed
  120. for changes in find_section_front_matter_changes(&current_meta, &prev.meta) {
  121. // Sort always comes first if present so the rendering will be fine
  122. match changes {
  123. SectionChangesNeeded::Sort => site.sort_sections_pages(Some(path)),
  124. SectionChangesNeeded::Render => site.render_section(&site.sections[path], false)?,
  125. SectionChangesNeeded::RenderWithPages => site.render_section(&site.sections[path], true)?,
  126. // can't be arsed to make the Delete efficient, it's not a common enough operation
  127. SectionChangesNeeded::Delete => {
  128. site.populate_sections();
  129. site.build()?;
  130. },
  131. };
  132. }
  133. return Ok(());
  134. },
  135. None => {
  136. site.register_get_url_fn();
  137. // New section, only render that one
  138. site.populate_sections();
  139. return site.render_section(&site.sections[path], true);
  140. }
  141. };
  142. }
  143. // A page was edited
  144. match site.add_page(path, true)? {
  145. Some(prev) => {
  146. site.register_get_url_fn();
  147. // Updating a page
  148. let current = site.pages[path].clone();
  149. // Front matter didn't change, only content did
  150. // so we render only the section page, not its pages
  151. if current.meta == prev.meta {
  152. return site.render_page(&site.pages[path]);
  153. }
  154. // Front matter changed
  155. for changes in find_page_front_matter_changes(&current.meta, &prev.meta) {
  156. // Sort always comes first if present so the rendering will be fine
  157. match changes {
  158. PageChangesNeeded::Tags => {
  159. site.populate_tags_and_categories();
  160. site.render_tags()?;
  161. },
  162. PageChangesNeeded::Categories => {
  163. site.populate_tags_and_categories();
  164. site.render_categories()?;
  165. },
  166. PageChangesNeeded::Sort => {
  167. let section_path = match find_parent_section(site, &site.pages[path]) {
  168. Some(s) => s.file.path.clone(),
  169. None => continue // Do nothing if it's an orphan page
  170. };
  171. site.populate_sections();
  172. site.sort_sections_pages(Some(&section_path));
  173. site.render_index()?;
  174. },
  175. PageChangesNeeded::Render => {
  176. site.render_page(&site.pages[path])?;
  177. },
  178. };
  179. }
  180. return Ok(());
  181. },
  182. None => {
  183. site.register_get_url_fn();
  184. // It's a new page!
  185. site.populate_sections();
  186. site.populate_tags_and_categories();
  187. // No need to optimise that yet, we can revisit if it becomes an issue
  188. site.build()?;
  189. }
  190. }
  191. Ok(())
  192. }
  193. /// What happens when a template is changed
  194. pub fn after_template_change(site: &mut Site, path: &Path) -> Result<()> {
  195. site.tera.full_reload()?;
  196. match path.file_name().unwrap().to_str().unwrap() {
  197. "sitemap.xml" => site.render_sitemap(),
  198. "rss.xml" => site.render_rss_feed(),
  199. "robots.txt" => site.render_robots(),
  200. "categories.html" | "category.html" => site.render_categories(),
  201. "tags.html" | "tag.html" => site.render_tags(),
  202. "page.html" => {
  203. site.render_sections()?;
  204. site.render_orphan_pages()
  205. },
  206. "section.html" => site.render_sections(),
  207. // Either the index or some unknown template changed
  208. // We can't really know what this change affects so rebuild all
  209. // the things
  210. _ => {
  211. site.render_sections()?;
  212. site.render_orphan_pages()?;
  213. site.render_categories()?;
  214. site.render_tags()
  215. },
  216. }
  217. }