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.

700 lines
24KB

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