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.

647 lines
23KB

  1. use std::collections::HashMap;
  2. use std::fs::{remove_dir_all, copy, create_dir_all};
  3. use std::path::{Path, PathBuf};
  4. use glob::glob;
  5. use tera::{Tera, Context};
  6. use walkdir::WalkDir;
  7. use errors::{Result, ResultExt};
  8. use config::{Config, get_config};
  9. use fs::{create_file, create_directory, ensure_directory_exists};
  10. use content::{Page, Section, Paginator, SortBy, Taxonomy, populate_previous_and_next_pages, sort_pages};
  11. use templates::{GUTENBERG_TERA, global_fns, render_redirect_template};
  12. use front_matter::InsertAnchor;
  13. #[derive(Debug)]
  14. pub struct Site {
  15. /// The base path of the gutenberg site
  16. pub base_path: PathBuf,
  17. /// The parsed config for the site
  18. pub config: Config,
  19. pub pages: HashMap<PathBuf, Page>,
  20. pub sections: HashMap<PathBuf, Section>,
  21. pub tera: Tera,
  22. live_reload: bool,
  23. output_path: PathBuf,
  24. static_path: PathBuf,
  25. pub tags: Option<Taxonomy>,
  26. pub categories: Option<Taxonomy>,
  27. /// A map of all .md files (section and pages) and their permalink
  28. /// We need that if there are relative links in the content that need to be resolved
  29. pub permalinks: HashMap<String, String>,
  30. }
  31. impl Site {
  32. /// Parse a site at the given path. Defaults to the current dir
  33. /// Passing in a path is only used in tests
  34. pub fn new<P: AsRef<Path>>(path: P, config_file: &str) -> Result<Site> {
  35. let path = path.as_ref();
  36. let tpl_glob = format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "templates/**/*.*ml");
  37. let mut tera = Tera::new(&tpl_glob).chain_err(|| "Error parsing templates")?;
  38. tera.extend(&GUTENBERG_TERA)?;
  39. let site = Site {
  40. base_path: path.to_path_buf(),
  41. config: get_config(path, config_file),
  42. pages: HashMap::new(),
  43. sections: HashMap::new(),
  44. tera: tera,
  45. live_reload: false,
  46. output_path: path.join("public"),
  47. static_path: path.join("static"),
  48. tags: None,
  49. categories: None,
  50. permalinks: HashMap::new(),
  51. };
  52. Ok(site)
  53. }
  54. /// What the function name says
  55. pub fn enable_live_reload(&mut self) {
  56. self.live_reload = true;
  57. }
  58. /// Get all the orphan (== without section) pages in the site
  59. pub fn get_all_orphan_pages(&self) -> Vec<&Page> {
  60. let mut pages_in_sections = vec![];
  61. let mut orphans = vec![];
  62. for s in self.sections.values() {
  63. pages_in_sections.extend(s.all_pages_path());
  64. }
  65. for page in self.pages.values() {
  66. if !pages_in_sections.contains(&page.file.path) {
  67. orphans.push(page);
  68. }
  69. }
  70. orphans
  71. }
  72. /// Used by tests to change the output path to a tmp dir
  73. #[doc(hidden)]
  74. pub fn set_output_path<P: AsRef<Path>>(&mut self, path: P) {
  75. self.output_path = path.as_ref().to_path_buf();
  76. }
  77. /// Reads all .md files in the `content` directory and create pages/sections
  78. /// out of them
  79. pub fn load(&mut self) -> Result<()> {
  80. let base_path = self.base_path.to_string_lossy().replace("\\", "/");
  81. let content_glob = format!("{}/{}", base_path, "content/**/*.md");
  82. for entry in glob(&content_glob).unwrap().filter_map(|e| e.ok()) {
  83. let path = entry.as_path();
  84. if path.file_name().unwrap() == "_index.md" {
  85. self.add_section(path, false)?;
  86. } else {
  87. self.add_page(path, false)?;
  88. }
  89. }
  90. // Insert a default index section if necessary so we don't need to create
  91. // a _index.md to render the index page
  92. let index_path = self.base_path.join("content").join("_index.md");
  93. if !self.sections.contains_key(&index_path) {
  94. let mut index_section = Section::default();
  95. index_section.permalink = self.config.make_permalink("");
  96. self.sections.insert(index_path, index_section);
  97. }
  98. // Silly thing needed to make the borrow checker happy
  99. let mut pages_insert_anchors = HashMap::new();
  100. for page in self.pages.values() {
  101. pages_insert_anchors.insert(page.file.path.clone(), self.find_parent_section_insert_anchor(&page.file.parent.clone()));
  102. }
  103. // TODO: make that parallel
  104. for page in self.pages.values_mut() {
  105. let insert_anchor = pages_insert_anchors[&page.file.path];
  106. page.render_markdown(&self.permalinks, &self.tera, &self.config, insert_anchor)?;
  107. }
  108. // TODO: make that parallel
  109. for section in self.sections.values_mut() {
  110. section.render_markdown(&self.permalinks, &self.tera, &self.config)?;
  111. }
  112. self.populate_sections();
  113. self.populate_tags_and_categories();
  114. self.tera.register_global_function("get_page", global_fns::make_get_page(&self.pages));
  115. self.tera.register_global_function("get_section", global_fns::make_get_section(&self.sections));
  116. self.register_get_url_fn();
  117. Ok(())
  118. }
  119. /// Separate fn as it can be called in the serve command
  120. pub fn register_get_url_fn(&mut self) {
  121. self.tera.register_global_function("get_url", global_fns::make_get_url(self.permalinks.clone()));
  122. }
  123. /// Add a page to the site
  124. /// The `render` parameter is used in the serve command, when rebuilding a page.
  125. /// If `true`, it will also render the markdown for that page
  126. /// Returns the previous page struct if there was one
  127. pub fn add_page(&mut self, path: &Path, render: bool) -> Result<Option<Page>> {
  128. let page = Page::from_file(&path, &self.config)?;
  129. self.permalinks.insert(page.file.relative.clone(), page.permalink.clone());
  130. let prev = self.pages.insert(page.file.path.clone(), page);
  131. if render {
  132. let insert_anchor = self.find_parent_section_insert_anchor(&self.pages[path].file.parent);
  133. let mut page = self.pages.get_mut(path).unwrap();
  134. page.render_markdown(&self.permalinks, &self.tera, &self.config, insert_anchor)?;
  135. }
  136. Ok(prev)
  137. }
  138. /// Add a section to the site
  139. /// The `render` parameter is used in the serve command, when rebuilding a page.
  140. /// If `true`, it will also render the markdown for that page
  141. /// Returns the previous section struct if there was one
  142. pub fn add_section(&mut self, path: &Path, render: bool) -> Result<Option<Section>> {
  143. let section = Section::from_file(path, &self.config)?;
  144. self.permalinks.insert(section.file.relative.clone(), section.permalink.clone());
  145. let prev = self.sections.insert(section.file.path.clone(), section);
  146. if render {
  147. let mut section = self.sections.get_mut(path).unwrap();
  148. section.render_markdown(&self.permalinks, &self.tera, &self.config)?;
  149. }
  150. Ok(prev)
  151. }
  152. /// Finds the insert_anchor for the parent section of the directory at `path`.
  153. /// Defaults to `AnchorInsert::None` if no parent section found
  154. pub fn find_parent_section_insert_anchor(&self, parent_path: &PathBuf) -> InsertAnchor {
  155. match self.sections.get(&parent_path.join("_index.md")) {
  156. Some(s) => s.meta.insert_anchor.unwrap(),
  157. None => InsertAnchor::None
  158. }
  159. }
  160. /// Find out the direct subsections of each subsection if there are some
  161. /// as well as the pages for each section
  162. pub fn populate_sections(&mut self) {
  163. let mut grandparent_paths = HashMap::new();
  164. for section in self.sections.values_mut() {
  165. if let Some(ref grand_parent) = section.file.grand_parent {
  166. grandparent_paths.entry(grand_parent.to_path_buf()).or_insert_with(|| vec![]).push(section.clone());
  167. }
  168. // Make sure the pages of a section are empty since we can call that many times on `serve`
  169. section.pages = vec![];
  170. section.ignored_pages = vec![];
  171. }
  172. for page in self.pages.values() {
  173. let parent_section_path = page.file.parent.join("_index.md");
  174. if self.sections.contains_key(&parent_section_path) {
  175. self.sections.get_mut(&parent_section_path).unwrap().pages.push(page.clone());
  176. }
  177. }
  178. for section in self.sections.values_mut() {
  179. match grandparent_paths.get(&section.file.parent) {
  180. Some(paths) => section.subsections.extend(paths.clone()),
  181. None => continue,
  182. };
  183. }
  184. self.sort_sections_pages(None);
  185. }
  186. /// Sorts the pages of the section at the given path
  187. /// By default will sort all sections but can be made to only sort a single one by providing a path
  188. pub fn sort_sections_pages(&mut self, only: Option<&Path>) {
  189. for (path, section) in &mut self.sections {
  190. if let Some(p) = only {
  191. if p != path {
  192. continue;
  193. }
  194. }
  195. let (sorted_pages, cannot_be_sorted_pages) = sort_pages(section.pages.clone(), section.meta.sort_by());
  196. section.pages = populate_previous_and_next_pages(&sorted_pages);
  197. section.ignored_pages = cannot_be_sorted_pages;
  198. }
  199. }
  200. /// Find all the tags and categories if it's asked in the config
  201. pub fn populate_tags_and_categories(&mut self) {
  202. let generate_tags_pages = self.config.generate_tags_pages.unwrap();
  203. let generate_categories_pages = self.config.generate_categories_pages.unwrap();
  204. if !generate_tags_pages && !generate_categories_pages {
  205. return;
  206. }
  207. // TODO: can we pass a reference?
  208. let (tags, categories) = Taxonomy::find_tags_and_categories(
  209. self.pages.values().cloned().collect::<Vec<_>>()
  210. );
  211. if generate_tags_pages {
  212. self.tags = Some(tags);
  213. }
  214. if generate_categories_pages {
  215. self.categories = Some(categories);
  216. }
  217. }
  218. /// Inject live reload script tag if in live reload mode
  219. fn inject_livereload(&self, html: String) -> String {
  220. if self.live_reload {
  221. return html.replace(
  222. "</body>",
  223. r#"<script src="/livereload.js?port=1112&mindelay=10"></script></body>"#
  224. );
  225. }
  226. html
  227. }
  228. /// Copy static file to public directory.
  229. pub fn copy_static_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
  230. let relative_path = path.as_ref().strip_prefix(&self.static_path).unwrap();
  231. let target_path = self.output_path.join(relative_path);
  232. if let Some(parent_directory) = target_path.parent() {
  233. create_dir_all(parent_directory)?;
  234. }
  235. copy(path.as_ref(), &target_path)?;
  236. Ok(())
  237. }
  238. /// Copy the content of the `static` folder into the `public` folder
  239. pub fn copy_static_directory(&self) -> Result<()> {
  240. for entry in WalkDir::new(&self.static_path).into_iter().filter_map(|e| e.ok()) {
  241. let relative_path = entry.path().strip_prefix(&self.static_path).unwrap();
  242. let target_path = self.output_path.join(relative_path);
  243. if entry.path().is_dir() {
  244. if !target_path.exists() {
  245. create_directory(&target_path)?;
  246. }
  247. } else {
  248. let entry_fullpath = self.base_path.join(entry.path());
  249. self.copy_static_file(entry_fullpath)?;
  250. }
  251. }
  252. Ok(())
  253. }
  254. /// Deletes the `public` directory if it exists
  255. pub fn clean(&self) -> Result<()> {
  256. if self.output_path.exists() {
  257. // Delete current `public` directory so we can start fresh
  258. remove_dir_all(&self.output_path).chain_err(|| "Couldn't delete `public` directory")?;
  259. }
  260. Ok(())
  261. }
  262. /// Renders a single content page
  263. pub fn render_page(&self, page: &Page, section: Option<&Section>) -> Result<()> {
  264. ensure_directory_exists(&self.output_path)?;
  265. // Copy the nesting of the content directory if we have sections for that page
  266. let mut current_path = self.output_path.to_path_buf();
  267. for component in page.path.split('/') {
  268. current_path.push(component);
  269. if !current_path.exists() {
  270. create_directory(&current_path)?;
  271. }
  272. }
  273. // Make sure the folder exists
  274. create_directory(&current_path)?;
  275. // Finally, create a index.html file there with the page rendered
  276. let output = page.render_html(&self.tera, &self.config, section)?;
  277. create_file(&current_path.join("index.html"), &self.inject_livereload(output))?;
  278. // Copy any asset we found previously into the same directory as the index.html
  279. for asset in &page.assets {
  280. let asset_path = asset.as_path();
  281. copy(&asset_path, &current_path.join(asset_path.file_name().unwrap()))?;
  282. }
  283. Ok(())
  284. }
  285. /// Deletes the `public` directory and builds the site
  286. pub fn build(&self) -> Result<()> {
  287. self.clean()?;
  288. // Render aliases first to allow overwriting
  289. self.render_aliases()?;
  290. self.render_sections()?;
  291. self.render_orphan_pages()?;
  292. self.render_sitemap()?;
  293. if self.config.generate_rss.unwrap() {
  294. self.render_rss_feed()?;
  295. }
  296. self.render_robots()?;
  297. // `render_categories` and `render_tags` will check whether the config allows
  298. // them to render or not
  299. self.render_categories()?;
  300. self.render_tags()?;
  301. self.copy_static_directory()
  302. }
  303. pub fn render_aliases(&self) -> Result<()> {
  304. for page in self.pages.values() {
  305. if let Some(ref aliases) = page.meta.aliases {
  306. for alias in aliases {
  307. let mut output_path = self.output_path.to_path_buf();
  308. for component in alias.split("/") {
  309. output_path.push(&component);
  310. if !output_path.exists() {
  311. create_directory(&output_path)?;
  312. }
  313. }
  314. create_file(&output_path.join("index.html"), &render_redirect_template(&page.permalink, &self.tera)?)?;
  315. }
  316. }
  317. }
  318. Ok(())
  319. }
  320. /// Renders robots.txt
  321. pub fn render_robots(&self) -> Result<()> {
  322. ensure_directory_exists(&self.output_path)?;
  323. create_file(
  324. &self.output_path.join("robots.txt"),
  325. &self.tera.render("robots.txt", &Context::new())?
  326. )
  327. }
  328. /// Renders all categories and the single category pages if there are some
  329. pub fn render_categories(&self) -> Result<()> {
  330. if let Some(ref categories) = self.categories {
  331. self.render_taxonomy(categories)?;
  332. }
  333. Ok(())
  334. }
  335. /// Renders all tags and the single tag pages if there are some
  336. pub fn render_tags(&self) -> Result<()> {
  337. if let Some(ref tags) = self.tags {
  338. self.render_taxonomy(tags)?;
  339. }
  340. Ok(())
  341. }
  342. fn render_taxonomy(&self, taxonomy: &Taxonomy) -> Result<()> {
  343. if taxonomy.items.is_empty() {
  344. return Ok(())
  345. }
  346. ensure_directory_exists(&self.output_path)?;
  347. let output_path = self.output_path.join(&taxonomy.get_list_name());
  348. let list_output = taxonomy.render_list(&self.tera, &self.config)?;
  349. create_directory(&output_path)?;
  350. create_file(&output_path.join("index.html"), &self.inject_livereload(list_output))?;
  351. for item in &taxonomy.items {
  352. let single_output = taxonomy.render_single_item(item, &self.tera, &self.config)?;
  353. create_directory(&output_path.join(&item.slug))?;
  354. create_file(
  355. &output_path.join(&item.slug).join("index.html"),
  356. &self.inject_livereload(single_output)
  357. )?;
  358. }
  359. Ok(())
  360. }
  361. /// What it says on the tin
  362. pub fn render_sitemap(&self) -> Result<()> {
  363. ensure_directory_exists(&self.output_path)?;
  364. let mut context = Context::new();
  365. context.add("pages", &self.pages.values().collect::<Vec<&Page>>());
  366. context.add("sections", &self.sections.values().collect::<Vec<&Section>>());
  367. let mut categories = vec![];
  368. if let Some(ref c) = self.categories {
  369. let name = c.get_list_name();
  370. categories.push(self.config.make_permalink(&name));
  371. for item in &c.items {
  372. categories.push(
  373. self.config.make_permalink(&format!("{}/{}", &name, item.slug))
  374. );
  375. }
  376. }
  377. context.add("categories", &categories);
  378. let mut tags = vec![];
  379. if let Some(ref t) = self.tags {
  380. let name = t.get_list_name();
  381. tags.push(self.config.make_permalink(&name));
  382. for item in &t.items {
  383. tags.push(
  384. self.config.make_permalink(&format!("{}/{}", &name, item.slug))
  385. );
  386. }
  387. }
  388. context.add("tags", &tags);
  389. let sitemap = self.tera.render("sitemap.xml", &context)?;
  390. create_file(&self.output_path.join("sitemap.xml"), &sitemap)?;
  391. Ok(())
  392. }
  393. pub fn render_rss_feed(&self) -> Result<()> {
  394. ensure_directory_exists(&self.output_path)?;
  395. let mut context = Context::new();
  396. let pages = self.pages.values()
  397. .filter(|p| p.meta.date.is_some())
  398. .take(self.config.rss_limit.unwrap()) // limit to the last n elements
  399. .cloned()
  400. .collect::<Vec<Page>>();
  401. // Don't generate a RSS feed if none of the pages has a date
  402. if pages.is_empty() {
  403. return Ok(());
  404. }
  405. context.add("last_build_date", &pages[0].meta.date);
  406. let (sorted_pages, _) = sort_pages(pages, SortBy::Date);
  407. context.add("pages", &sorted_pages);
  408. context.add("config", &self.config);
  409. let rss_feed_url = if self.config.base_url.ends_with('/') {
  410. format!("{}{}", self.config.base_url, "rss.xml")
  411. } else {
  412. format!("{}/{}", self.config.base_url, "rss.xml")
  413. };
  414. context.add("feed_url", &rss_feed_url);
  415. let sitemap = self.tera.render("rss.xml", &context)?;
  416. create_file(&self.output_path.join("rss.xml"), &sitemap)?;
  417. Ok(())
  418. }
  419. /// Create a hashmap of paths to section
  420. /// For example `content/posts/_index.md` key will be `posts`
  421. fn get_sections_map(&self) -> HashMap<String, Section> {
  422. self.sections
  423. .values()
  424. .map(|s| (s.file.components.join("/"), s.clone()))
  425. .collect()
  426. }
  427. /// Renders a single section
  428. pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> {
  429. ensure_directory_exists(&self.output_path)?;
  430. let public = self.output_path.clone();
  431. let mut output_path = public.to_path_buf();
  432. for component in &section.file.components {
  433. output_path.push(component);
  434. if !output_path.exists() {
  435. create_directory(&output_path)?;
  436. }
  437. }
  438. if render_pages {
  439. for page in &section.pages {
  440. self.render_page(page, Some(section))?;
  441. }
  442. }
  443. if !section.meta.should_render() {
  444. return Ok(());
  445. }
  446. if section.meta.is_paginated() {
  447. self.render_paginated(&output_path, section)?;
  448. } else {
  449. let output = section.render_html(
  450. if section.is_index() { self.get_sections_map() } else { HashMap::new() },
  451. &self.tera,
  452. &self.config,
  453. )?;
  454. create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
  455. }
  456. Ok(())
  457. }
  458. pub fn render_index(&self) -> Result<()> {
  459. self.render_section(&self.sections[&self.base_path.join("content").join("_index.md")], false)
  460. }
  461. /// Renders all sections
  462. pub fn render_sections(&self) -> Result<()> {
  463. for section in self.sections.values() {
  464. self.render_section(section, true)?;
  465. }
  466. Ok(())
  467. }
  468. /// Renders all pages that do not belong to any sections
  469. pub fn render_orphan_pages(&self) -> Result<()> {
  470. ensure_directory_exists(&self.output_path)?;
  471. for page in self.get_all_orphan_pages() {
  472. self.render_page(page, None)?;
  473. }
  474. Ok(())
  475. }
  476. /// Renders a list of pages when the section/index is wanting pagination.
  477. fn render_paginated(&self, output_path: &Path, section: &Section) -> Result<()> {
  478. ensure_directory_exists(&self.output_path)?;
  479. let paginate_path = match section.meta.paginate_path {
  480. Some(ref s) => s.clone(),
  481. None => unreachable!()
  482. };
  483. let paginator = Paginator::new(&section.pages, section);
  484. for (i, pager) in paginator.pagers.iter().enumerate() {
  485. let folder_path = output_path.join(&paginate_path);
  486. let page_path = folder_path.join(&format!("{}", i + 1));
  487. create_directory(&folder_path)?;
  488. create_directory(&page_path)?;
  489. let output = paginator.render_pager(pager, self)?;
  490. if i > 0 {
  491. create_file(&page_path.join("index.html"), &self.inject_livereload(output))?;
  492. } else {
  493. create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
  494. create_file(&page_path.join("index.html"), &render_redirect_template(&section.permalink, &self.tera)?)?;
  495. }
  496. }
  497. Ok(())
  498. }
  499. }
  500. /// Resolves an internal link (of the `./posts/something.md#hey` sort) to its absolute link
  501. pub fn resolve_internal_link(link: &str, permalinks: &HashMap<String, String>) -> Result<String> {
  502. // First we remove the ./ since that's gutenberg specific
  503. let clean_link = link.replacen("./", "", 1);
  504. // Then we remove any potential anchor
  505. // parts[0] will be the file path and parts[1] the anchor if present
  506. let parts = clean_link.split('#').collect::<Vec<_>>();
  507. match permalinks.get(parts[0]) {
  508. Some(p) => {
  509. if parts.len() > 1 {
  510. Ok(format!("{}#{}", p, parts[1]))
  511. } else {
  512. Ok(p.to_string())
  513. }
  514. },
  515. None => bail!(format!("Relative link {} not found.", link)),
  516. }
  517. }
  518. #[cfg(test)]
  519. mod tests {
  520. use std::collections::HashMap;
  521. use super::resolve_internal_link;
  522. #[test]
  523. fn can_resolve_valid_internal_link() {
  524. let mut permalinks = HashMap::new();
  525. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
  526. let res = resolve_internal_link("./pages/about.md", &permalinks).unwrap();
  527. assert_eq!(res, "https://vincent.is/about");
  528. }
  529. #[test]
  530. fn can_resolve_internal_links_with_anchors() {
  531. let mut permalinks = HashMap::new();
  532. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
  533. let res = resolve_internal_link("./pages/about.md#hello", &permalinks).unwrap();
  534. assert_eq!(res, "https://vincent.is/about#hello");
  535. }
  536. #[test]
  537. fn errors_resolve_inexistant_internal_link() {
  538. let res = resolve_internal_link("./pages/about.md#hello", &HashMap::new());
  539. assert!(res.is_err());
  540. }
  541. }