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.

865 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 tempfile;
  20. use std::collections::HashMap;
  21. use std::fs::{create_dir_all, 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 sass_rs::{Options as SassOptions, OutputStyle, compile_file};
  27. use errors::{Result, ResultExt};
  28. use config::{Config, get_config};
  29. use utils::fs::{create_file, copy_directory, create_directory, ensure_directory_exists};
  30. use utils::templates::{render_template, rewrite_theme_paths};
  31. use utils::net::get_available_port;
  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. // the live reload port to be used if there is one
  60. pub live_reload: Option<u16>,
  61. pub output_path: PathBuf,
  62. pub static_path: PathBuf,
  63. pub tags: Option<Taxonomy>,
  64. pub categories: Option<Taxonomy>,
  65. /// A map of all .md files (section and pages) and their permalink
  66. /// We need that if there are relative links in the content that need to be resolved
  67. pub permalinks: HashMap<String, String>,
  68. }
  69. impl Site {
  70. /// Parse a site at the given path. Defaults to the current dir
  71. /// Passing in a path is only used in tests
  72. pub fn new<P: AsRef<Path>>(path: P, config_file: &str) -> Result<Site> {
  73. let path = path.as_ref();
  74. let mut config = get_config(path, config_file);
  75. let tpl_glob = format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "templates/**/*.*ml");
  76. // Only parsing as we might be extending templates from themes and that would error
  77. // as we haven't loaded them yet
  78. let mut tera = Tera::parse(&tpl_glob).chain_err(|| "Error parsing templates")?;
  79. if let Some(theme) = config.theme.clone() {
  80. // Grab data from the extra section of the theme
  81. config.merge_with_theme(&path.join("themes").join(&theme).join("theme.toml"))?;
  82. // Test that the templates folder exist for that theme
  83. let theme_path = path.join("themes").join(&theme);
  84. if !theme_path.join("templates").exists() {
  85. bail!("Theme `{}` is missing a templates folder", theme);
  86. }
  87. let theme_tpl_glob = format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "themes/**/*.html");
  88. let mut tera_theme = Tera::parse(&theme_tpl_glob).chain_err(|| "Error parsing templates from themes")?;
  89. rewrite_theme_paths(&mut tera_theme, &theme);
  90. tera_theme.build_inheritance_chains()?;
  91. tera.extend(&tera_theme)?;
  92. }
  93. tera.extend(&GUTENBERG_TERA)?;
  94. // the `extend` above already does it but hey
  95. tera.build_inheritance_chains()?;
  96. let site = Site {
  97. base_path: path.to_path_buf(),
  98. config,
  99. tera,
  100. pages: HashMap::new(),
  101. sections: HashMap::new(),
  102. live_reload: None,
  103. output_path: path.join("public"),
  104. static_path: path.join("static"),
  105. tags: None,
  106. categories: None,
  107. permalinks: HashMap::new(),
  108. };
  109. Ok(site)
  110. }
  111. /// The index section is ALWAYS at that path
  112. pub fn index_section_path(&self) -> PathBuf {
  113. self.base_path.join("content").join("_index.md")
  114. }
  115. pub fn enable_live_reload(&mut self) {
  116. self.live_reload = get_available_port();
  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 let Some(port) = self.live_reload {
  360. return html.replace(
  361. "</body>",
  362. &format!(r#"<script src="/livereload.js?port={}&mindelay=10"></script></body>"#, port)
  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(&format!("search_index.{}.js", self.config.default_language)),
  448. &format!(
  449. "window.searchIndex = {};",
  450. search::build_index(&self.sections, &self.config.default_language)?
  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. for alias in &page.meta.aliases {
  509. let mut output_path = self.output_path.to_path_buf();
  510. for component in alias.split('/') {
  511. output_path.push(&component);
  512. if !output_path.exists() {
  513. create_directory(&output_path)?;
  514. }
  515. }
  516. create_file(&output_path.join("index.html"), &render_redirect_template(&page.permalink, &self.tera)?)?;
  517. }
  518. }
  519. Ok(())
  520. }
  521. /// Renders robots.txt
  522. pub fn render_robots(&self) -> Result<()> {
  523. ensure_directory_exists(&self.output_path)?;
  524. create_file(
  525. &self.output_path.join("robots.txt"),
  526. &render_template("robots.txt", &self.tera, &Context::new(), &self.config.theme)?
  527. )
  528. }
  529. /// Renders all categories and the single category pages if there are some
  530. pub fn render_categories(&self) -> Result<()> {
  531. if let Some(ref categories) = self.categories {
  532. self.render_taxonomy(categories)?;
  533. }
  534. Ok(())
  535. }
  536. /// Renders all tags and the single tag pages if there are some
  537. pub fn render_tags(&self) -> Result<()> {
  538. if let Some(ref tags) = self.tags {
  539. self.render_taxonomy(tags)?;
  540. }
  541. Ok(())
  542. }
  543. fn render_taxonomy(&self, taxonomy: &Taxonomy) -> Result<()> {
  544. if taxonomy.items.is_empty() {
  545. return Ok(())
  546. }
  547. ensure_directory_exists(&self.output_path)?;
  548. let output_path = self.output_path.join(&taxonomy.get_list_name());
  549. let list_output = taxonomy.render_list(&self.tera, &self.config)?;
  550. create_directory(&output_path)?;
  551. create_file(&output_path.join("index.html"), &self.inject_livereload(list_output))?;
  552. taxonomy
  553. .items
  554. .par_iter()
  555. .map(|item| {
  556. let single_output = taxonomy.render_single_item(item, &self.tera, &self.config)?;
  557. create_directory(&output_path.join(&item.slug))?;
  558. create_file(
  559. &output_path.join(&item.slug).join("index.html"),
  560. &self.inject_livereload(single_output)
  561. )
  562. })
  563. .fold(|| Ok(()), Result::and)
  564. .reduce(|| Ok(()), Result::and)
  565. }
  566. /// What it says on the tin
  567. pub fn render_sitemap(&self) -> Result<()> {
  568. ensure_directory_exists(&self.output_path)?;
  569. let mut context = Context::new();
  570. let mut pages = self.pages
  571. .values()
  572. .filter(|p| !p.is_draft())
  573. .map(|p| {
  574. let date = match p.meta.date {
  575. Some(ref d) => Some(d.to_string()),
  576. None => None,
  577. };
  578. SitemapEntry::new(p.permalink.clone(), date)
  579. })
  580. .collect::<Vec<_>>();
  581. pages.sort_by(|a, b| a.permalink.cmp(&b.permalink));
  582. context.add("pages", &pages);
  583. let mut sections = self.sections
  584. .values()
  585. .map(|s| SitemapEntry::new(s.permalink.clone(), None))
  586. .collect::<Vec<_>>();
  587. sections.sort_by(|a, b| a.permalink.cmp(&b.permalink));
  588. context.add("sections", &sections);
  589. let mut categories = vec![];
  590. if let Some(ref c) = self.categories {
  591. let name = c.get_list_name();
  592. categories.push(SitemapEntry::new(self.config.make_permalink(&name), None));
  593. for item in &c.items {
  594. categories.push(
  595. SitemapEntry::new(self.config.make_permalink(&format!("{}/{}", &name, item.slug)), None),
  596. );
  597. }
  598. }
  599. categories.sort_by(|a, b| a.permalink.cmp(&b.permalink));
  600. context.add("categories", &categories);
  601. let mut tags = vec![];
  602. if let Some(ref t) = self.tags {
  603. let name = t.get_list_name();
  604. tags.push(SitemapEntry::new(self.config.make_permalink(&name), None));
  605. for item in &t.items {
  606. tags.push(
  607. SitemapEntry::new(self.config.make_permalink(&format!("{}/{}", &name, item.slug)), None),
  608. );
  609. }
  610. }
  611. tags.sort_by(|a, b| a.permalink.cmp(&b.permalink));
  612. context.add("tags", &tags);
  613. context.add("config", &self.config);
  614. let sitemap = &render_template("sitemap.xml", &self.tera, &context, &self.config.theme)?;
  615. create_file(&self.output_path.join("sitemap.xml"), sitemap)?;
  616. Ok(())
  617. }
  618. pub fn render_rss_feed(&self) -> Result<()> {
  619. ensure_directory_exists(&self.output_path)?;
  620. let mut context = Context::new();
  621. let pages = self.pages.values()
  622. .filter(|p| p.meta.date.is_some() && !p.is_draft())
  623. .cloned()
  624. .collect::<Vec<Page>>();
  625. // Don't generate a RSS feed if none of the pages has a date
  626. if pages.is_empty() {
  627. return Ok(());
  628. }
  629. let (sorted_pages, _) = sort_pages(pages, SortBy::Date);
  630. context.add("last_build_date", &sorted_pages[0].meta.date.clone().map(|d| d.to_string()));
  631. // limit to the last n elements)
  632. context.add("pages", &sorted_pages.iter().take(self.config.rss_limit).collect::<Vec<_>>());
  633. context.add("config", &self.config);
  634. let rss_feed_url = if self.config.base_url.ends_with('/') {
  635. format!("{}{}", self.config.base_url, "rss.xml")
  636. } else {
  637. format!("{}/{}", self.config.base_url, "rss.xml")
  638. };
  639. context.add("feed_url", &rss_feed_url);
  640. let feed = &render_template("rss.xml", &self.tera, &context, &self.config.theme)?;
  641. create_file(&self.output_path.join("rss.xml"), feed)?;
  642. Ok(())
  643. }
  644. /// Renders a single section
  645. pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> {
  646. ensure_directory_exists(&self.output_path)?;
  647. let public = self.output_path.clone();
  648. let mut output_path = public.to_path_buf();
  649. for component in &section.file.components {
  650. output_path.push(component);
  651. if !output_path.exists() {
  652. create_directory(&output_path)?;
  653. }
  654. }
  655. if render_pages {
  656. section
  657. .pages
  658. .par_iter()
  659. .map(|p| self.render_page(p))
  660. .fold(|| Ok(()), Result::and)
  661. .reduce(|| Ok(()), Result::and)?;
  662. }
  663. if !section.meta.render {
  664. return Ok(());
  665. }
  666. if let Some(ref redirect_to) = section.meta.redirect_to {
  667. let permalink = self.config.make_permalink(redirect_to);
  668. create_file(&output_path.join("index.html"), &render_redirect_template(&permalink, &self.tera)?)?;
  669. return Ok(());
  670. }
  671. if section.meta.is_paginated() {
  672. self.render_paginated(&output_path, section)?;
  673. } else {
  674. let output = section.render_html(&self.tera, &self.config)?;
  675. create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
  676. }
  677. Ok(())
  678. }
  679. /// Used only on reload
  680. pub fn render_index(&self) -> Result<()> {
  681. self.render_section(
  682. &self.sections[&self.base_path.join("content").join("_index.md")],
  683. false
  684. )
  685. }
  686. /// Renders all sections
  687. pub fn render_sections(&self) -> Result<()> {
  688. self.sections
  689. .values()
  690. .collect::<Vec<_>>()
  691. .into_par_iter()
  692. .map(|s| self.render_section(s, true))
  693. .fold(|| Ok(()), Result::and)
  694. .reduce(|| Ok(()), Result::and)
  695. }
  696. /// Renders all pages that do not belong to any sections
  697. pub fn render_orphan_pages(&self) -> Result<()> {
  698. ensure_directory_exists(&self.output_path)?;
  699. for page in self.get_all_orphan_pages() {
  700. self.render_page(page)?;
  701. }
  702. Ok(())
  703. }
  704. /// Renders a list of pages when the section/index is wanting pagination.
  705. pub fn render_paginated(&self, output_path: &Path, section: &Section) -> Result<()> {
  706. ensure_directory_exists(&self.output_path)?;
  707. let paginator = Paginator::new(&section.pages, section);
  708. let folder_path = output_path.join(&section.meta.paginate_path);
  709. create_directory(&folder_path)?;
  710. paginator
  711. .pagers
  712. .par_iter()
  713. .enumerate()
  714. .map(|(i, pager)| {
  715. let page_path = folder_path.join(&format!("{}", i + 1));
  716. create_directory(&page_path)?;
  717. let output = paginator.render_pager(pager, &self.config, &self.tera)?;
  718. if i > 0 {
  719. create_file(&page_path.join("index.html"), &self.inject_livereload(output))?;
  720. } else {
  721. create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
  722. create_file(&page_path.join("index.html"), &render_redirect_template(&section.permalink, &self.tera)?)?;
  723. }
  724. Ok(())
  725. })
  726. .fold(|| Ok(()), Result::and)
  727. .reduce(|| Ok(()), Result::and)
  728. }
  729. }