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.

484 lines
18KB

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