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.

lib.rs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. extern crate site;
  2. extern crate errors;
  3. extern crate content;
  4. extern crate front_matter;
  5. use std::path::{Path, Component};
  6. use errors::Result;
  7. use site::Site;
  8. use content::{Page, Section};
  9. use front_matter::{PageFrontMatter, SectionFrontMatter};
  10. /// Finds the section that contains the page given if there is one
  11. pub fn find_parent_section<'a>(site: &'a Site, page: &Page) -> Option<&'a Section> {
  12. for section in site.sections.values() {
  13. if section.is_child_page(&page.file.path) {
  14. return Some(section)
  15. }
  16. }
  17. None
  18. }
  19. #[derive(Debug, Clone, Copy, PartialEq)]
  20. pub enum PageChangesNeeded {
  21. /// Editing `tags`
  22. Tags,
  23. /// Editing `categories`
  24. Categories,
  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.should_render() && !new.should_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.tags != other.tags {
  73. changes_needed.push(PageChangesNeeded::Tags);
  74. }
  75. if current.category != other.category {
  76. changes_needed.push(PageChangesNeeded::Categories);
  77. }
  78. if current.date != other.date || current.order != other.order || current.weight != other.weight {
  79. changes_needed.push(PageChangesNeeded::Sort);
  80. }
  81. changes_needed.push(PageChangesNeeded::Render);
  82. changes_needed
  83. }
  84. /// Handles a path deletion: could be a page, a section, a folder
  85. fn delete_element(site: &mut Site, path: &Path, is_section: bool) -> Result<()> {
  86. // Ignore the event if this path was not known
  87. if !site.sections.contains_key(path) && !site.pages.contains_key(path) {
  88. return Ok(());
  89. }
  90. if is_section {
  91. if let Some(s) = site.pages.remove(path) {
  92. site.permalinks.remove(&s.file.relative);
  93. site.populate_sections();
  94. }
  95. } else {
  96. if let Some(p) = site.pages.remove(path) {
  97. site.permalinks.remove(&p.file.relative);
  98. if p.meta.has_tags() || p.meta.category.is_some() {
  99. site.populate_tags_and_categories();
  100. }
  101. // if there is a parent section, we will need to re-render it
  102. // most likely
  103. if find_parent_section(site, &p).is_some() {
  104. site.populate_sections();
  105. }
  106. };
  107. }
  108. // Ensure we have our fn updated so it doesn't contain the permalink(s)/section/page deleted
  109. site.register_tera_global_fns();
  110. // Deletion is something that doesn't happen all the time so we
  111. // don't need to optimise it too much
  112. return site.build();
  113. }
  114. /// Handles a `_index.md` (a section) being edited in some ways
  115. fn handle_section_editing(site: &mut Site, path: &Path) -> Result<()> {
  116. let section = Section::from_file(path, &site.config)?;
  117. match site.add_section(section, true)? {
  118. // Updating a section
  119. Some(prev) => {
  120. if site.sections[path].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.sections[path], false);
  124. }
  125. // Front matter changed
  126. for changes in find_section_front_matter_changes(&site.sections[path].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.sections[path], false)?,
  134. SectionChangesNeeded::RenderWithPages => site.render_section(&site.sections[path], 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. return Ok(());
  143. },
  144. // New section, only render that one
  145. None => {
  146. site.populate_sections();
  147. site.register_tera_global_fns();
  148. return site.render_section(&site.sections[path], true);
  149. }
  150. };
  151. }
  152. macro_rules! render_parent_section {
  153. ($site: expr, $path: expr) => {
  154. match find_parent_section($site, &$site.pages[$path]) {
  155. Some(s) => {
  156. $site.render_section(s, false)?;
  157. },
  158. None => (),
  159. };
  160. }
  161. }
  162. /// Handles a page being edited in some ways
  163. fn handle_page_editing(site: &mut Site, path: &Path) -> Result<()> {
  164. let page = Page::from_file(path, &site.config)?;
  165. match site.add_page(page, true)? {
  166. // Updating a page
  167. Some(prev) => {
  168. // Front matter didn't change, only content did
  169. if site.pages[path].meta == prev.meta {
  170. // Other than the page itself, the summary might be seen
  171. // on a paginated list for a blog for example
  172. if site.pages[path].summary.is_some() {
  173. render_parent_section!(site, path);
  174. }
  175. // TODO: register_tera_global_fns is expensive as it involves lots of cloning
  176. // I can't think of a valid usecase where you would need the content
  177. // of a page through a global fn so it's commented out for now
  178. // site.register_tera_global_fns();
  179. return site.render_page(& site.pages[path]);
  180. }
  181. // Front matter changed
  182. let mut taxonomies_populated = false;
  183. let mut sections_populated = false;
  184. for changes in find_page_front_matter_changes(&site.pages[path].meta, &prev.meta) {
  185. // Sort always comes first if present so the rendering will be fine
  186. match changes {
  187. PageChangesNeeded::Tags => {
  188. if !taxonomies_populated {
  189. site.populate_tags_and_categories();
  190. taxonomies_populated = true;
  191. }
  192. site.register_tera_global_fns();
  193. site.render_tags()?;
  194. },
  195. PageChangesNeeded::Categories => {
  196. if !taxonomies_populated {
  197. site.populate_tags_and_categories();
  198. taxonomies_populated = true;
  199. }
  200. site.register_tera_global_fns();
  201. site.render_categories()?;
  202. },
  203. PageChangesNeeded::Sort => {
  204. let section_path = match find_parent_section(site, &site.pages[path]) {
  205. Some(s) => s.file.path.clone(),
  206. None => continue // Do nothing if it's an orphan page
  207. };
  208. if !sections_populated {
  209. site.populate_sections();
  210. sections_populated = true;
  211. }
  212. site.sort_sections_pages(Some(&section_path));
  213. site.register_tera_global_fns();
  214. site.render_index()?;
  215. },
  216. PageChangesNeeded::Render => {
  217. if !sections_populated {
  218. site.populate_sections();
  219. sections_populated = true;
  220. }
  221. site.register_tera_global_fns();
  222. render_parent_section!(site, path);
  223. site.render_page(&site.pages[path])?;
  224. },
  225. };
  226. }
  227. Ok(())
  228. },
  229. // It's a new page!
  230. None => {
  231. site.populate_sections();
  232. site.populate_tags_and_categories();
  233. site.register_tera_global_fns();
  234. // No need to optimise that yet, we can revisit if it becomes an issue
  235. site.build()
  236. }
  237. }
  238. }
  239. // What happens when a section or a page is changed
  240. pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> {
  241. let is_section = path.file_name().unwrap() == "_index.md";
  242. // A page or section got deleted
  243. if !path.exists() {
  244. delete_element(site, path, is_section)?;
  245. }
  246. if is_section {
  247. handle_section_editing(site, path)
  248. } else {
  249. handle_page_editing(site, path)
  250. }
  251. }
  252. /// What happens when a template is changed
  253. pub fn after_template_change(site: &mut Site, path: &Path) -> Result<()> {
  254. site.tera.full_reload()?;
  255. let filename = path.file_name().unwrap().to_str().unwrap();
  256. match filename {
  257. "sitemap.xml" => site.render_sitemap(),
  258. "rss.xml" => site.render_rss_feed(),
  259. "robots.txt" => site.render_robots(),
  260. "categories.html" | "category.html" => site.render_categories(),
  261. "tags.html" | "tag.html" => site.render_tags(),
  262. "page.html" => {
  263. site.render_sections()?;
  264. site.render_orphan_pages()
  265. },
  266. "section.html" => site.render_sections(),
  267. // Either the index or some unknown template changed
  268. // We can't really know what this change affects so rebuild all
  269. // the things
  270. _ => {
  271. // If we are updating a shortcode, re-render the markdown of all pages/site
  272. // because we have no clue which one needs rebuilding
  273. // TODO: look if there the shortcode is used in the markdown instead of re-rendering
  274. // everything
  275. if path.components().collect::<Vec<_>>().contains(&Component::Normal("shortcodes".as_ref())) {
  276. site.render_markdown()?;
  277. }
  278. site.populate_sections();
  279. site.render_sections()?;
  280. site.render_orphan_pages()?;
  281. site.render_categories()?;
  282. site.render_tags()
  283. },
  284. }
  285. }
  286. #[cfg(test)]
  287. mod tests {
  288. use front_matter::{PageFrontMatter, SectionFrontMatter, SortBy};
  289. use super::{
  290. find_page_front_matter_changes, find_section_front_matter_changes,
  291. PageChangesNeeded, SectionChangesNeeded
  292. };
  293. #[test]
  294. fn can_find_tag_changes_in_page_frontmatter() {
  295. let new = PageFrontMatter { tags: Some(vec!["a tag".to_string()]), ..PageFrontMatter::default() };
  296. let changes = find_page_front_matter_changes(&PageFrontMatter::default(), &new);
  297. assert_eq!(changes, vec![PageChangesNeeded::Tags, PageChangesNeeded::Render]);
  298. }
  299. #[test]
  300. fn can_find_category_changes_in_page_frontmatter() {
  301. let current = PageFrontMatter { category: Some("a category".to_string()), ..PageFrontMatter::default() };
  302. let changes = find_page_front_matter_changes(&current, &PageFrontMatter::default());
  303. assert_eq!(changes, vec![PageChangesNeeded::Categories, PageChangesNeeded::Render]);
  304. }
  305. #[test]
  306. fn can_find_multiple_changes_in_page_frontmatter() {
  307. let current = PageFrontMatter { category: Some("a category".to_string()), order: Some(1), ..PageFrontMatter::default() };
  308. let changes = find_page_front_matter_changes(&current, &PageFrontMatter::default());
  309. assert_eq!(changes, vec![PageChangesNeeded::Categories, PageChangesNeeded::Sort, PageChangesNeeded::Render]);
  310. }
  311. #[test]
  312. fn can_find_sort_changes_in_section_frontmatter() {
  313. let new = SectionFrontMatter { sort_by: Some(SortBy::Date), ..SectionFrontMatter::default() };
  314. let changes = find_section_front_matter_changes(&SectionFrontMatter::default(), &new);
  315. assert_eq!(changes, vec![SectionChangesNeeded::Sort, SectionChangesNeeded::Render]);
  316. }
  317. #[test]
  318. fn can_find_render_changes_in_section_frontmatter() {
  319. let new = SectionFrontMatter { render: Some(false), ..SectionFrontMatter::default() };
  320. let changes = find_section_front_matter_changes(&SectionFrontMatter::default(), &new);
  321. assert_eq!(changes, vec![SectionChangesNeeded::Delete]);
  322. }
  323. #[test]
  324. fn can_find_paginate_by_changes_in_section_frontmatter() {
  325. let new = SectionFrontMatter { paginate_by: Some(10), ..SectionFrontMatter::default() };
  326. let changes = find_section_front_matter_changes(&SectionFrontMatter::default(), &new);
  327. assert_eq!(changes, vec![SectionChangesNeeded::RenderWithPages]);
  328. }
  329. }