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.

912 lines
32KB

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