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.

856 lines
31KB

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