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.

382 lines
14KB

  1. extern crate site;
  2. #[macro_use]
  3. extern crate errors;
  4. extern crate content;
  5. extern crate front_matter;
  6. use std::path::{Path, Component};
  7. use errors::Result;
  8. use site::Site;
  9. use content::{Page, Section};
  10. use front_matter::{PageFrontMatter, SectionFrontMatter};
  11. /// Finds the section that contains the page given if there is one
  12. pub fn find_parent_section<'a>(site: &'a Site, page: &Page) -> Option<&'a Section> {
  13. for section in site.sections.values() {
  14. if section.is_child_page(&page.file.path) {
  15. return Some(section);
  16. }
  17. }
  18. None
  19. }
  20. #[derive(Debug, Clone, Copy, PartialEq)]
  21. pub enum PageChangesNeeded {
  22. /// Editing `taxonomies`
  23. Taxonomies,
  24. /// Editing `date`, `order` or `weight`
  25. Sort,
  26. /// Editing anything causes a re-render of the page
  27. Render,
  28. }
  29. #[derive(Debug, Clone, Copy, PartialEq)]
  30. pub enum SectionChangesNeeded {
  31. /// Editing `sort_by`
  32. Sort,
  33. /// Editing `title`, `description`, `extra`, `template` or setting `render` to true
  34. Render,
  35. /// Editing `paginate_by`, `paginate_path` or `insert_anchor_links`
  36. RenderWithPages,
  37. /// Setting `render` to false
  38. Delete,
  39. }
  40. /// Evaluates all the params in the front matter that changed so we can do the smallest
  41. /// delta in the serve command
  42. /// Order matters as the actions will be done in insertion order
  43. fn find_section_front_matter_changes(current: &SectionFrontMatter, new: &SectionFrontMatter) -> Vec<SectionChangesNeeded> {
  44. let mut changes_needed = vec![];
  45. if current.sort_by != new.sort_by {
  46. changes_needed.push(SectionChangesNeeded::Sort);
  47. }
  48. // We want to hide the section
  49. // TODO: what to do on redirect_path change?
  50. if current.render && !new.render {
  51. changes_needed.push(SectionChangesNeeded::Delete);
  52. // Nothing else we can do
  53. return changes_needed;
  54. }
  55. if current.paginate_by != new.paginate_by
  56. || current.paginate_path != new.paginate_path
  57. || current.insert_anchor_links != new.insert_anchor_links {
  58. changes_needed.push(SectionChangesNeeded::RenderWithPages);
  59. // Nothing else we can do
  60. return changes_needed;
  61. }
  62. // Any new change will trigger a re-rendering of the section page only
  63. changes_needed.push(SectionChangesNeeded::Render);
  64. changes_needed
  65. }
  66. /// Evaluates all the params in the front matter that changed so we can do the smallest
  67. /// delta in the serve command
  68. /// Order matters as the actions will be done in insertion order
  69. fn find_page_front_matter_changes(current: &PageFrontMatter, other: &PageFrontMatter) -> Vec<PageChangesNeeded> {
  70. let mut changes_needed = vec![];
  71. if current.taxonomies != other.taxonomies {
  72. changes_needed.push(PageChangesNeeded::Taxonomies);
  73. }
  74. if current.date != other.date || current.order != other.order || current.weight != other.weight {
  75. changes_needed.push(PageChangesNeeded::Sort);
  76. }
  77. changes_needed.push(PageChangesNeeded::Render);
  78. changes_needed
  79. }
  80. /// Handles a path deletion: could be a page, a section, a folder
  81. fn delete_element(site: &mut Site, path: &Path, is_section: bool) -> Result<()> {
  82. // Ignore the event if this path was not known
  83. if !site.sections.contains_key(path) && !site.pages.contains_key(path) {
  84. return Ok(());
  85. }
  86. if is_section {
  87. if let Some(s) = site.pages.remove(path) {
  88. site.permalinks.remove(&s.file.relative);
  89. site.populate_sections();
  90. }
  91. } else if let Some(p) = site.pages.remove(path) {
  92. site.permalinks.remove(&p.file.relative);
  93. if !p.meta.taxonomies.is_empty() {
  94. site.populate_taxonomies()?;
  95. }
  96. // if there is a parent section, we will need to re-render it
  97. // most likely
  98. if find_parent_section(site, &p).is_some() {
  99. site.populate_sections();
  100. }
  101. }
  102. // Ensure we have our fn updated so it doesn't contain the permalink(s)/section/page deleted
  103. site.register_tera_global_fns();
  104. // Deletion is something that doesn't happen all the time so we
  105. // don't need to optimise it too much
  106. site.build()
  107. }
  108. /// Handles a `_index.md` (a section) being edited in some ways
  109. fn handle_section_editing(site: &mut Site, path: &Path) -> Result<()> {
  110. let section = Section::from_file(path, &site.config)?;
  111. match site.add_section(section, true)? {
  112. // Updating a section
  113. Some(prev) => {
  114. // Copy the section data so we don't end up with an almost empty object
  115. site.sections.get_mut(path).unwrap().pages = prev.pages;
  116. site.sections.get_mut(path).unwrap().ignored_pages = prev.ignored_pages;
  117. site.sections.get_mut(path).unwrap().subsections = prev.subsections;
  118. if site.sections[path].meta == prev.meta {
  119. // Front matter didn't change, only content did
  120. // so we render only the section page, not its pages
  121. return site.render_section(&site.sections[path], false);
  122. }
  123. // Front matter changed
  124. for changes in find_section_front_matter_changes(&site.sections[path].meta, &prev.meta) {
  125. // Sort always comes first if present so the rendering will be fine
  126. match changes {
  127. SectionChangesNeeded::Sort => {
  128. site.sort_sections_pages(Some(path));
  129. site.register_tera_global_fns();
  130. }
  131. SectionChangesNeeded::Render => site.render_section(&site.sections[path], false)?,
  132. SectionChangesNeeded::RenderWithPages => site.render_section(&site.sections[path], true)?,
  133. // not a common enough operation to make it worth optimizing
  134. SectionChangesNeeded::Delete => {
  135. site.populate_sections();
  136. site.build()?;
  137. }
  138. };
  139. }
  140. Ok(())
  141. }
  142. // New section, only render that one
  143. None => {
  144. site.populate_sections();
  145. site.register_tera_global_fns();
  146. site.render_section(&site.sections[path], true)
  147. }
  148. }
  149. }
  150. macro_rules! render_parent_section {
  151. ($site: expr, $path: expr) => {
  152. if let Some(s) = find_parent_section($site, &$site.pages[$path]) {
  153. $site.render_section(s, false)?;
  154. };
  155. }
  156. }
  157. /// Handles a page being edited in some ways
  158. fn handle_page_editing(site: &mut Site, path: &Path) -> Result<()> {
  159. let page = Page::from_file(path, &site.config)?;
  160. match site.add_page(page, true)? {
  161. // Updating a page
  162. Some(prev) => {
  163. // Front matter didn't change, only content did
  164. if site.pages[path].meta == prev.meta {
  165. // Other than the page itself, the summary might be seen
  166. // on a paginated list for a blog for example
  167. if site.pages[path].summary.is_some() {
  168. render_parent_section!(site, path);
  169. }
  170. // TODO: register_tera_global_fns is expensive as it involves lots of cloning
  171. // I can't think of a valid usecase where you would need the content
  172. // of a page through a global fn so it's commented out for now
  173. // site.register_tera_global_fns();
  174. return site.render_page(&site.pages[path]);
  175. }
  176. // Front matter changed
  177. let mut sections_populated = false;
  178. for changes in find_page_front_matter_changes(&site.pages[path].meta, &prev.meta) {
  179. // Sort always comes first if present so the rendering will be fine
  180. match changes {
  181. PageChangesNeeded::Taxonomies => {
  182. site.populate_taxonomies()?;
  183. site.register_tera_global_fns();
  184. site.render_taxonomies()?;
  185. }
  186. PageChangesNeeded::Sort => {
  187. let section_path = match find_parent_section(site, &site.pages[path]) {
  188. Some(s) => s.file.path.clone(),
  189. None => continue // Do nothing if it's an orphan page
  190. };
  191. if !sections_populated {
  192. site.populate_sections();
  193. sections_populated = true;
  194. }
  195. site.sort_sections_pages(Some(&section_path));
  196. site.register_tera_global_fns();
  197. site.render_index()?;
  198. }
  199. PageChangesNeeded::Render => {
  200. if !sections_populated {
  201. site.populate_sections();
  202. sections_populated = true;
  203. }
  204. site.register_tera_global_fns();
  205. render_parent_section!(site, path);
  206. site.render_page(&site.pages[path])?;
  207. }
  208. };
  209. }
  210. Ok(())
  211. }
  212. // It's a new page!
  213. None => {
  214. site.populate_sections();
  215. site.populate_taxonomies()?;
  216. site.register_tera_global_fns();
  217. // No need to optimise that yet, we can revisit if it becomes an issue
  218. site.build()
  219. }
  220. }
  221. }
  222. /// What happens when a section or a page is changed
  223. pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> {
  224. let is_section = path.file_name().unwrap() == "_index.md";
  225. let is_md = path.extension().unwrap() == "md";
  226. let index = path.parent().unwrap().join("index.md");
  227. // A few situations can happen:
  228. // 1. Change on .md files
  229. // a. Is there an `index.md`? Return an error if it's something other than delete
  230. // b. Deleted? remove the element
  231. // c. Edited?
  232. // 1. filename is `_index.md`, this is a section
  233. // 1. it's a page otherwise
  234. // 2. Change on non .md files
  235. // a. Try to find a corresponding `_index.md`
  236. // 1. Nothing? Return Ok
  237. // 2. Something? Update the page
  238. if is_md {
  239. // only delete if it was able to be added in the first place
  240. if !index.exists() && !path.exists() {
  241. delete_element(site, path, is_section)?;
  242. }
  243. // Added another .md in a assets directory
  244. if index.exists() && path.exists() && path != index {
  245. bail!(
  246. "Change on {:?} detected but there is already an `index.md` in the same folder",
  247. path.display()
  248. );
  249. } else if index.exists() && !path.exists() {
  250. // deleted the wrong .md, do nothing
  251. return Ok(());
  252. }
  253. if is_section {
  254. handle_section_editing(site, path)
  255. } else {
  256. handle_page_editing(site, path)
  257. }
  258. } else if index.exists() {
  259. handle_page_editing(site, &index)
  260. } else {
  261. Ok(())
  262. }
  263. }
  264. /// What happens when a template is changed
  265. pub fn after_template_change(site: &mut Site, path: &Path) -> Result<()> {
  266. site.tera.full_reload()?;
  267. let filename = path.file_name().unwrap().to_str().unwrap();
  268. match filename {
  269. "sitemap.xml" => site.render_sitemap(),
  270. "rss.xml" => site.render_rss_feed(site.pages.values().collect(), None),
  271. "robots.txt" => site.render_robots(),
  272. "single.html" | "list.html" => site.render_taxonomies(),
  273. "page.html" => {
  274. site.render_sections()?;
  275. site.render_orphan_pages()
  276. }
  277. "section.html" => site.render_sections(),
  278. // Either the index or some unknown template changed
  279. // We can't really know what this change affects so rebuild all
  280. // the things
  281. _ => {
  282. // If we are updating a shortcode, re-render the markdown of all pages/site
  283. // because we have no clue which one needs rebuilding
  284. // TODO: look if there the shortcode is used in the markdown instead of re-rendering
  285. // everything
  286. if path.components().any(|x| x == Component::Normal("shortcodes".as_ref())) {
  287. site.render_markdown()?;
  288. }
  289. site.populate_sections();
  290. site.render_sections()?;
  291. site.render_orphan_pages()?;
  292. site.render_taxonomies()
  293. }
  294. }
  295. }
  296. #[cfg(test)]
  297. mod tests {
  298. use std::collections::HashMap;
  299. use front_matter::{PageFrontMatter, SectionFrontMatter, SortBy};
  300. use super::{
  301. find_page_front_matter_changes, find_section_front_matter_changes,
  302. PageChangesNeeded, SectionChangesNeeded,
  303. };
  304. #[test]
  305. fn can_find_taxonomy_changes_in_page_frontmatter() {
  306. let mut taxonomies = HashMap::new();
  307. taxonomies.insert("tags".to_string(), vec!["a tag".to_string()]);
  308. let new = PageFrontMatter { taxonomies, ..PageFrontMatter::default() };
  309. let changes = find_page_front_matter_changes(&PageFrontMatter::default(), &new);
  310. assert_eq!(changes, vec![PageChangesNeeded::Taxonomies, PageChangesNeeded::Render]);
  311. }
  312. #[test]
  313. fn can_find_multiple_changes_in_page_frontmatter() {
  314. let mut taxonomies = HashMap::new();
  315. taxonomies.insert("categories".to_string(), vec!["a category".to_string()]);
  316. let current = PageFrontMatter { taxonomies, order: Some(1), ..PageFrontMatter::default() };
  317. let changes = find_page_front_matter_changes(&current, &PageFrontMatter::default());
  318. assert_eq!(changes, vec![PageChangesNeeded::Taxonomies, PageChangesNeeded::Sort, PageChangesNeeded::Render]);
  319. }
  320. #[test]
  321. fn can_find_sort_changes_in_section_frontmatter() {
  322. let new = SectionFrontMatter { sort_by: SortBy::Date, ..SectionFrontMatter::default() };
  323. let changes = find_section_front_matter_changes(&SectionFrontMatter::default(), &new);
  324. assert_eq!(changes, vec![SectionChangesNeeded::Sort, SectionChangesNeeded::Render]);
  325. }
  326. #[test]
  327. fn can_find_render_changes_in_section_frontmatter() {
  328. let new = SectionFrontMatter { render: false, ..SectionFrontMatter::default() };
  329. let changes = find_section_front_matter_changes(&SectionFrontMatter::default(), &new);
  330. assert_eq!(changes, vec![SectionChangesNeeded::Delete]);
  331. }
  332. #[test]
  333. fn can_find_paginate_by_changes_in_section_frontmatter() {
  334. let new = SectionFrontMatter { paginate_by: Some(10), ..SectionFrontMatter::default() };
  335. let changes = find_section_front_matter_changes(&SectionFrontMatter::default(), &new);
  336. assert_eq!(changes, vec![SectionChangesNeeded::RenderWithPages]);
  337. }
  338. }