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.

385 lines
15KB

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