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.

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