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.

452 lines
17KB

  1. extern crate site;
  2. #[macro_use]
  3. extern crate errors;
  4. extern crate front_matter;
  5. extern crate library;
  6. use std::path::{Component, Path};
  7. use errors::Result;
  8. use front_matter::{PageFrontMatter, SectionFrontMatter};
  9. use library::{Page, Section};
  10. use site::Site;
  11. #[derive(Debug, Clone, Copy, PartialEq)]
  12. pub enum PageChangesNeeded {
  13. /// Editing `taxonomies`
  14. Taxonomies,
  15. /// Editing `date`, `order` or `weight`
  16. Sort,
  17. /// Editing anything causes a re-render of the page
  18. Render,
  19. }
  20. #[derive(Debug, Clone, Copy, PartialEq)]
  21. pub enum SectionChangesNeeded {
  22. /// Editing `sort_by`
  23. Sort,
  24. /// Editing `title`, `description`, `extra`, `template` or setting `render` to true
  25. Render,
  26. /// Editing `paginate_by`, `paginate_path` or `insert_anchor_links`
  27. RenderWithPages,
  28. /// Setting `render` to false
  29. Delete,
  30. /// Changing `transparent`
  31. Transparent,
  32. }
  33. /// Evaluates all the params in the front matter that changed so we can do the smallest
  34. /// delta in the serve command
  35. /// Order matters as the actions will be done in insertion order
  36. fn find_section_front_matter_changes(
  37. current: &SectionFrontMatter,
  38. new: &SectionFrontMatter,
  39. ) -> Vec<SectionChangesNeeded> {
  40. let mut changes_needed = vec![];
  41. if current.sort_by != new.sort_by {
  42. changes_needed.push(SectionChangesNeeded::Sort);
  43. }
  44. if current.transparent != new.transparent {
  45. changes_needed.push(SectionChangesNeeded::Transparent);
  46. }
  47. // We want to hide the section
  48. // TODO: what to do on redirect_path change?
  49. if current.render && !new.render {
  50. changes_needed.push(SectionChangesNeeded::Delete);
  51. // Nothing else we can do
  52. return changes_needed;
  53. }
  54. if current.paginate_by != new.paginate_by
  55. || current.paginate_path != new.paginate_path
  56. || current.insert_anchor_links != new.insert_anchor_links
  57. {
  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(
  70. current: &PageFrontMatter,
  71. other: &PageFrontMatter,
  72. ) -> Vec<PageChangesNeeded> {
  73. let mut changes_needed = vec![];
  74. if current.taxonomies != other.taxonomies {
  75. changes_needed.push(PageChangesNeeded::Taxonomies);
  76. }
  77. if current.date != other.date || current.order != other.order || current.weight != other.weight
  78. {
  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. {
  87. let mut library = site.library.write().unwrap();
  88. // Ignore the event if this path was not known
  89. if !library.contains_section(&path.to_path_buf())
  90. && !library.contains_page(&path.to_path_buf())
  91. {
  92. return Ok(());
  93. }
  94. if is_section {
  95. if let Some(s) = library.remove_section(&path.to_path_buf()) {
  96. site.permalinks.remove(&s.file.relative);
  97. }
  98. } else if let Some(p) = library.remove_page(&path.to_path_buf()) {
  99. site.permalinks.remove(&p.file.relative);
  100. }
  101. }
  102. // We might have delete the root _index.md so ensure we have at least the default one
  103. // before populating
  104. site.create_default_index_sections()?;
  105. site.populate_sections();
  106. site.populate_taxonomies()?;
  107. // Ensure we have our fn updated so it doesn't contain the permalink(s)/section/page deleted
  108. site.register_early_global_fns();
  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. 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. let pathbuf = path.to_path_buf();
  118. match site.add_section(section, true)? {
  119. // Updating a section
  120. Some(prev) => {
  121. site.populate_sections();
  122. {
  123. let library = site.library.read().unwrap();
  124. if library.get_section(&pathbuf).unwrap().meta == prev.meta {
  125. // Front matter didn't change, only content did
  126. // so we render only the section page, not its pages
  127. return site.render_section(&library.get_section(&pathbuf).unwrap(), false);
  128. }
  129. }
  130. // Front matter changed
  131. let changes = find_section_front_matter_changes(
  132. &site.library.read().unwrap().get_section(&pathbuf).unwrap().meta,
  133. &prev.meta,
  134. );
  135. for change in changes {
  136. // Sort always comes first if present so the rendering will be fine
  137. match change {
  138. SectionChangesNeeded::Sort => {
  139. site.register_tera_global_fns();
  140. }
  141. SectionChangesNeeded::Render => site.render_section(
  142. &site.library.read().unwrap().get_section(&pathbuf).unwrap(),
  143. false,
  144. )?,
  145. SectionChangesNeeded::RenderWithPages => site.render_section(
  146. &site.library.read().unwrap().get_section(&pathbuf).unwrap(),
  147. true,
  148. )?,
  149. // not a common enough operation to make it worth optimizing
  150. SectionChangesNeeded::Delete | SectionChangesNeeded::Transparent => {
  151. site.build()?;
  152. }
  153. };
  154. }
  155. Ok(())
  156. }
  157. // New section, only render that one
  158. None => {
  159. site.populate_sections();
  160. site.register_tera_global_fns();
  161. site.render_section(&site.library.read().unwrap().get_section(&pathbuf).unwrap(), true)
  162. }
  163. }
  164. }
  165. macro_rules! render_parent_sections {
  166. ($site: expr, $path: expr) => {
  167. for s in $site.library.read().unwrap().find_parent_sections($path) {
  168. $site.render_section(s, false)?;
  169. }
  170. };
  171. }
  172. /// Handles a page being edited in some ways
  173. fn handle_page_editing(site: &mut Site, path: &Path) -> Result<()> {
  174. let page = Page::from_file(path, &site.config)?;
  175. let pathbuf = path.to_path_buf();
  176. match site.add_page(page, true)? {
  177. // Updating a page
  178. Some(prev) => {
  179. site.populate_sections();
  180. site.populate_taxonomies()?;
  181. site.register_tera_global_fns();
  182. {
  183. let library = site.library.read().unwrap();
  184. // Front matter didn't change, only content did
  185. if library.get_page(&pathbuf).unwrap().meta == prev.meta {
  186. // Other than the page itself, the summary might be seen
  187. // on a paginated list for a blog for example
  188. if library.get_page(&pathbuf).unwrap().summary.is_some() {
  189. render_parent_sections!(site, path);
  190. }
  191. return site.render_page(&library.get_page(&pathbuf).unwrap());
  192. }
  193. }
  194. // Front matter changed
  195. let changes = find_page_front_matter_changes(
  196. &site.library.read().unwrap().get_page(&pathbuf).unwrap().meta,
  197. &prev.meta,
  198. );
  199. for change in changes {
  200. site.register_tera_global_fns();
  201. // Sort always comes first if present so the rendering will be fine
  202. match change {
  203. PageChangesNeeded::Taxonomies => {
  204. site.populate_taxonomies()?;
  205. site.render_taxonomies()?;
  206. }
  207. PageChangesNeeded::Sort => {
  208. site.render_index()?;
  209. }
  210. PageChangesNeeded::Render => {
  211. render_parent_sections!(site, path);
  212. site.render_page(
  213. &site.library.read().unwrap().get_page(&path.to_path_buf()).unwrap(),
  214. )?;
  215. }
  216. };
  217. }
  218. Ok(())
  219. }
  220. // It's a new page!
  221. None => {
  222. site.populate_sections();
  223. site.populate_taxonomies()?;
  224. site.register_early_global_fns();
  225. site.register_tera_global_fns();
  226. // No need to optimise that yet, we can revisit if it becomes an issue
  227. site.build()
  228. }
  229. }
  230. }
  231. /// What happens when we rename a file/folder in the content directory.
  232. /// Note that this is only called for folders when it isn't empty
  233. pub fn after_content_rename(site: &mut Site, old: &Path, new: &Path) -> Result<()> {
  234. let new_path = if new.is_dir() {
  235. if new.join("_index.md").exists() {
  236. // This is a section keep the dir folder to differentiate from renaming _index.md
  237. // which doesn't do the same thing
  238. new.to_path_buf()
  239. } else if new.join("index.md").exists() {
  240. new.join("index.md")
  241. } else {
  242. bail!("Got unexpected folder {:?} while handling renaming that was not expected", new);
  243. }
  244. } else {
  245. new.to_path_buf()
  246. };
  247. // A section folder has been renamed: just reload the whole site and rebuild it as we
  248. // do not really know what needs to be rendered
  249. if new_path.is_dir() {
  250. site.load()?;
  251. return site.build();
  252. }
  253. // We ignore renames on non-markdown files for now
  254. if let Some(ext) = new_path.extension() {
  255. if ext != "md" {
  256. return Ok(());
  257. }
  258. }
  259. // Renaming a file to _index.md, let the section editing do something and hope for the best
  260. if new_path.file_name().unwrap() == "_index.md" {
  261. // We aren't entirely sure where the original thing was so just try to delete whatever was
  262. // at the old path
  263. {
  264. let mut library = site.library.write().unwrap();
  265. library.remove_page(&old.to_path_buf());
  266. library.remove_section(&old.to_path_buf());
  267. }
  268. return handle_section_editing(site, &new_path);
  269. }
  270. // If it is a page, just delete what was there before and
  271. // fake it's a new page
  272. let old_path = if new_path.file_name().unwrap() == "index.md" {
  273. old.join("index.md")
  274. } else {
  275. old.to_path_buf()
  276. };
  277. site.library.write().unwrap().remove_page(&old_path);
  278. handle_page_editing(site, &new_path)
  279. }
  280. /// What happens when a section or a page is created/edited
  281. pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> {
  282. let is_section = path.file_name().unwrap() == "_index.md";
  283. let is_md = path.extension().unwrap() == "md";
  284. let index = path.parent().unwrap().join("index.md");
  285. let mut potential_indices = vec![path.parent().unwrap().join("index.md")];
  286. for language in &site.config.languages {
  287. potential_indices.push(path.parent().unwrap().join(format!("index.{}.md", language.code)));
  288. }
  289. let colocated_index = potential_indices.contains(&path.to_path_buf());
  290. // A few situations can happen:
  291. // 1. Change on .md files
  292. // a. Is there already an `index.md`? Return an error if it's something other than delete
  293. // b. Deleted? remove the element
  294. // c. Edited?
  295. // 1. filename is `_index.md`, this is a section
  296. // 1. it's a page otherwise
  297. // 2. Change on non .md files
  298. // a. Try to find a corresponding `_index.md`
  299. // 1. Nothing? Return Ok
  300. // 2. Something? Update the page
  301. if is_md {
  302. // only delete if it was able to be added in the first place
  303. if !index.exists() && !path.exists() {
  304. return delete_element(site, path, is_section);
  305. }
  306. // Added another .md in a assets directory
  307. if index.exists() && path.exists() && !colocated_index {
  308. bail!(
  309. "Change on {:?} detected but only files named `index.md` with an optional language code are allowed",
  310. path.display()
  311. );
  312. } else if index.exists() && !path.exists() {
  313. // deleted the wrong .md, do nothing
  314. return Ok(());
  315. }
  316. if is_section {
  317. handle_section_editing(site, path)
  318. } else {
  319. handle_page_editing(site, path)
  320. }
  321. } else if index.exists() {
  322. handle_page_editing(site, &index)
  323. } else {
  324. Ok(())
  325. }
  326. }
  327. /// What happens when a template is changed
  328. pub fn after_template_change(site: &mut Site, path: &Path) -> Result<()> {
  329. site.tera.full_reload()?;
  330. let filename = path.file_name().unwrap().to_str().unwrap();
  331. match filename {
  332. "sitemap.xml" => site.render_sitemap(),
  333. "rss.xml" => site.render_rss_feed(site.library.read().unwrap().pages_values(), None),
  334. "robots.txt" => site.render_robots(),
  335. "single.html" | "list.html" => site.render_taxonomies(),
  336. "page.html" => {
  337. site.render_sections()?;
  338. site.render_orphan_pages()
  339. }
  340. "section.html" => site.render_sections(),
  341. "404.html" => site.render_404(),
  342. // Either the index or some unknown template changed
  343. // We can't really know what this change affects so rebuild all
  344. // the things
  345. _ => {
  346. // If we are updating a shortcode, re-render the markdown of all pages/site
  347. // because we have no clue which one needs rebuilding
  348. // TODO: look if there the shortcode is used in the markdown instead of re-rendering
  349. // everything
  350. if path.components().any(|x| x == Component::Normal("shortcodes".as_ref())) {
  351. site.render_markdown()?;
  352. }
  353. site.populate_sections();
  354. site.populate_taxonomies()?;
  355. site.render_sections()?;
  356. site.render_orphan_pages()?;
  357. site.render_taxonomies()
  358. }
  359. }
  360. }
  361. #[cfg(test)]
  362. mod tests {
  363. use std::collections::HashMap;
  364. use super::{
  365. find_page_front_matter_changes, find_section_front_matter_changes, PageChangesNeeded,
  366. SectionChangesNeeded,
  367. };
  368. use front_matter::{PageFrontMatter, SectionFrontMatter, SortBy};
  369. #[test]
  370. fn can_find_taxonomy_changes_in_page_frontmatter() {
  371. let mut taxonomies = HashMap::new();
  372. taxonomies.insert("tags".to_string(), vec!["a tag".to_string()]);
  373. let new = PageFrontMatter { taxonomies, ..PageFrontMatter::default() };
  374. let changes = find_page_front_matter_changes(&PageFrontMatter::default(), &new);
  375. assert_eq!(changes, vec![PageChangesNeeded::Taxonomies, PageChangesNeeded::Render]);
  376. }
  377. #[test]
  378. fn can_find_multiple_changes_in_page_frontmatter() {
  379. let mut taxonomies = HashMap::new();
  380. taxonomies.insert("categories".to_string(), vec!["a category".to_string()]);
  381. let current = PageFrontMatter { taxonomies, order: Some(1), ..PageFrontMatter::default() };
  382. let changes = find_page_front_matter_changes(&current, &PageFrontMatter::default());
  383. assert_eq!(
  384. changes,
  385. vec![PageChangesNeeded::Taxonomies, PageChangesNeeded::Sort, PageChangesNeeded::Render]
  386. );
  387. }
  388. #[test]
  389. fn can_find_sort_changes_in_section_frontmatter() {
  390. let new = SectionFrontMatter { sort_by: SortBy::Date, ..SectionFrontMatter::default() };
  391. let changes = find_section_front_matter_changes(&SectionFrontMatter::default(), &new);
  392. assert_eq!(changes, vec![SectionChangesNeeded::Sort, SectionChangesNeeded::Render]);
  393. }
  394. #[test]
  395. fn can_find_render_changes_in_section_frontmatter() {
  396. let new = SectionFrontMatter { render: false, ..SectionFrontMatter::default() };
  397. let changes = find_section_front_matter_changes(&SectionFrontMatter::default(), &new);
  398. assert_eq!(changes, vec![SectionChangesNeeded::Delete]);
  399. }
  400. #[test]
  401. fn can_find_paginate_by_changes_in_section_frontmatter() {
  402. let new = SectionFrontMatter { paginate_by: Some(10), ..SectionFrontMatter::default() };
  403. let changes = find_section_front_matter_changes(&SectionFrontMatter::default(), &new);
  404. assert_eq!(changes, vec![SectionChangesNeeded::RenderWithPages]);
  405. }
  406. }