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.

875 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::{ZOLA_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 zola 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. config.load_extra_syntaxes(path)?;
  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!(
  87. "{}/{}",
  88. path.to_string_lossy().replace("\\", "/"),
  89. format!("themes/{}/templates/**/*.*ml", theme)
  90. );
  91. let mut tera_theme = Tera::parse(&theme_tpl_glob).chain_err(|| "Error parsing templates from themes")?;
  92. rewrite_theme_paths(&mut tera_theme, &theme);
  93. // TODO: same as below
  94. if theme_path.join("templates").join("robots.txt").exists() {
  95. tera_theme.add_template_file(theme_path.join("templates").join("robots.txt"), None)?;
  96. }
  97. tera_theme.build_inheritance_chains()?;
  98. tera.extend(&tera_theme)?;
  99. }
  100. tera.extend(&ZOLA_TERA)?;
  101. // the `extend` above already does it but hey
  102. tera.build_inheritance_chains()?;
  103. // TODO: Tera doesn't use globset right now so we can load the robots.txt as part
  104. // of the glob above, therefore we load it manually if it exists.
  105. if path.join("templates").join("robots.txt").exists() {
  106. tera.add_template_file(path.join("templates").join("robots.txt"), Some("robots.txt"))?;
  107. }
  108. let content_path = path.join("content");
  109. let static_path = path.join("static");
  110. let imageproc = imageproc::Processor::new(content_path.clone(), &static_path, &config.base_url);
  111. let site = Site {
  112. base_path: path.to_path_buf(),
  113. config,
  114. tera,
  115. imageproc: Arc::new(Mutex::new(imageproc)),
  116. live_reload: None,
  117. output_path: path.join("public"),
  118. content_path,
  119. static_path,
  120. taxonomies: Vec::new(),
  121. permalinks: HashMap::new(),
  122. // We will allocate it properly later on
  123. library: Library::new(0, 0),
  124. };
  125. Ok(site)
  126. }
  127. /// The index section is ALWAYS at that path
  128. pub fn index_section_path(&self) -> PathBuf {
  129. self.content_path.join("_index.md")
  130. }
  131. /// We avoid the port the server is going to use as it's not bound yet
  132. /// when calling this function and we could end up having tried to bind
  133. /// both http and websocket server to the same port
  134. pub fn enable_live_reload(&mut self, port_to_avoid: u16) {
  135. self.live_reload = get_available_port(port_to_avoid);
  136. }
  137. /// Get all the orphan (== without section) pages in the site
  138. pub fn get_all_orphan_pages(&self) -> Vec<&Page> {
  139. self.library.get_all_orphan_pages()
  140. }
  141. pub fn set_base_url(&mut self, base_url: String) {
  142. let mut imageproc = self.imageproc.lock().unwrap();
  143. imageproc.set_base_url(&base_url);
  144. self.config.base_url = base_url;
  145. }
  146. pub fn set_output_path<P: AsRef<Path>>(&mut self, path: P) {
  147. self.output_path = path.as_ref().to_path_buf();
  148. }
  149. /// Reads all .md files in the `content` directory and create pages/sections
  150. /// out of them
  151. pub fn load(&mut self) -> Result<()> {
  152. let base_path = self.base_path.to_string_lossy().replace("\\", "/");
  153. let content_glob = format!("{}/{}", base_path, "content/**/*.md");
  154. let (section_entries, page_entries): (Vec<_>, Vec<_>) = glob(&content_glob)
  155. .unwrap()
  156. .filter_map(|e| e.ok())
  157. .filter(|e| !e.as_path().file_name().unwrap().to_str().unwrap().starts_with('.'))
  158. .partition(|entry| entry.as_path().file_name().unwrap() == "_index.md");
  159. self.library = Library::new(page_entries.len(), section_entries.len());
  160. let sections = {
  161. let config = &self.config;
  162. section_entries
  163. .into_par_iter()
  164. .map(|entry| {
  165. let path = entry.as_path();
  166. Section::from_file(path, config)
  167. })
  168. .collect::<Vec<_>>()
  169. };
  170. let pages = {
  171. let config = &self.config;
  172. page_entries
  173. .into_par_iter()
  174. .map(|entry| {
  175. let path = entry.as_path();
  176. Page::from_file(path, config)
  177. })
  178. .collect::<Vec<_>>()
  179. };
  180. // Kinda duplicated code for add_section/add_page but necessary to do it that
  181. // way because of the borrow checker
  182. for section in sections {
  183. let s = section?;
  184. self.add_section(s, false)?;
  185. }
  186. // Insert a default index section if necessary so we don't need to create
  187. // a _index.md to render the index page at the root of the site
  188. let index_path = self.index_section_path();
  189. if let Some(ref index_section) = self.library.get_section(&index_path) {
  190. if self.config.build_search_index && !index_section.meta.in_search_index {
  191. bail!(
  192. "You have enabled search in the config but disabled it in the index section: \
  193. either turn off the search in the config or remote `in_search_index = true` from the \
  194. section front-matter."
  195. )
  196. }
  197. }
  198. // Not in else because of borrow checker
  199. if !self.library.contains_section(&index_path) {
  200. let mut index_section = Section::default();
  201. index_section.permalink = self.config.make_permalink("");
  202. index_section.file.path = self.content_path.join("_index.md");
  203. index_section.file.parent = self.content_path.clone();
  204. index_section.file.relative = "_index.md".to_string();
  205. self.library.insert_section(index_section);
  206. }
  207. let mut pages_insert_anchors = HashMap::new();
  208. for page in pages {
  209. let p = page?;
  210. pages_insert_anchors.insert(p.file.path.clone(), self.find_parent_section_insert_anchor(&p.file.parent.clone()));
  211. self.add_page(p, false)?;
  212. }
  213. self.register_early_global_fns();
  214. self.populate_sections();
  215. self.render_markdown()?;
  216. self.populate_taxonomies()?;
  217. self.register_tera_global_fns();
  218. Ok(())
  219. }
  220. /// Render the markdown of all pages/sections
  221. /// Used in a build and in `serve` if a shortcode has changed
  222. pub fn render_markdown(&mut self) -> Result<()> {
  223. // Another silly thing needed to not borrow &self in parallel and
  224. // make the borrow checker happy
  225. let permalinks = &self.permalinks;
  226. let tera = &self.tera;
  227. let config = &self.config;
  228. // This is needed in the first place because of silly borrow checker
  229. let mut pages_insert_anchors = HashMap::new();
  230. for (_, p) in self.library.pages() {
  231. pages_insert_anchors.insert(p.file.path.clone(), self.find_parent_section_insert_anchor(&p.file.parent.clone()));
  232. }
  233. self.library
  234. .pages_mut()
  235. .values_mut()
  236. .collect::<Vec<_>>()
  237. .par_iter_mut()
  238. .map(|page| {
  239. let insert_anchor = pages_insert_anchors[&page.file.path];
  240. page.render_markdown(permalinks, tera, config, insert_anchor)
  241. })
  242. .collect::<Result<()>>()?;
  243. self.library
  244. .sections_mut()
  245. .values_mut()
  246. .collect::<Vec<_>>()
  247. .par_iter_mut()
  248. .map(|section| section.render_markdown(permalinks, tera, config))
  249. .collect::<Result<()>>()?;
  250. Ok(())
  251. }
  252. /// Adds global fns that are to be available to shortcodes while rendering markdown
  253. pub fn register_early_global_fns(&mut self) {
  254. self.tera.register_function(
  255. "get_url", global_fns::make_get_url(self.permalinks.clone(), self.config.clone()),
  256. );
  257. self.tera.register_function(
  258. "resize_image", global_fns::make_resize_image(self.imageproc.clone()),
  259. );
  260. }
  261. pub fn register_tera_global_fns(&mut self) {
  262. self.tera.register_function("trans", global_fns::make_trans(self.config.clone()));
  263. self.tera.register_function("get_page", global_fns::make_get_page(&self.library));
  264. self.tera.register_function("get_section", global_fns::make_get_section(&self.library));
  265. self.tera.register_function(
  266. "get_taxonomy",
  267. global_fns::make_get_taxonomy(&self.taxonomies, &self.library),
  268. );
  269. self.tera.register_function(
  270. "get_taxonomy_url",
  271. global_fns::make_get_taxonomy_url(&self.taxonomies),
  272. );
  273. self.tera.register_function("load_data", global_fns::make_load_data(self.content_path.clone()));
  274. }
  275. /// Add a page to the site
  276. /// The `render` parameter is used in the serve command, when rebuilding a page.
  277. /// If `true`, it will also render the markdown for that page
  278. /// Returns the previous page struct if there was one at the same path
  279. pub fn add_page(&mut self, mut page: Page, render: bool) -> Result<Option<Page>> {
  280. self.permalinks.insert(page.file.relative.clone(), page.permalink.clone());
  281. if render {
  282. let insert_anchor = self.find_parent_section_insert_anchor(&page.file.parent);
  283. page.render_markdown(&self.permalinks, &self.tera, &self.config, insert_anchor)?;
  284. }
  285. let prev = self.library.remove_page(&page.file.path);
  286. self.library.insert_page(page);
  287. Ok(prev)
  288. }
  289. /// Add a section to the site
  290. /// The `render` parameter is used in the serve command, when rebuilding a page.
  291. /// If `true`, it will also render the markdown for that page
  292. /// Returns the previous section struct if there was one at the same path
  293. pub fn add_section(&mut self, mut section: Section, render: bool) -> Result<Option<Section>> {
  294. self.permalinks.insert(section.file.relative.clone(), section.permalink.clone());
  295. if render {
  296. section.render_markdown(&self.permalinks, &self.tera, &self.config)?;
  297. }
  298. let prev = self.library.remove_section(&section.file.path);
  299. self.library.insert_section(section);
  300. Ok(prev)
  301. }
  302. /// Finds the insert_anchor for the parent section of the directory at `path`.
  303. /// Defaults to `AnchorInsert::None` if no parent section found
  304. pub fn find_parent_section_insert_anchor(&self, parent_path: &PathBuf) -> InsertAnchor {
  305. match self.library.get_section(&parent_path.join("_index.md")) {
  306. Some(s) => s.meta.insert_anchor_links,
  307. None => InsertAnchor::None
  308. }
  309. }
  310. /// Find out the direct subsections of each subsection if there are some
  311. /// as well as the pages for each section
  312. pub fn populate_sections(&mut self) {
  313. self.library.populate_sections();
  314. }
  315. /// Find all the tags and categories if it's asked in the config
  316. pub fn populate_taxonomies(&mut self) -> Result<()> {
  317. if self.config.taxonomies.is_empty() {
  318. return Ok(());
  319. }
  320. self.taxonomies = find_taxonomies(&self.config, &self.library)?;
  321. Ok(())
  322. }
  323. /// Inject live reload script tag if in live reload mode
  324. fn inject_livereload(&self, html: String) -> String {
  325. if let Some(port) = self.live_reload {
  326. return html.replace(
  327. "</body>",
  328. &format!(r#"<script src="/livereload.js?port={}&mindelay=10"></script></body>"#, port),
  329. );
  330. }
  331. html
  332. }
  333. /// Copy the main `static` folder and the theme `static` folder if a theme is used
  334. pub fn copy_static_directories(&self) -> Result<()> {
  335. // The user files will overwrite the theme files
  336. if let Some(ref theme) = self.config.theme {
  337. copy_directory(
  338. &self.base_path.join("themes").join(theme).join("static"),
  339. &self.output_path,
  340. )?;
  341. }
  342. // We're fine with missing static folders
  343. if self.static_path.exists() {
  344. copy_directory(&self.static_path, &self.output_path)?;
  345. }
  346. Ok(())
  347. }
  348. pub fn num_img_ops(&self) -> usize {
  349. let imageproc = self.imageproc.lock().unwrap();
  350. imageproc.num_img_ops()
  351. }
  352. pub fn process_images(&self) -> Result<()> {
  353. let mut imageproc = self.imageproc.lock().unwrap();
  354. imageproc.prune()?;
  355. imageproc.do_process()
  356. }
  357. /// Deletes the `public` directory if it exists
  358. pub fn clean(&self) -> Result<()> {
  359. if self.output_path.exists() {
  360. // Delete current `public` directory so we can start fresh
  361. remove_dir_all(&self.output_path).chain_err(|| "Couldn't delete output directory")?;
  362. }
  363. Ok(())
  364. }
  365. /// Renders a single content page
  366. pub fn render_page(&self, page: &Page) -> Result<()> {
  367. ensure_directory_exists(&self.output_path)?;
  368. // Copy the nesting of the content directory if we have sections for that page
  369. let mut current_path = self.output_path.to_path_buf();
  370. for component in page.path.split('/') {
  371. current_path.push(component);
  372. if !current_path.exists() {
  373. create_directory(&current_path)?;
  374. }
  375. }
  376. // Make sure the folder exists
  377. create_directory(&current_path)?;
  378. // Finally, create a index.html file there with the page rendered
  379. let output = page.render_html(&self.tera, &self.config, &self.library)?;
  380. create_file(&current_path.join("index.html"), &self.inject_livereload(output))?;
  381. // Copy any asset we found previously into the same directory as the index.html
  382. for asset in &page.assets {
  383. let asset_path = asset.as_path();
  384. copy(&asset_path, &current_path.join(asset_path.file_name().unwrap()))?;
  385. }
  386. Ok(())
  387. }
  388. /// Deletes the `public` directory and builds the site
  389. pub fn build(&self) -> Result<()> {
  390. self.clean()?;
  391. // Render aliases first to allow overwriting
  392. self.render_aliases()?;
  393. self.render_sections()?;
  394. self.render_orphan_pages()?;
  395. self.render_sitemap()?;
  396. if self.config.generate_rss {
  397. self.render_rss_feed(self.library.pages_values(), None)?;
  398. }
  399. self.render_404()?;
  400. self.render_robots()?;
  401. self.render_taxonomies()?;
  402. if let Some(ref theme) = self.config.theme {
  403. let theme_path = self.base_path.join("themes").join(theme);
  404. if theme_path.join("sass").exists() {
  405. self.compile_sass(&theme_path)?;
  406. }
  407. }
  408. if self.config.compile_sass {
  409. self.compile_sass(&self.base_path)?;
  410. }
  411. self.process_images()?;
  412. self.copy_static_directories()?;
  413. if self.config.build_search_index {
  414. self.build_search_index()?;
  415. }
  416. Ok(())
  417. }
  418. pub fn build_search_index(&self) -> Result<()> {
  419. // index first
  420. create_file(
  421. &self.output_path.join(&format!("search_index.{}.js", self.config.default_language)),
  422. &format!(
  423. "window.searchIndex = {};",
  424. search::build_index(&self.config.default_language, &self.library)?
  425. ),
  426. )?;
  427. // then elasticlunr.min.js
  428. create_file(
  429. &self.output_path.join("elasticlunr.min.js"),
  430. search::ELASTICLUNR_JS,
  431. )?;
  432. Ok(())
  433. }
  434. pub fn compile_sass(&self, base_path: &Path) -> Result<()> {
  435. ensure_directory_exists(&self.output_path)?;
  436. let sass_path = {
  437. let mut sass_path = PathBuf::from(base_path);
  438. sass_path.push("sass");
  439. sass_path
  440. };
  441. let mut options = SassOptions::default();
  442. options.output_style = OutputStyle::Compressed;
  443. let mut compiled_paths = self.compile_sass_glob(&sass_path, "scss", &options.clone())?;
  444. options.indented_syntax = true;
  445. compiled_paths.extend(self.compile_sass_glob(&sass_path, "sass", &options)?);
  446. compiled_paths.sort();
  447. for window in compiled_paths.windows(2) {
  448. if window[0].1 == window[1].1 {
  449. bail!(
  450. "SASS path conflict: \"{}\" and \"{}\" both compile to \"{}\"",
  451. window[0].0.display(),
  452. window[1].0.display(),
  453. window[0].1.display(),
  454. );
  455. }
  456. }
  457. Ok(())
  458. }
  459. fn compile_sass_glob(&self, sass_path: &Path, extension: &str, options: &SassOptions) -> Result<Vec<(PathBuf, PathBuf)>> {
  460. let glob_string = format!("{}/**/*.{}", sass_path.display(), extension);
  461. let files = glob(&glob_string)
  462. .unwrap()
  463. .filter_map(|e| e.ok())
  464. .filter(|entry| !entry.as_path().file_name().unwrap().to_string_lossy().starts_with('_'))
  465. .collect::<Vec<_>>();
  466. let mut compiled_paths = Vec::new();
  467. for file in files {
  468. let css = compile_file(&file, options.clone())?;
  469. let path_inside_sass = file.strip_prefix(&sass_path).unwrap();
  470. let parent_inside_sass = path_inside_sass.parent();
  471. let css_output_path = self.output_path.join(path_inside_sass).with_extension("css");
  472. if parent_inside_sass.is_some() {
  473. create_dir_all(&css_output_path.parent().unwrap())?;
  474. }
  475. create_file(&css_output_path, &css)?;
  476. compiled_paths.push((path_inside_sass.to_owned(), css_output_path));
  477. }
  478. Ok(compiled_paths)
  479. }
  480. pub fn render_aliases(&self) -> Result<()> {
  481. for (_, page) in self.library.pages() {
  482. for alias in &page.meta.aliases {
  483. let mut output_path = self.output_path.to_path_buf();
  484. let mut split = alias.split('/').collect::<Vec<_>>();
  485. // If the alias ends with an html file name, use that instead of mapping
  486. // as a path containing an `index.html`
  487. let page_name = match split.pop() {
  488. Some(part) if part.ends_with(".html") => part,
  489. Some(part) => {
  490. split.push(part);
  491. "index.html"
  492. }
  493. None => "index.html"
  494. };
  495. for component in split {
  496. output_path.push(&component);
  497. if !output_path.exists() {
  498. create_directory(&output_path)?;
  499. }
  500. }
  501. create_file(&output_path.join(page_name), &render_redirect_template(&page.permalink, &self.tera)?)?;
  502. }
  503. }
  504. Ok(())
  505. }
  506. /// Renders 404.html
  507. pub fn render_404(&self) -> Result<()> {
  508. ensure_directory_exists(&self.output_path)?;
  509. let mut context = Context::new();
  510. context.insert("config", &self.config);
  511. create_file(
  512. &self.output_path.join("404.html"),
  513. &render_template("404.html", &self.tera, &context, &self.config.theme)?,
  514. )
  515. }
  516. /// Renders robots.txt
  517. pub fn render_robots(&self) -> Result<()> {
  518. ensure_directory_exists(&self.output_path)?;
  519. let mut context = Context::new();
  520. context.insert("config", &self.config);
  521. create_file(
  522. &self.output_path.join("robots.txt"),
  523. &render_template("robots.txt", &self.tera, &context, &self.config.theme)?,
  524. )
  525. }
  526. /// Renders all taxonomies with at least one non-draft post
  527. pub fn render_taxonomies(&self) -> Result<()> {
  528. // TODO: make parallel?
  529. for taxonomy in &self.taxonomies {
  530. self.render_taxonomy(taxonomy)?;
  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.kind.name);
  540. let list_output = taxonomy.render_all_terms(&self.tera, &self.config, &self.library)?;
  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. if taxonomy.kind.rss {
  548. self.render_rss_feed(
  549. item.pages.iter().map(|p| self.library.get_page_by_key(*p)).collect(),
  550. Some(&PathBuf::from(format!("{}/{}", taxonomy.kind.name, item.slug))),
  551. )?;
  552. }
  553. if taxonomy.kind.is_paginated() {
  554. self.render_paginated(&output_path, &Paginator::from_taxonomy(&taxonomy, item, &self.library))
  555. } else {
  556. let single_output = taxonomy.render_term(item, &self.tera, &self.config, &self.library)?;
  557. let path = output_path.join(&item.slug);
  558. create_directory(&path)?;
  559. create_file(
  560. &path.join("index.html"),
  561. &self.inject_livereload(single_output),
  562. )
  563. }
  564. })
  565. .collect::<Result<()>>()
  566. }
  567. /// What it says on the tin
  568. pub fn render_sitemap(&self) -> Result<()> {
  569. ensure_directory_exists(&self.output_path)?;
  570. let mut context = Context::new();
  571. let mut pages = self.library
  572. .pages_values()
  573. .iter()
  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.insert("pages", &pages);
  585. let mut sections = self.library
  586. .sections_values()
  587. .iter()
  588. .map(|s| SitemapEntry::new(s.permalink.clone(), None))
  589. .collect::<Vec<_>>();
  590. sections.sort_by(|a, b| a.permalink.cmp(&b.permalink));
  591. context.insert("sections", &sections);
  592. let mut taxonomies = vec![];
  593. for taxonomy in &self.taxonomies {
  594. let name = &taxonomy.kind.name;
  595. let mut terms = vec![];
  596. terms.push(SitemapEntry::new(self.config.make_permalink(name), None));
  597. for item in &taxonomy.items {
  598. terms.push(SitemapEntry::new(self.config.make_permalink(&format!("{}/{}", &name, item.slug)), None));
  599. }
  600. terms.sort_by(|a, b| a.permalink.cmp(&b.permalink));
  601. taxonomies.push(terms);
  602. }
  603. context.insert("taxonomies", &taxonomies);
  604. context.insert("config", &self.config);
  605. let sitemap = &render_template("sitemap.xml", &self.tera, &context, &self.config.theme)?;
  606. create_file(&self.output_path.join("sitemap.xml"), sitemap)?;
  607. Ok(())
  608. }
  609. /// Renders a RSS feed for the given path and at the given path
  610. /// If both arguments are `None`, it will render only the RSS feed for the whole
  611. /// site at the root folder.
  612. pub fn render_rss_feed(&self, all_pages: Vec<&Page>, base_path: Option<&PathBuf>) -> Result<()> {
  613. ensure_directory_exists(&self.output_path)?;
  614. let mut context = Context::new();
  615. let mut pages = all_pages
  616. .into_iter()
  617. .filter(|p| p.meta.date.is_some() && !p.is_draft())
  618. .collect::<Vec<_>>();
  619. // Don't generate a RSS feed if none of the pages has a date
  620. if pages.is_empty() {
  621. return Ok(());
  622. }
  623. pages.par_sort_unstable_by(sort_actual_pages_by_date);
  624. context.insert("last_build_date", &pages[0].meta.date.clone().map(|d| d.to_string()));
  625. // limit to the last n elements if the limit is set; otherwise use all.
  626. let num_entries = self.config.rss_limit.unwrap_or(pages.len());
  627. let p = pages
  628. .iter()
  629. .take(num_entries)
  630. .map(|x| x.to_serialized_basic(&self.library))
  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, &self.library)?;
  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. }