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.

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