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.

929 lines
33KB

  1. extern crate tera;
  2. extern crate rayon;
  3. extern crate glob;
  4. extern crate serde;
  5. #[macro_use]
  6. extern crate serde_derive;
  7. extern crate sass_rs;
  8. #[macro_use]
  9. extern crate errors;
  10. extern crate config;
  11. extern crate utils;
  12. extern crate front_matter;
  13. extern crate templates;
  14. extern crate pagination;
  15. extern crate taxonomies;
  16. extern crate content;
  17. extern crate search;
  18. extern crate imageproc;
  19. #[cfg(test)]
  20. extern crate tempfile;
  21. use std::collections::{HashMap, HashSet};
  22. use std::fs::{create_dir_all, remove_dir_all, copy};
  23. use std::mem;
  24. use std::path::{Path, PathBuf};
  25. use std::sync::{Arc, Mutex};
  26. use glob::glob;
  27. use tera::{Tera, Context};
  28. use sass_rs::{Options as SassOptions, OutputStyle, compile_file};
  29. use errors::{Result, ResultExt};
  30. use config::{Config, get_config};
  31. use utils::fs::{create_file, copy_directory, create_directory, ensure_directory_exists};
  32. use utils::templates::{render_template, rewrite_theme_paths};
  33. use utils::net::get_available_port;
  34. use content::{Page, Section, populate_siblings, sort_pages, sort_pages_by_date};
  35. use templates::{GUTENBERG_TERA, global_fns, render_redirect_template};
  36. use front_matter::{InsertAnchor};
  37. use taxonomies::{Taxonomy, find_taxonomies};
  38. use pagination::Paginator;
  39. use rayon::prelude::*;
  40. /// The sitemap only needs links and potentially date so we trim down
  41. /// all pages to only that
  42. #[derive(Debug, Serialize)]
  43. struct SitemapEntry {
  44. permalink: String,
  45. date: Option<String>,
  46. }
  47. impl SitemapEntry {
  48. pub fn new(permalink: String, date: Option<String>) -> SitemapEntry {
  49. SitemapEntry { permalink, date }
  50. }
  51. }
  52. #[derive(Debug)]
  53. pub struct Site {
  54. /// The base path of the gutenberg site
  55. pub base_path: PathBuf,
  56. /// The parsed config for the site
  57. pub config: Config,
  58. pub pages: HashMap<PathBuf, Page>,
  59. pub sections: HashMap<PathBuf, Section>,
  60. pub tera: Tera,
  61. imageproc: Arc<Mutex<imageproc::Processor>>,
  62. // the live reload port to be used if there is one
  63. pub live_reload: Option<u16>,
  64. pub output_path: PathBuf,
  65. content_path: PathBuf,
  66. pub static_path: PathBuf,
  67. pub taxonomies: Vec<Taxonomy>,
  68. /// A map of all .md files (section and pages) and their permalink
  69. /// We need that if there are relative links in the content that need to be resolved
  70. pub permalinks: HashMap<String, String>,
  71. }
  72. impl Site {
  73. /// Parse a site at the given path. Defaults to the current dir
  74. /// Passing in a path is only used in tests
  75. pub fn new<P: AsRef<Path>>(path: P, config_file: &str) -> Result<Site> {
  76. let path = path.as_ref();
  77. let mut config = get_config(path, config_file);
  78. let tpl_glob = format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "templates/**/*.*ml");
  79. // Only parsing as we might be extending templates from themes and that would error
  80. // as we haven't loaded them yet
  81. let mut tera = Tera::parse(&tpl_glob).chain_err(|| "Error parsing templates")?;
  82. if let Some(theme) = config.theme.clone() {
  83. // Grab data from the extra section of the theme
  84. config.merge_with_theme(&path.join("themes").join(&theme).join("theme.toml"))?;
  85. // Test that the templates folder exist for that theme
  86. let theme_path = path.join("themes").join(&theme);
  87. if !theme_path.join("templates").exists() {
  88. bail!("Theme `{}` is missing a templates folder", theme);
  89. }
  90. let theme_tpl_glob = format!(
  91. "{}/{}",
  92. path.to_string_lossy().replace("\\", "/"),
  93. format!("themes/{}/templates/**/*.*ml", theme)
  94. );
  95. let mut tera_theme = Tera::parse(&theme_tpl_glob).chain_err(|| "Error parsing templates from themes")?;
  96. rewrite_theme_paths(&mut tera_theme, &theme);
  97. // TODO: same as above
  98. if theme_path.join("templates").join("robots.txt").exists() {
  99. tera_theme.add_template_file(theme_path.join("templates").join("robots.txt"), None)?;
  100. }
  101. tera_theme.build_inheritance_chains()?;
  102. tera.extend(&tera_theme)?;
  103. }
  104. tera.extend(&GUTENBERG_TERA)?;
  105. // the `extend` above already does it but hey
  106. tera.build_inheritance_chains()?;
  107. // TODO: Tera doesn't use globset right now so we can load the robots.txt as part
  108. // of the glob above, therefore we load it manually if it exists.
  109. if path.join("templates").join("robots.txt").exists() {
  110. tera.add_template_file(path.join("templates").join("robots.txt"), Some("robots.txt"))?;
  111. }
  112. let content_path = path.join("content");
  113. let static_path = path.join("static");
  114. let imageproc = imageproc::Processor::new(content_path.clone(), &static_path, &config.base_url);
  115. let site = Site {
  116. base_path: path.to_path_buf(),
  117. config,
  118. tera,
  119. pages: HashMap::new(),
  120. sections: HashMap::new(),
  121. imageproc: Arc::new(Mutex::new(imageproc)),
  122. live_reload: None,
  123. output_path: path.join("public"),
  124. content_path,
  125. static_path,
  126. taxonomies: Vec::new(),
  127. permalinks: HashMap::new(),
  128. };
  129. Ok(site)
  130. }
  131. /// The index section is ALWAYS at that path
  132. pub fn index_section_path(&self) -> PathBuf {
  133. self.content_path.join("_index.md")
  134. }
  135. pub fn enable_live_reload(&mut self) {
  136. self.live_reload = get_available_port();
  137. }
  138. /// Get all the orphan (== without section) pages in the site
  139. pub fn get_all_orphan_pages(&self) -> Vec<&Page> {
  140. let pages_in_sections = self.sections
  141. .values()
  142. .flat_map(|s| s.all_pages_path())
  143. .collect::<HashSet<_>>();
  144. self.pages
  145. .values()
  146. .filter(|page| !pages_in_sections.contains(&page.file.path))
  147. .collect()
  148. }
  149. pub fn set_base_url(&mut self, base_url: String) {
  150. let mut imageproc = self.imageproc.lock().unwrap();
  151. imageproc.set_base_url(&base_url);
  152. self.config.base_url = base_url;
  153. }
  154. pub fn set_output_path<P: AsRef<Path>>(&mut self, path: P) {
  155. self.output_path = path.as_ref().to_path_buf();
  156. }
  157. /// Reads all .md files in the `content` directory and create pages/sections
  158. /// out of them
  159. pub fn load(&mut self) -> Result<()> {
  160. let base_path = self.base_path.to_string_lossy().replace("\\", "/");
  161. let content_glob = format!("{}/{}", base_path, "content/**/*.md");
  162. let (section_entries, page_entries): (Vec<_>, Vec<_>) = glob(&content_glob)
  163. .unwrap()
  164. .filter_map(|e| e.ok())
  165. .filter(|e| !e.as_path().file_name().unwrap().to_str().unwrap().starts_with('.'))
  166. .partition(|entry| entry.as_path().file_name().unwrap() == "_index.md");
  167. let sections = {
  168. let config = &self.config;
  169. section_entries
  170. .into_par_iter()
  171. .map(|entry| {
  172. let path = entry.as_path();
  173. Section::from_file(path, config)
  174. })
  175. .collect::<Vec<_>>()
  176. };
  177. let pages = {
  178. let config = &self.config;
  179. page_entries
  180. .into_par_iter()
  181. .map(|entry| {
  182. let path = entry.as_path();
  183. Page::from_file(path, config)
  184. })
  185. .collect::<Vec<_>>()
  186. };
  187. // Kinda duplicated code for add_section/add_page but necessary to do it that
  188. // way because of the borrow checker
  189. for section in sections {
  190. let s = section?;
  191. self.add_section(s, false)?;
  192. }
  193. // Insert a default index section if necessary so we don't need to create
  194. // a _index.md to render the index page at the root of the site
  195. let index_path = self.index_section_path();
  196. if let Some(ref index_section) = self.sections.get(&index_path) {
  197. if self.config.build_search_index && !index_section.meta.in_search_index {
  198. bail!(
  199. "You have enabled search in the config but disabled it in the index section: \
  200. either turn off the search in the config or remote `in_search_index = true` from the \
  201. section front-matter."
  202. )
  203. }
  204. }
  205. // Not in else because of borrow checker
  206. if !self.sections.contains_key(&index_path) {
  207. let mut index_section = Section::default();
  208. index_section.permalink = self.config.make_permalink("");
  209. index_section.file.parent = self.content_path.clone();
  210. index_section.file.relative = "_index.md".to_string();
  211. self.sections.insert(index_path, index_section);
  212. }
  213. let mut pages_insert_anchors = HashMap::new();
  214. for page in pages {
  215. let p = page?;
  216. pages_insert_anchors.insert(p.file.path.clone(), self.find_parent_section_insert_anchor(&p.file.parent.clone()));
  217. self.add_page(p, false)?;
  218. }
  219. self.register_early_global_fns();
  220. self.render_markdown()?;
  221. self.populate_sections();
  222. self.populate_taxonomies()?;
  223. self.register_tera_global_fns();
  224. Ok(())
  225. }
  226. /// Render the markdown of all pages/sections
  227. /// Used in a build and in `serve` if a shortcode has changed
  228. pub fn render_markdown(&mut self) -> Result<()> {
  229. // Another silly thing needed to not borrow &self in parallel and
  230. // make the borrow checker happy
  231. let permalinks = &self.permalinks;
  232. let tera = &self.tera;
  233. let config = &self.config;
  234. let base_path = &self.base_path;
  235. // This is needed in the first place because of silly borrow checker
  236. let mut pages_insert_anchors = HashMap::new();
  237. for p in self.pages.values() {
  238. pages_insert_anchors.insert(p.file.path.clone(), self.find_parent_section_insert_anchor(&p.file.parent.clone()));
  239. }
  240. self.pages.par_iter_mut()
  241. .map(|(_, page)| {
  242. let insert_anchor = pages_insert_anchors[&page.file.path];
  243. page.render_markdown(permalinks, tera, config, base_path, insert_anchor)
  244. })
  245. .collect::<Result<()>>()?;
  246. self.sections.par_iter_mut()
  247. .map(|(_, section)| section.render_markdown(permalinks, tera, config, base_path))
  248. .collect::<Result<()>>()?;
  249. Ok(())
  250. }
  251. /// Adds global fns that are to be available to shortcodes while rendering markdown
  252. pub fn register_early_global_fns(&mut self) {
  253. self.tera.register_function(
  254. "get_url", global_fns::make_get_url(self.permalinks.clone(), self.config.clone()),
  255. );
  256. self.tera.register_function(
  257. "resize_image", global_fns::make_resize_image(self.imageproc.clone()),
  258. );
  259. }
  260. pub fn register_tera_global_fns(&mut self) {
  261. self.tera.register_function("trans", global_fns::make_trans(self.config.clone()));
  262. self.tera.register_function("get_page", global_fns::make_get_page(&self.pages));
  263. self.tera.register_function("get_section", global_fns::make_get_section(&self.sections));
  264. self.tera.register_function(
  265. "get_taxonomy",
  266. global_fns::make_get_taxonomy(self.taxonomies.clone()),
  267. );
  268. self.tera.register_function(
  269. "get_taxonomy_url",
  270. global_fns::make_get_taxonomy_url(self.taxonomies.clone()),
  271. );
  272. }
  273. /// Add a page to the site
  274. /// The `render` parameter is used in the serve command, when rebuilding a page.
  275. /// If `true`, it will also render the markdown for that page
  276. /// Returns the previous page struct if there was one at the same path
  277. pub fn add_page(&mut self, page: Page, render: bool) -> Result<Option<Page>> {
  278. let path = page.file.path.clone();
  279. self.permalinks.insert(page.file.relative.clone(), page.permalink.clone());
  280. let prev = self.pages.insert(page.file.path.clone(), page);
  281. if render {
  282. let insert_anchor = self.find_parent_section_insert_anchor(&self.pages[&path].file.parent);
  283. let page = self.pages.get_mut(&path).unwrap();
  284. page.render_markdown(&self.permalinks, &self.tera, &self.config, &self.base_path, insert_anchor)?;
  285. }
  286. Ok(prev)
  287. }
  288. /// Add a section to the site
  289. /// The `render` parameter is used in the serve command, when rebuilding a page.
  290. /// If `true`, it will also render the markdown for that page
  291. /// Returns the previous section struct if there was one at the same path
  292. pub fn add_section(&mut self, section: Section, render: bool) -> Result<Option<Section>> {
  293. let path = section.file.path.clone();
  294. self.permalinks.insert(section.file.relative.clone(), section.permalink.clone());
  295. let prev = self.sections.insert(section.file.path.clone(), section);
  296. if render {
  297. let section = self.sections.get_mut(&path).unwrap();
  298. section.render_markdown(&self.permalinks, &self.tera, &self.config, &self.base_path)?;
  299. }
  300. Ok(prev)
  301. }
  302. /// Finds the insert_anchor for the parent section of the directory at `path`.
  303. /// Defaults to `AnchorInsert::None` if no parent section found
  304. pub fn find_parent_section_insert_anchor(&self, parent_path: &PathBuf) -> InsertAnchor {
  305. match self.sections.get(&parent_path.join("_index.md")) {
  306. Some(s) => s.meta.insert_anchor_links,
  307. None => InsertAnchor::None
  308. }
  309. }
  310. /// Find out the direct subsections of each subsection if there are some
  311. /// as well as the pages for each section
  312. pub fn populate_sections(&mut self) {
  313. let mut grandparent_paths: HashMap<PathBuf, Vec<PathBuf>> = HashMap::new();
  314. for section in self.sections.values_mut() {
  315. if let Some(ref grand_parent) = section.file.grand_parent {
  316. grandparent_paths
  317. .entry(grand_parent.to_path_buf())
  318. .or_insert_with(|| vec![])
  319. .push(section.file.path.clone());
  320. }
  321. // Make sure the pages of a section are empty since we can call that many times on `serve`
  322. section.pages = vec![];
  323. section.ignored_pages = vec![];
  324. }
  325. for page in self.pages.values() {
  326. let parent_section_path = page.file.parent.join("_index.md");
  327. if self.sections.contains_key(&parent_section_path) {
  328. // TODO: use references instead of cloning to avoid having to call populate_section on
  329. // content change
  330. self.sections.get_mut(&parent_section_path).unwrap().pages.push(page.clone());
  331. }
  332. }
  333. self.sort_sections_pages(None);
  334. // TODO: remove this clone
  335. let sections = self.sections.clone();
  336. for section in self.sections.values_mut() {
  337. if let Some(paths) = grandparent_paths.get(&section.file.parent) {
  338. section.subsections = paths
  339. .iter()
  340. .map(|p| sections[p].clone())
  341. .collect::<Vec<_>>();
  342. section.subsections
  343. .sort_by(|a, b| a.meta.weight.cmp(&b.meta.weight));
  344. }
  345. }
  346. }
  347. /// Sorts the pages of the section at the given path
  348. /// By default will sort all sections but can be made to only sort a single one by providing a path
  349. pub fn sort_sections_pages(&mut self, only: Option<&Path>) {
  350. for (path, section) in &mut self.sections {
  351. if let Some(p) = only {
  352. if p != path {
  353. continue;
  354. }
  355. }
  356. let pages = mem::replace(&mut section.pages, vec![]);
  357. let (sorted_pages, cannot_be_sorted_pages) = sort_pages(pages, section.meta.sort_by);
  358. section.pages = populate_siblings(&sorted_pages, section.meta.sort_by);
  359. section.ignored_pages = cannot_be_sorted_pages;
  360. }
  361. }
  362. /// Find all the tags and categories if it's asked in the config
  363. pub fn populate_taxonomies(&mut self) -> Result<()> {
  364. if self.config.taxonomies.is_empty() {
  365. return Ok(());
  366. }
  367. self.taxonomies = find_taxonomies(
  368. &self.config,
  369. self.pages
  370. .values()
  371. .filter(|p| !p.is_draft())
  372. .collect::<Vec<_>>(),
  373. )?;
  374. Ok(())
  375. }
  376. /// Inject live reload script tag if in live reload mode
  377. fn inject_livereload(&self, html: String) -> String {
  378. if let Some(port) = self.live_reload {
  379. return html.replace(
  380. "</body>",
  381. &format!(r#"<script src="/livereload.js?port={}&mindelay=10"></script></body>"#, port),
  382. );
  383. }
  384. html
  385. }
  386. /// Copy the main `static` folder and the theme `static` folder if a theme is used
  387. pub fn copy_static_directories(&self) -> Result<()> {
  388. // The user files will overwrite the theme files
  389. if let Some(ref theme) = self.config.theme {
  390. copy_directory(
  391. &self.base_path.join("themes").join(theme).join("static"),
  392. &self.output_path,
  393. )?;
  394. }
  395. // We're fine with missing static folders
  396. if self.static_path.exists() {
  397. copy_directory(&self.static_path, &self.output_path)?;
  398. }
  399. Ok(())
  400. }
  401. pub fn num_img_ops(&self) -> usize {
  402. let imageproc = self.imageproc.lock().unwrap();
  403. imageproc.num_img_ops()
  404. }
  405. pub fn process_images(&self) -> Result<()> {
  406. let mut imageproc = self.imageproc.lock().unwrap();
  407. imageproc.prune()?;
  408. imageproc.do_process()
  409. }
  410. /// Deletes the `public` directory if it exists
  411. pub fn clean(&self) -> Result<()> {
  412. if self.output_path.exists() {
  413. // Delete current `public` directory so we can start fresh
  414. remove_dir_all(&self.output_path).chain_err(|| "Couldn't delete output directory")?;
  415. }
  416. Ok(())
  417. }
  418. /// Renders a single content page
  419. pub fn render_page(&self, page: &Page) -> Result<()> {
  420. ensure_directory_exists(&self.output_path)?;
  421. // Copy the nesting of the content directory if we have sections for that page
  422. let mut current_path = self.output_path.to_path_buf();
  423. for component in page.path.split('/') {
  424. current_path.push(component);
  425. if !current_path.exists() {
  426. create_directory(&current_path)?;
  427. }
  428. }
  429. // Make sure the folder exists
  430. create_directory(&current_path)?;
  431. // Finally, create a index.html file there with the page rendered
  432. let output = page.render_html(&self.tera, &self.config)?;
  433. create_file(&current_path.join("index.html"), &self.inject_livereload(output))?;
  434. // Copy any asset we found previously into the same directory as the index.html
  435. for asset in &page.assets {
  436. let asset_path = asset.as_path();
  437. copy(&asset_path, &current_path.join(asset_path.file_name().unwrap()))?;
  438. }
  439. Ok(())
  440. }
  441. /// Deletes the `public` directory and builds the site
  442. pub fn build(&self) -> Result<()> {
  443. self.clean()?;
  444. // Render aliases first to allow overwriting
  445. self.render_aliases()?;
  446. self.render_sections()?;
  447. self.render_orphan_pages()?;
  448. self.render_sitemap()?;
  449. if self.config.generate_rss {
  450. self.render_rss_feed(self.pages.values().collect(), None)?;
  451. }
  452. self.render_404()?;
  453. self.render_robots()?;
  454. self.render_taxonomies()?;
  455. if let Some(ref theme) = self.config.theme {
  456. let theme_path = self.base_path.join("themes").join(theme);
  457. if theme_path.join("sass").exists() {
  458. self.compile_sass(&theme_path)?;
  459. }
  460. }
  461. if self.config.compile_sass {
  462. self.compile_sass(&self.base_path)?;
  463. }
  464. self.process_images()?;
  465. self.copy_static_directories()?;
  466. if self.config.build_search_index {
  467. self.build_search_index()?;
  468. }
  469. Ok(())
  470. }
  471. pub fn build_search_index(&self) -> Result<()> {
  472. // index first
  473. create_file(
  474. &self.output_path.join(&format!("search_index.{}.js", self.config.default_language)),
  475. &format!(
  476. "window.searchIndex = {};",
  477. search::build_index(&self.sections, &self.config.default_language)?
  478. ),
  479. )?;
  480. // then elasticlunr.min.js
  481. create_file(
  482. &self.output_path.join("elasticlunr.min.js"),
  483. search::ELASTICLUNR_JS,
  484. )?;
  485. Ok(())
  486. }
  487. pub fn compile_sass(&self, base_path: &Path) -> Result<()> {
  488. ensure_directory_exists(&self.output_path)?;
  489. let sass_path = {
  490. let mut sass_path = PathBuf::from(base_path);
  491. sass_path.push("sass");
  492. sass_path
  493. };
  494. let mut options = SassOptions::default();
  495. options.output_style = OutputStyle::Compressed;
  496. let mut compiled_paths = self.compile_sass_glob(&sass_path, "scss", &options.clone())?;
  497. options.indented_syntax = true;
  498. compiled_paths.extend(self.compile_sass_glob(&sass_path, "sass", &options)?);
  499. compiled_paths.sort();
  500. for window in compiled_paths.windows(2) {
  501. if window[0].1 == window[1].1 {
  502. bail!(
  503. "SASS path conflict: \"{}\" and \"{}\" both compile to \"{}\"",
  504. window[0].0.display(),
  505. window[1].0.display(),
  506. window[0].1.display(),
  507. );
  508. }
  509. }
  510. Ok(())
  511. }
  512. fn compile_sass_glob(&self, sass_path: &Path, extension: &str, options: &SassOptions) -> Result<Vec<(PathBuf, PathBuf)>> {
  513. let glob_string = format!("{}/**/*.{}", sass_path.display(), extension);
  514. let files = glob(&glob_string)
  515. .unwrap()
  516. .filter_map(|e| e.ok())
  517. .filter(|entry| !entry.as_path().file_name().unwrap().to_string_lossy().starts_with('_'))
  518. .collect::<Vec<_>>();
  519. let mut compiled_paths = Vec::new();
  520. for file in files {
  521. let css = compile_file(&file, options.clone())?;
  522. let path_inside_sass = file.strip_prefix(&sass_path).unwrap();
  523. let parent_inside_sass = path_inside_sass.parent();
  524. let css_output_path = self.output_path.join(path_inside_sass).with_extension("css");
  525. if parent_inside_sass.is_some() {
  526. create_dir_all(&css_output_path.parent().unwrap())?;
  527. }
  528. create_file(&css_output_path, &css)?;
  529. compiled_paths.push((path_inside_sass.to_owned(), css_output_path));
  530. }
  531. Ok(compiled_paths)
  532. }
  533. pub fn render_aliases(&self) -> Result<()> {
  534. for page in self.pages.values() {
  535. for alias in &page.meta.aliases {
  536. let mut output_path = self.output_path.to_path_buf();
  537. let mut split = alias.split('/').collect::<Vec<_>>();
  538. // If the alias ends with an html file name, use that instead of mapping
  539. // as a path containing an `index.html`
  540. let page_name = match split.pop() {
  541. Some(part) if part.ends_with(".html") => part,
  542. Some(part) => {
  543. split.push(part);
  544. "index.html"
  545. }
  546. None => "index.html"
  547. };
  548. for component in split {
  549. output_path.push(&component);
  550. if !output_path.exists() {
  551. create_directory(&output_path)?;
  552. }
  553. }
  554. create_file(&output_path.join(page_name), &render_redirect_template(&page.permalink, &self.tera)?)?;
  555. }
  556. }
  557. Ok(())
  558. }
  559. /// Renders 404.html
  560. pub fn render_404(&self) -> Result<()> {
  561. ensure_directory_exists(&self.output_path)?;
  562. let mut context = Context::new();
  563. context.insert("config", &self.config);
  564. create_file(
  565. &self.output_path.join("404.html"),
  566. &render_template("404.html", &self.tera, &context, &self.config.theme)?,
  567. )
  568. }
  569. /// Renders robots.txt
  570. pub fn render_robots(&self) -> Result<()> {
  571. ensure_directory_exists(&self.output_path)?;
  572. let mut context = Context::new();
  573. context.insert("config", &self.config);
  574. create_file(
  575. &self.output_path.join("robots.txt"),
  576. &render_template("robots.txt", &self.tera, &context, &self.config.theme)?,
  577. )
  578. }
  579. /// Renders all taxonomies with at least one non-draft post
  580. pub fn render_taxonomies(&self) -> Result<()> {
  581. // TODO: make parallel?
  582. for taxonomy in &self.taxonomies {
  583. self.render_taxonomy(taxonomy)?;
  584. }
  585. Ok(())
  586. }
  587. fn render_taxonomy(&self, taxonomy: &Taxonomy) -> Result<()> {
  588. if taxonomy.items.is_empty() {
  589. return Ok(());
  590. }
  591. ensure_directory_exists(&self.output_path)?;
  592. let output_path = self.output_path.join(&taxonomy.kind.name);
  593. let list_output = taxonomy.render_all_terms(&self.tera, &self.config)?;
  594. create_directory(&output_path)?;
  595. create_file(&output_path.join("index.html"), &self.inject_livereload(list_output))?;
  596. taxonomy
  597. .items
  598. .par_iter()
  599. .map(|item| {
  600. if taxonomy.kind.rss {
  601. self.render_rss_feed(
  602. item.pages.iter().map(|p| p).collect(),
  603. Some(&PathBuf::from(format!("{}/{}", taxonomy.kind.name, item.slug))),
  604. )?;
  605. }
  606. if taxonomy.kind.is_paginated() {
  607. self.render_paginated(&output_path, &Paginator::from_taxonomy(&taxonomy, item))
  608. } else {
  609. let single_output = taxonomy.render_term(item, &self.tera, &self.config)?;
  610. let path = output_path.join(&item.slug);
  611. create_directory(&path)?;
  612. create_file(
  613. &path.join("index.html"),
  614. &self.inject_livereload(single_output),
  615. )
  616. }
  617. })
  618. .collect::<Result<()>>()
  619. }
  620. /// What it says on the tin
  621. pub fn render_sitemap(&self) -> Result<()> {
  622. ensure_directory_exists(&self.output_path)?;
  623. let mut context = Context::new();
  624. let mut pages = self.pages
  625. .values()
  626. .filter(|p| !p.is_draft())
  627. .map(|p| {
  628. let date = match p.meta.date {
  629. Some(ref d) => Some(d.to_string()),
  630. None => None,
  631. };
  632. SitemapEntry::new(p.permalink.clone(), date)
  633. })
  634. .collect::<Vec<_>>();
  635. pages.sort_by(|a, b| a.permalink.cmp(&b.permalink));
  636. context.insert("pages", &pages);
  637. let mut sections = self.sections
  638. .values()
  639. .map(|s| SitemapEntry::new(s.permalink.clone(), None))
  640. .collect::<Vec<_>>();
  641. sections.sort_by(|a, b| a.permalink.cmp(&b.permalink));
  642. context.insert("sections", &sections);
  643. let mut taxonomies = vec![];
  644. for taxonomy in &self.taxonomies {
  645. let name = &taxonomy.kind.name;
  646. let mut terms = vec![];
  647. terms.push(SitemapEntry::new(self.config.make_permalink(name), None));
  648. for item in &taxonomy.items {
  649. terms.push(SitemapEntry::new(self.config.make_permalink(&format!("{}/{}", &name, item.slug)), None));
  650. }
  651. terms.sort_by(|a, b| a.permalink.cmp(&b.permalink));
  652. taxonomies.push(terms);
  653. }
  654. context.insert("taxonomies", &taxonomies);
  655. context.insert("config", &self.config);
  656. let sitemap = &render_template("sitemap.xml", &self.tera, &context, &self.config.theme)?;
  657. create_file(&self.output_path.join("sitemap.xml"), sitemap)?;
  658. Ok(())
  659. }
  660. /// Renders a RSS feed for the given path and at the given path
  661. /// If both arguments are `None`, it will render only the RSS feed for the whole
  662. /// site at the root folder.
  663. pub fn render_rss_feed(&self, all_pages: Vec<&Page>, base_path: Option<&PathBuf>) -> Result<()> {
  664. ensure_directory_exists(&self.output_path)?;
  665. let mut context = Context::new();
  666. let mut pages = all_pages
  667. .into_iter()
  668. .filter(|p| p.meta.date.is_some() && !p.is_draft())
  669. .collect::<Vec<_>>();
  670. pages.par_sort_unstable_by(sort_pages_by_date);
  671. // Don't generate a RSS feed if none of the pages has a date
  672. if pages.is_empty() {
  673. return Ok(());
  674. }
  675. context.insert("last_build_date", &pages[0].meta.date.clone().map(|d| d.to_string()));
  676. // limit to the last n elements
  677. context.insert("pages", &pages.iter().take(self.config.rss_limit).collect::<Vec<_>>());
  678. context.insert("config", &self.config);
  679. let rss_feed_url = if let Some(ref base) = base_path {
  680. self.config.make_permalink(&base.join("rss.xml").to_string_lossy().replace('\\', "/"))
  681. } else {
  682. self.config.make_permalink("rss.xml")
  683. };
  684. context.insert("feed_url", &rss_feed_url);
  685. let feed = &render_template("rss.xml", &self.tera, &context, &self.config.theme)?;
  686. if let Some(ref base) = base_path {
  687. let mut output_path = self.output_path.clone().to_path_buf();
  688. for component in base.components() {
  689. output_path.push(component);
  690. if !output_path.exists() {
  691. create_directory(&output_path)?;
  692. }
  693. }
  694. create_file(&output_path.join("rss.xml"), feed)?;
  695. } else {
  696. create_file(&self.output_path.join("rss.xml"), feed)?;
  697. }
  698. Ok(())
  699. }
  700. /// Renders a single section
  701. pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> {
  702. ensure_directory_exists(&self.output_path)?;
  703. let public = self.output_path.clone();
  704. let mut output_path = public.to_path_buf();
  705. for component in &section.file.components {
  706. output_path.push(component);
  707. if !output_path.exists() {
  708. create_directory(&output_path)?;
  709. }
  710. }
  711. // Copy any asset we found previously into the same directory as the index.html
  712. for asset in &section.assets {
  713. let asset_path = asset.as_path();
  714. copy(&asset_path, &output_path.join(asset_path.file_name().unwrap()))?;
  715. }
  716. if render_pages {
  717. section
  718. .pages
  719. .par_iter()
  720. .map(|p| self.render_page(p))
  721. .collect::<Result<()>>()?;
  722. }
  723. if !section.meta.render {
  724. return Ok(());
  725. }
  726. if let Some(ref redirect_to) = section.meta.redirect_to {
  727. let permalink = self.config.make_permalink(redirect_to);
  728. create_file(&output_path.join("index.html"), &render_redirect_template(&permalink, &self.tera)?)?;
  729. return Ok(());
  730. }
  731. if section.meta.is_paginated() {
  732. self.render_paginated(&output_path, &Paginator::from_section(&section.pages, section))?;
  733. } else {
  734. let output = section.render_html(&self.tera, &self.config)?;
  735. create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
  736. }
  737. Ok(())
  738. }
  739. /// Used only on reload
  740. pub fn render_index(&self) -> Result<()> {
  741. self.render_section(
  742. &self.sections[&self.content_path.join("_index.md")],
  743. false,
  744. )
  745. }
  746. /// Renders all sections
  747. pub fn render_sections(&self) -> Result<()> {
  748. self.sections
  749. .values()
  750. .collect::<Vec<_>>()
  751. .into_par_iter()
  752. .map(|s| self.render_section(s, true))
  753. .collect::<Result<()>>()
  754. }
  755. /// Renders all pages that do not belong to any sections
  756. pub fn render_orphan_pages(&self) -> Result<()> {
  757. ensure_directory_exists(&self.output_path)?;
  758. for page in self.get_all_orphan_pages() {
  759. self.render_page(page)?;
  760. }
  761. Ok(())
  762. }
  763. /// Renders a list of pages when the section/index is wanting pagination.
  764. pub fn render_paginated(&self, output_path: &Path, paginator: &Paginator) -> Result<()> {
  765. ensure_directory_exists(&self.output_path)?;
  766. let folder_path = output_path.join(&paginator.paginate_path);
  767. create_directory(&folder_path)?;
  768. paginator
  769. .pagers
  770. .par_iter()
  771. .map(|pager| {
  772. let page_path = folder_path.join(&format!("{}", pager.index));
  773. create_directory(&page_path)?;
  774. let output = paginator.render_pager(pager, &self.config, &self.tera)?;
  775. if pager.index > 1 {
  776. create_file(&page_path.join("index.html"), &self.inject_livereload(output))?;
  777. } else {
  778. create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
  779. create_file(&page_path.join("index.html"), &render_redirect_template(&paginator.permalink, &self.tera)?)?;
  780. }
  781. Ok(())
  782. })
  783. .collect::<Result<()>>()
  784. }
  785. }