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.

252 lines
9.2KB

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