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.

867 lines
31KB

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