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.

676 lines
24KB

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