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.

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