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.

405 lines
15KB

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