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.

250 lines
9.2KB

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