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.

725 lines
25KB

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