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.

1045 lines
38KB

  1. extern crate glob;
  2. extern crate rayon;
  3. extern crate serde;
  4. extern crate tera;
  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 front_matter;
  12. extern crate imageproc;
  13. extern crate library;
  14. extern crate search;
  15. extern crate templates;
  16. extern crate utils;
  17. #[cfg(test)]
  18. extern crate tempfile;
  19. use std::collections::HashMap;
  20. use std::fs::{copy, create_dir_all, remove_dir_all};
  21. use std::path::{Path, PathBuf};
  22. use std::sync::{Arc, Mutex, RwLock};
  23. use glob::glob;
  24. use rayon::prelude::*;
  25. use sass_rs::{compile_file, Options as SassOptions, OutputStyle};
  26. use tera::{Context, Tera};
  27. use config::{get_config, Config};
  28. use errors::{Result, Error};
  29. use front_matter::InsertAnchor;
  30. use library::{
  31. find_taxonomies, sort_actual_pages_by_date, Library, Page, Paginator, Section, Taxonomy,
  32. };
  33. use templates::{global_fns, render_redirect_template, ZOLA_TERA};
  34. use utils::fs::{copy_directory, create_directory, create_file, ensure_directory_exists};
  35. use utils::net::get_available_port;
  36. use utils::templates::{render_template, rewrite_theme_paths};
  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 zola site
  52. pub base_path: PathBuf,
  53. /// The parsed config for the site
  54. pub config: Config,
  55. pub tera: Tera,
  56. imageproc: Arc<Mutex<imageproc::Processor>>,
  57. // the live reload port to be used if there is one
  58. pub live_reload: Option<u16>,
  59. pub output_path: PathBuf,
  60. content_path: PathBuf,
  61. pub static_path: PathBuf,
  62. pub taxonomies: Vec<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. /// Contains all pages and sections of the site
  67. pub library: Arc<RwLock<Library>>,
  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. config.load_extra_syntaxes(path)?;
  76. let tpl_glob =
  77. format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "templates/**/*.*ml");
  78. // Only parsing as we might be extending templates from themes and that would error
  79. // as we haven't loaded them yet
  80. let mut tera = Tera::parse(&tpl_glob).map_err(|e| Error::chain("Error parsing templates", e))?;
  81. if let Some(theme) = config.theme.clone() {
  82. // Grab data from the extra section of the theme
  83. config.merge_with_theme(&path.join("themes").join(&theme).join("theme.toml"))?;
  84. // Test that the templates folder exist for that theme
  85. let theme_path = path.join("themes").join(&theme);
  86. if !theme_path.join("templates").exists() {
  87. bail!("Theme `{}` is missing a templates folder", theme);
  88. }
  89. let theme_tpl_glob = format!(
  90. "{}/{}",
  91. path.to_string_lossy().replace("\\", "/"),
  92. format!("themes/{}/templates/**/*.*ml", theme)
  93. );
  94. let mut tera_theme =
  95. Tera::parse(&theme_tpl_glob).map_err(|e| Error::chain("Error parsing templates from themes", e))?;
  96. rewrite_theme_paths(&mut tera_theme, &theme);
  97. // TODO: we do that twice, make it dry?
  98. if theme_path.join("templates").join("robots.txt").exists() {
  99. tera_theme
  100. .add_template_file(theme_path.join("templates").join("robots.txt"), None)?;
  101. }
  102. tera_theme.build_inheritance_chains()?;
  103. tera.extend(&tera_theme)?;
  104. }
  105. tera.extend(&ZOLA_TERA)?;
  106. // the `extend` above already does it but hey
  107. tera.build_inheritance_chains()?;
  108. // TODO: Tera doesn't use globset right now so we can load the robots.txt as part
  109. // of the glob above, therefore we load it manually if it exists.
  110. if path.join("templates").join("robots.txt").exists() {
  111. tera.add_template_file(path.join("templates").join("robots.txt"), Some("robots.txt"))?;
  112. }
  113. let content_path = path.join("content");
  114. let static_path = path.join("static");
  115. let imageproc =
  116. imageproc::Processor::new(content_path.clone(), &static_path, &config.base_url);
  117. let site = Site {
  118. base_path: path.to_path_buf(),
  119. config,
  120. tera,
  121. imageproc: Arc::new(Mutex::new(imageproc)),
  122. live_reload: None,
  123. output_path: path.join("public"),
  124. content_path,
  125. static_path,
  126. taxonomies: Vec::new(),
  127. permalinks: HashMap::new(),
  128. // We will allocate it properly later on
  129. library: Arc::new(RwLock::new(Library::new(0, 0, false))),
  130. };
  131. Ok(site)
  132. }
  133. /// The index sections are ALWAYS at those paths
  134. /// There are one index section for the basic language + 1 per language
  135. fn index_section_paths(&self) -> Vec<(PathBuf, Option<String>)> {
  136. let mut res = vec![(self.content_path.join("_index.md"), None)];
  137. for language in &self.config.languages {
  138. res.push((
  139. self.content_path.join(format!("_index.{}.md", language.code)),
  140. Some(language.code.clone()),
  141. ));
  142. }
  143. res
  144. }
  145. /// We avoid the port the server is going to use as it's not bound yet
  146. /// when calling this function and we could end up having tried to bind
  147. /// both http and websocket server to the same port
  148. pub fn enable_live_reload(&mut self, port_to_avoid: u16) {
  149. self.live_reload = get_available_port(port_to_avoid);
  150. }
  151. /// Get the number of orphan (== without section) pages in the site
  152. pub fn get_number_orphan_pages(&self) -> usize {
  153. self.library.read().unwrap().get_all_orphan_pages().len()
  154. }
  155. pub fn set_base_url(&mut self, base_url: String) {
  156. let mut imageproc = self.imageproc.lock().expect("Couldn't lock imageproc (set_base_url)");
  157. imageproc.set_base_url(&base_url);
  158. self.config.base_url = base_url;
  159. }
  160. pub fn set_output_path<P: AsRef<Path>>(&mut self, path: P) {
  161. self.output_path = path.as_ref().to_path_buf();
  162. }
  163. /// Reads all .md files in the `content` directory and create pages/sections
  164. /// out of them
  165. pub fn load(&mut self) -> Result<()> {
  166. let base_path = self.base_path.to_string_lossy().replace("\\", "/");
  167. let content_glob = format!("{}/{}", base_path, "content/**/*.md");
  168. let (section_entries, page_entries): (Vec<_>, Vec<_>) = glob(&content_glob)
  169. .expect("Invalid glob")
  170. .filter_map(|e| e.ok())
  171. .filter(|e| !e.as_path().file_name().unwrap().to_str().unwrap().starts_with('.'))
  172. .partition(|entry| {
  173. entry.as_path().file_name().unwrap().to_str().unwrap().starts_with("_index.")
  174. });
  175. self.library =
  176. Arc::new(RwLock::new(Library::new(page_entries.len(), section_entries.len(), self.config.is_multilingual())));
  177. let sections = {
  178. let config = &self.config;
  179. section_entries
  180. .into_par_iter()
  181. .map(|entry| {
  182. let path = entry.as_path();
  183. Section::from_file(path, config)
  184. })
  185. .collect::<Vec<_>>()
  186. };
  187. let pages = {
  188. let config = &self.config;
  189. page_entries
  190. .into_par_iter()
  191. .map(|entry| {
  192. let path = entry.as_path();
  193. Page::from_file(path, config)
  194. })
  195. .collect::<Vec<_>>()
  196. };
  197. // Kinda duplicated code for add_section/add_page but necessary to do it that
  198. // way because of the borrow checker
  199. for section in sections {
  200. let s = section?;
  201. self.add_section(s, false)?;
  202. }
  203. // Insert a default index section for each language if necessary so we don't need to create
  204. // a _index.md to render the index page at the root of the site
  205. for (index_path, lang) in self.index_section_paths() {
  206. if let Some(ref index_section) = self.library.read().unwrap().get_section(&index_path) {
  207. if self.config.build_search_index && !index_section.meta.in_search_index {
  208. bail!(
  209. "You have enabled search in the config but disabled it in the index section: \
  210. either turn off the search in the config or remote `in_search_index = true` from the \
  211. section front-matter."
  212. )
  213. }
  214. }
  215. let mut library = self.library.write().expect("Get lock for load");
  216. // Not in else because of borrow checker
  217. if !library.contains_section(&index_path) {
  218. let mut index_section = Section::default();
  219. index_section.file.parent = self.content_path.clone();
  220. index_section.file.filename =
  221. index_path.file_name().unwrap().to_string_lossy().to_string();
  222. if let Some(ref l) = lang {
  223. index_section.file.name = format!("_index.{}", l);
  224. index_section.permalink = self.config.make_permalink(l);
  225. let filename = format!("_index.{}.md", l);
  226. index_section.file.path = self.content_path.join(&filename);
  227. index_section.file.relative = filename;
  228. index_section.lang = index_section.file.find_language(&self.config)?;
  229. } else {
  230. index_section.file.name = "_index".to_string();
  231. index_section.permalink = self.config.make_permalink("");
  232. index_section.file.path = self.content_path.join("_index.md");
  233. index_section.file.relative = "_index.md".to_string();
  234. }
  235. library.insert_section(index_section);
  236. }
  237. }
  238. let mut pages_insert_anchors = HashMap::new();
  239. for page in pages {
  240. let p = page?;
  241. pages_insert_anchors.insert(
  242. p.file.path.clone(),
  243. self.find_parent_section_insert_anchor(&p.file.parent.clone(), &p.lang),
  244. );
  245. self.add_page(p, false)?;
  246. }
  247. self.register_early_global_fns();
  248. self.populate_sections();
  249. self.render_markdown()?;
  250. self.populate_taxonomies()?;
  251. self.register_tera_global_fns();
  252. Ok(())
  253. }
  254. /// Render the markdown of all pages/sections
  255. /// Used in a build and in `serve` if a shortcode has changed
  256. pub fn render_markdown(&mut self) -> Result<()> {
  257. // Another silly thing needed to not borrow &self in parallel and
  258. // make the borrow checker happy
  259. let permalinks = &self.permalinks;
  260. let tera = &self.tera;
  261. let config = &self.config;
  262. // This is needed in the first place because of silly borrow checker
  263. let mut pages_insert_anchors = HashMap::new();
  264. for (_, p) in self.library.read().unwrap().pages() {
  265. pages_insert_anchors.insert(
  266. p.file.path.clone(),
  267. self.find_parent_section_insert_anchor(&p.file.parent.clone(), &p.lang),
  268. );
  269. }
  270. let mut library = self.library.write().expect("Get lock for render_markdown");
  271. library
  272. .pages_mut()
  273. .values_mut()
  274. .collect::<Vec<_>>()
  275. .par_iter_mut()
  276. .map(|page| {
  277. let insert_anchor = pages_insert_anchors[&page.file.path];
  278. page.render_markdown(permalinks, tera, config, insert_anchor)
  279. })
  280. .collect::<Result<()>>()?;
  281. library
  282. .sections_mut()
  283. .values_mut()
  284. .collect::<Vec<_>>()
  285. .par_iter_mut()
  286. .map(|section| section.render_markdown(permalinks, tera, config))
  287. .collect::<Result<()>>()?;
  288. Ok(())
  289. }
  290. /// Adds global fns that are to be available to shortcodes while
  291. /// markdown
  292. pub fn register_early_global_fns(&mut self) {
  293. self.tera.register_function(
  294. "get_url",
  295. global_fns::GetUrl::new(self.config.clone(), self.permalinks.clone()),
  296. );
  297. self.tera.register_function(
  298. "resize_image",
  299. global_fns::ResizeImage::new(self.imageproc.clone()),
  300. );
  301. self.tera.register_function(
  302. "load_data",
  303. global_fns::LoadData::new(self.content_path.clone(), self.base_path.clone()),
  304. );
  305. self.tera.register_function("trans", global_fns::Trans::new(self.config.clone()));
  306. self.tera.register_function(
  307. "get_taxonomy_url",
  308. global_fns::GetTaxonomyUrl::new(&self.taxonomies),
  309. );
  310. }
  311. pub fn register_tera_global_fns(&mut self) {
  312. self.tera.register_function("get_page", global_fns::GetPage::new(self.base_path.clone(), self.library.clone()));
  313. self.tera.register_function("get_section", global_fns::GetSection::new(self.base_path.clone(), self.library.clone()));
  314. self.tera.register_function(
  315. "get_taxonomy",
  316. global_fns::GetTaxonomy::new(self.taxonomies.clone(), self.library.clone()),
  317. );
  318. }
  319. /// Add a page to the site
  320. /// The `render` parameter is used in the serve command, when rebuilding a page.
  321. /// If `true`, it will also render the markdown for that page
  322. /// Returns the previous page struct if there was one at the same path
  323. pub fn add_page(&mut self, mut page: Page, render: bool) -> Result<Option<Page>> {
  324. self.permalinks.insert(page.file.relative.clone(), page.permalink.clone());
  325. if render {
  326. let insert_anchor =
  327. self.find_parent_section_insert_anchor(&page.file.parent, &page.lang);
  328. page.render_markdown(&self.permalinks, &self.tera, &self.config, insert_anchor)?;
  329. }
  330. let mut library = self.library.write().expect("Get lock for add_page");
  331. let prev = library.remove_page(&page.file.path);
  332. library.insert_page(page);
  333. Ok(prev)
  334. }
  335. /// Add a section to the site
  336. /// The `render` parameter is used in the serve command, when rebuilding a page.
  337. /// If `true`, it will also render the markdown for that page
  338. /// Returns the previous section struct if there was one at the same path
  339. pub fn add_section(&mut self, mut section: Section, render: bool) -> Result<Option<Section>> {
  340. self.permalinks.insert(section.file.relative.clone(), section.permalink.clone());
  341. if render {
  342. section.render_markdown(&self.permalinks, &self.tera, &self.config)?;
  343. }
  344. let mut library = self.library.write().expect("Get lock for add_section");
  345. let prev = library.remove_section(&section.file.path);
  346. library.insert_section(section);
  347. Ok(prev)
  348. }
  349. /// Finds the insert_anchor for the parent section of the directory at `path`.
  350. /// Defaults to `AnchorInsert::None` if no parent section found
  351. pub fn find_parent_section_insert_anchor(
  352. &self,
  353. parent_path: &PathBuf,
  354. lang: &str,
  355. ) -> InsertAnchor {
  356. let parent = if lang != self.config.default_language {
  357. parent_path.join(format!("_index.{}.md", lang))
  358. } else {
  359. parent_path.join("_index.md")
  360. };
  361. match self.library.read().unwrap().get_section(&parent) {
  362. Some(s) => s.meta.insert_anchor_links,
  363. None => InsertAnchor::None,
  364. }
  365. }
  366. /// Find out the direct subsections of each subsection if there are some
  367. /// as well as the pages for each section
  368. pub fn populate_sections(&mut self) {
  369. let mut library = self.library.write().expect("Get lock for populate_sections");
  370. library.populate_sections(&self.config);
  371. }
  372. /// Find all the tags and categories if it's asked in the config
  373. pub fn populate_taxonomies(&mut self) -> Result<()> {
  374. if self.config.taxonomies.is_empty() {
  375. return Ok(());
  376. }
  377. self.taxonomies = find_taxonomies(&self.config, &self.library.read().unwrap())?;
  378. Ok(())
  379. }
  380. /// Inject live reload script tag if in live reload mode
  381. fn inject_livereload(&self, html: String) -> String {
  382. if let Some(port) = self.live_reload {
  383. return html.replace(
  384. "</body>",
  385. &format!(
  386. r#"<script src="/livereload.js?port={}&mindelay=10"></script></body>"#,
  387. port
  388. ),
  389. );
  390. }
  391. html
  392. }
  393. /// Copy the main `static` folder and the theme `static` folder if a theme is used
  394. pub fn copy_static_directories(&self) -> Result<()> {
  395. // The user files will overwrite the theme files
  396. if let Some(ref theme) = self.config.theme {
  397. copy_directory(
  398. &self.base_path.join("themes").join(theme).join("static"),
  399. &self.output_path,
  400. )?;
  401. }
  402. // We're fine with missing static folders
  403. if self.static_path.exists() {
  404. copy_directory(&self.static_path, &self.output_path)?;
  405. }
  406. Ok(())
  407. }
  408. pub fn num_img_ops(&self) -> usize {
  409. let imageproc = self.imageproc.lock().expect("Couldn't lock imageproc (num_img_ops)");
  410. imageproc.num_img_ops()
  411. }
  412. pub fn process_images(&self) -> Result<()> {
  413. let mut imageproc =
  414. self.imageproc.lock().expect("Couldn't lock imageproc (process_images)");
  415. imageproc.prune()?;
  416. imageproc.do_process()
  417. }
  418. /// Deletes the `public` directory if it exists
  419. pub fn clean(&self) -> Result<()> {
  420. if self.output_path.exists() {
  421. // Delete current `public` directory so we can start fresh
  422. remove_dir_all(&self.output_path).map_err(|e| Error::chain("Couldn't delete output directory", e))?;
  423. }
  424. Ok(())
  425. }
  426. /// Renders a single content page
  427. pub fn render_page(&self, page: &Page) -> Result<()> {
  428. ensure_directory_exists(&self.output_path)?;
  429. // Copy the nesting of the content directory if we have sections for that page
  430. let mut current_path = self.output_path.to_path_buf();
  431. for component in page.path.split('/') {
  432. current_path.push(component);
  433. if !current_path.exists() {
  434. create_directory(&current_path)?;
  435. }
  436. }
  437. // Make sure the folder exists
  438. create_directory(&current_path)?;
  439. // Finally, create a index.html file there with the page rendered
  440. let output = page.render_html(&self.tera, &self.config, &self.library.read().unwrap())?;
  441. create_file(&current_path.join("index.html"), &self.inject_livereload(output))?;
  442. // Copy any asset we found previously into the same directory as the index.html
  443. for asset in &page.assets {
  444. let asset_path = asset.as_path();
  445. copy(
  446. &asset_path,
  447. &current_path
  448. .join(asset_path.file_name().expect("Couldn't get filename from page asset")),
  449. )?;
  450. }
  451. Ok(())
  452. }
  453. /// Deletes the `public` directory and builds the site
  454. pub fn build(&self) -> Result<()> {
  455. self.clean()?;
  456. // Render aliases first to allow overwriting
  457. self.render_aliases()?;
  458. self.render_sections()?;
  459. self.render_orphan_pages()?;
  460. self.render_sitemap()?;
  461. let library = self.library.read().unwrap();
  462. if self.config.generate_rss {
  463. let pages = if self.config.is_multilingual() {
  464. library
  465. .pages_values()
  466. .iter()
  467. .filter(|p| p.lang == self.config.default_language)
  468. .map(|p| *p)
  469. .collect()
  470. } else {
  471. library.pages_values()
  472. };
  473. self.render_rss_feed(pages, None)?;
  474. }
  475. for lang in &self.config.languages {
  476. if !lang.rss {
  477. continue;
  478. }
  479. let pages = library
  480. .pages_values()
  481. .iter()
  482. .filter(|p| p.lang == lang.code)
  483. .map(|p| *p)
  484. .collect();
  485. self.render_rss_feed(pages, Some(&PathBuf::from(lang.code.clone())))?;
  486. }
  487. self.render_404()?;
  488. self.render_robots()?;
  489. self.render_taxonomies()?;
  490. if let Some(ref theme) = self.config.theme {
  491. let theme_path = self.base_path.join("themes").join(theme);
  492. if theme_path.join("sass").exists() {
  493. self.compile_sass(&theme_path)?;
  494. }
  495. }
  496. if self.config.compile_sass {
  497. self.compile_sass(&self.base_path)?;
  498. }
  499. self.process_images()?;
  500. self.copy_static_directories()?;
  501. if self.config.build_search_index {
  502. self.build_search_index()?;
  503. }
  504. Ok(())
  505. }
  506. pub fn build_search_index(&self) -> Result<()> {
  507. // index first
  508. create_file(
  509. &self.output_path.join(&format!("search_index.{}.js", self.config.default_language)),
  510. &format!(
  511. "window.searchIndex = {};",
  512. search::build_index(&self.config.default_language, &self.library.read().unwrap())?
  513. ),
  514. )?;
  515. // then elasticlunr.min.js
  516. create_file(&self.output_path.join("elasticlunr.min.js"), search::ELASTICLUNR_JS)?;
  517. Ok(())
  518. }
  519. pub fn compile_sass(&self, base_path: &Path) -> Result<()> {
  520. ensure_directory_exists(&self.output_path)?;
  521. let sass_path = {
  522. let mut sass_path = PathBuf::from(base_path);
  523. sass_path.push("sass");
  524. sass_path
  525. };
  526. let mut options = SassOptions::default();
  527. options.output_style = OutputStyle::Compressed;
  528. let mut compiled_paths = self.compile_sass_glob(&sass_path, "scss", &options.clone())?;
  529. options.indented_syntax = true;
  530. compiled_paths.extend(self.compile_sass_glob(&sass_path, "sass", &options)?);
  531. compiled_paths.sort();
  532. for window in compiled_paths.windows(2) {
  533. if window[0].1 == window[1].1 {
  534. bail!(
  535. "SASS path conflict: \"{}\" and \"{}\" both compile to \"{}\"",
  536. window[0].0.display(),
  537. window[1].0.display(),
  538. window[0].1.display(),
  539. );
  540. }
  541. }
  542. Ok(())
  543. }
  544. fn compile_sass_glob(
  545. &self,
  546. sass_path: &Path,
  547. extension: &str,
  548. options: &SassOptions,
  549. ) -> Result<Vec<(PathBuf, PathBuf)>> {
  550. let glob_string = format!("{}/**/*.{}", sass_path.display(), extension);
  551. let files = glob(&glob_string)
  552. .expect("Invalid glob for sass")
  553. .filter_map(|e| e.ok())
  554. .filter(|entry| {
  555. !entry.as_path().file_name().unwrap().to_string_lossy().starts_with('_')
  556. })
  557. .collect::<Vec<_>>();
  558. let mut compiled_paths = Vec::new();
  559. for file in files {
  560. let css = compile_file(&file, options.clone())?;
  561. let path_inside_sass = file.strip_prefix(&sass_path).unwrap();
  562. let parent_inside_sass = path_inside_sass.parent();
  563. let css_output_path = self.output_path.join(path_inside_sass).with_extension("css");
  564. if parent_inside_sass.is_some() {
  565. create_dir_all(&css_output_path.parent().unwrap())?;
  566. }
  567. create_file(&css_output_path, &css)?;
  568. compiled_paths.push((path_inside_sass.to_owned(), css_output_path));
  569. }
  570. Ok(compiled_paths)
  571. }
  572. pub fn render_aliases(&self) -> Result<()> {
  573. ensure_directory_exists(&self.output_path)?;
  574. for (_, page) in self.library.read().unwrap().pages() {
  575. for alias in &page.meta.aliases {
  576. let mut output_path = self.output_path.to_path_buf();
  577. let mut split = alias.split('/').collect::<Vec<_>>();
  578. // If the alias ends with an html file name, use that instead of mapping
  579. // as a path containing an `index.html`
  580. let page_name = match split.pop() {
  581. Some(part) if part.ends_with(".html") => part,
  582. Some(part) => {
  583. split.push(part);
  584. "index.html"
  585. }
  586. None => "index.html",
  587. };
  588. for component in split {
  589. output_path.push(&component);
  590. if !output_path.exists() {
  591. create_directory(&output_path)?;
  592. }
  593. }
  594. create_file(
  595. &output_path.join(page_name),
  596. &render_redirect_template(&page.permalink, &self.tera)?,
  597. )?;
  598. }
  599. }
  600. Ok(())
  601. }
  602. /// Renders 404.html
  603. pub fn render_404(&self) -> Result<()> {
  604. ensure_directory_exists(&self.output_path)?;
  605. let mut context = Context::new();
  606. context.insert("config", &self.config);
  607. let output = render_template("404.html", &self.tera, context, &self.config.theme)?;
  608. create_file(&self.output_path.join("404.html"), &self.inject_livereload(output))
  609. }
  610. /// Renders robots.txt
  611. pub fn render_robots(&self) -> Result<()> {
  612. ensure_directory_exists(&self.output_path)?;
  613. let mut context = Context::new();
  614. context.insert("config", &self.config);
  615. create_file(
  616. &self.output_path.join("robots.txt"),
  617. &render_template("robots.txt", &self.tera, context, &self.config.theme)?,
  618. )
  619. }
  620. /// Renders all taxonomies with at least one non-draft post
  621. pub fn render_taxonomies(&self) -> Result<()> {
  622. for taxonomy in &self.taxonomies {
  623. self.render_taxonomy(taxonomy)?;
  624. }
  625. Ok(())
  626. }
  627. fn render_taxonomy(&self, taxonomy: &Taxonomy) -> Result<()> {
  628. if taxonomy.items.is_empty() {
  629. return Ok(());
  630. }
  631. ensure_directory_exists(&self.output_path)?;
  632. let output_path = if taxonomy.kind.lang != self.config.default_language {
  633. let mid_path = self.output_path.join(&taxonomy.kind.lang);
  634. create_directory(&mid_path)?;
  635. mid_path.join(&taxonomy.kind.name)
  636. } else {
  637. self.output_path.join(&taxonomy.kind.name)
  638. };
  639. let list_output = taxonomy.render_all_terms(&self.tera, &self.config, &self.library.read().unwrap())?;
  640. create_directory(&output_path)?;
  641. create_file(&output_path.join("index.html"), &self.inject_livereload(list_output))?;
  642. let library = self.library.read().unwrap();
  643. taxonomy
  644. .items
  645. .par_iter()
  646. .map(|item| {
  647. let path = output_path.join(&item.slug);
  648. if taxonomy.kind.is_paginated() {
  649. self.render_paginated(
  650. &path,
  651. &Paginator::from_taxonomy(&taxonomy, item, &library),
  652. )?;
  653. } else {
  654. let single_output =
  655. taxonomy.render_term(item, &self.tera, &self.config, &library)?;
  656. create_directory(&path)?;
  657. create_file(&path.join("index.html"), &self.inject_livereload(single_output))?;
  658. }
  659. if taxonomy.kind.rss {
  660. self.render_rss_feed(
  661. item.pages.iter().map(|p| library.get_page_by_key(*p)).collect(),
  662. Some(&PathBuf::from(format!("{}/{}", taxonomy.kind.name, item.slug))),
  663. )
  664. } else {
  665. Ok(())
  666. }
  667. })
  668. .collect::<Result<()>>()
  669. }
  670. /// What it says on the tin
  671. pub fn render_sitemap(&self) -> Result<()> {
  672. ensure_directory_exists(&self.output_path)?;
  673. let mut context = Context::new();
  674. let mut pages = self
  675. .library
  676. .read()
  677. .unwrap()
  678. .pages_values()
  679. .iter()
  680. .filter(|p| !p.is_draft())
  681. .map(|p| {
  682. let date = match p.meta.date {
  683. Some(ref d) => Some(d.to_string()),
  684. None => None,
  685. };
  686. SitemapEntry::new(p.permalink.clone(), date)
  687. })
  688. .collect::<Vec<_>>();
  689. pages.sort_by(|a, b| a.permalink.cmp(&b.permalink));
  690. context.insert("pages", &pages);
  691. let mut sections = self
  692. .library
  693. .read().unwrap()
  694. .sections_values()
  695. .iter()
  696. .filter(|s| s.meta.render)
  697. .map(|s| SitemapEntry::new(s.permalink.clone(), None))
  698. .collect::<Vec<_>>();
  699. for section in
  700. self.library.read().unwrap().sections_values().iter().filter(|s| s.meta.paginate_by.is_some())
  701. {
  702. let number_pagers = (section.pages.len() as f64
  703. / section.meta.paginate_by.unwrap() as f64)
  704. .ceil() as isize;
  705. for i in 1..=number_pagers {
  706. let permalink =
  707. format!("{}{}/{}/", section.permalink, section.meta.paginate_path, i);
  708. sections.push(SitemapEntry::new(permalink, None))
  709. }
  710. }
  711. sections.sort_by(|a, b| a.permalink.cmp(&b.permalink));
  712. context.insert("sections", &sections);
  713. let mut taxonomies = vec![];
  714. for taxonomy in &self.taxonomies {
  715. let name = &taxonomy.kind.name;
  716. let mut terms = vec![];
  717. terms.push(SitemapEntry::new(self.config.make_permalink(name), None));
  718. for item in &taxonomy.items {
  719. terms.push(SitemapEntry::new(
  720. self.config.make_permalink(&format!("{}/{}", &name, item.slug)),
  721. None,
  722. ));
  723. if taxonomy.kind.is_paginated() {
  724. let number_pagers = (item.pages.len() as f64
  725. / taxonomy.kind.paginate_by.unwrap() as f64)
  726. .ceil() as isize;
  727. for i in 1..=number_pagers {
  728. let permalink = self.config.make_permalink(&format!(
  729. "{}/{}/{}/{}",
  730. name,
  731. item.slug,
  732. taxonomy.kind.paginate_path(),
  733. i
  734. ));
  735. terms.push(SitemapEntry::new(permalink, None))
  736. }
  737. }
  738. }
  739. terms.sort_by(|a, b| a.permalink.cmp(&b.permalink));
  740. taxonomies.push(terms);
  741. }
  742. context.insert("taxonomies", &taxonomies);
  743. context.insert("config", &self.config);
  744. let sitemap = &render_template("sitemap.xml", &self.tera, context, &self.config.theme)?;
  745. create_file(&self.output_path.join("sitemap.xml"), sitemap)?;
  746. Ok(())
  747. }
  748. /// Renders a RSS feed for the given path and at the given path
  749. /// If both arguments are `None`, it will render only the RSS feed for the whole
  750. /// site at the root folder.
  751. pub fn render_rss_feed(
  752. &self,
  753. all_pages: Vec<&Page>,
  754. base_path: Option<&PathBuf>,
  755. ) -> Result<()> {
  756. ensure_directory_exists(&self.output_path)?;
  757. let mut context = Context::new();
  758. let mut pages = all_pages
  759. .into_iter()
  760. .filter(|p| p.meta.date.is_some() && !p.is_draft())
  761. .collect::<Vec<_>>();
  762. // Don't generate a RSS feed if none of the pages has a date
  763. if pages.is_empty() {
  764. return Ok(());
  765. }
  766. pages.par_sort_unstable_by(sort_actual_pages_by_date);
  767. context.insert("last_build_date", &pages[0].meta.date.clone());
  768. let library = self.library.read().unwrap();
  769. // limit to the last n elements if the limit is set; otherwise use all.
  770. let num_entries = self.config.rss_limit.unwrap_or_else(|| pages.len());
  771. let p = pages
  772. .iter()
  773. .take(num_entries)
  774. .map(|x| x.to_serialized_basic(&library))
  775. .collect::<Vec<_>>();
  776. context.insert("pages", &p);
  777. context.insert("config", &self.config);
  778. let rss_feed_url = if let Some(ref base) = base_path {
  779. self.config.make_permalink(&base.join("rss.xml").to_string_lossy().replace('\\', "/"))
  780. } else {
  781. self.config.make_permalink("rss.xml")
  782. };
  783. context.insert("feed_url", &rss_feed_url);
  784. let feed = &render_template("rss.xml", &self.tera, context, &self.config.theme)?;
  785. if let Some(ref base) = base_path {
  786. let mut output_path = self.output_path.clone();
  787. for component in base.components() {
  788. output_path.push(component);
  789. if !output_path.exists() {
  790. create_directory(&output_path)?;
  791. }
  792. }
  793. create_file(&output_path.join("rss.xml"), feed)?;
  794. } else {
  795. create_file(&self.output_path.join("rss.xml"), feed)?;
  796. }
  797. Ok(())
  798. }
  799. /// Renders a single section
  800. pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> {
  801. ensure_directory_exists(&self.output_path)?;
  802. let mut output_path = self.output_path.clone();
  803. if section.lang != self.config.default_language {
  804. output_path.push(&section.lang);
  805. if !output_path.exists() {
  806. create_directory(&output_path)?;
  807. }
  808. }
  809. for component in &section.file.components {
  810. output_path.push(component);
  811. if !output_path.exists() {
  812. create_directory(&output_path)?;
  813. }
  814. }
  815. // Copy any asset we found previously into the same directory as the index.html
  816. for asset in &section.assets {
  817. let asset_path = asset.as_path();
  818. copy(
  819. &asset_path,
  820. &output_path.join(
  821. asset_path.file_name().expect("Failed to get asset filename for section"),
  822. ),
  823. )?;
  824. }
  825. if render_pages {
  826. section
  827. .pages
  828. .par_iter()
  829. .map(|k| self.render_page(self.library.read().unwrap().get_page_by_key(*k)))
  830. .collect::<Result<()>>()?;
  831. }
  832. if !section.meta.render {
  833. return Ok(());
  834. }
  835. if let Some(ref redirect_to) = section.meta.redirect_to {
  836. let permalink = self.config.make_permalink(redirect_to);
  837. create_file(
  838. &output_path.join("index.html"),
  839. &render_redirect_template(&permalink, &self.tera)?,
  840. )?;
  841. return Ok(());
  842. }
  843. if section.meta.is_paginated() {
  844. self.render_paginated(&output_path, &Paginator::from_section(&section, &self.library.read().unwrap()))?;
  845. } else {
  846. let output = section.render_html(&self.tera, &self.config, &self.library.read().unwrap())?;
  847. create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
  848. }
  849. Ok(())
  850. }
  851. /// Used only on reload
  852. pub fn render_index(&self) -> Result<()> {
  853. self.render_section(
  854. &self
  855. .library
  856. .read().unwrap()
  857. .get_section(&self.content_path.join("_index.md"))
  858. .expect("Failed to get index section"),
  859. false,
  860. )
  861. }
  862. /// Renders all sections
  863. pub fn render_sections(&self) -> Result<()> {
  864. self.library
  865. .read().unwrap()
  866. .sections_values()
  867. .into_par_iter()
  868. .map(|s| self.render_section(s, true))
  869. .collect::<Result<()>>()
  870. }
  871. /// Renders all pages that do not belong to any sections
  872. pub fn render_orphan_pages(&self) -> Result<()> {
  873. ensure_directory_exists(&self.output_path)?;
  874. let library = self.library.read().unwrap();
  875. for page in library.get_all_orphan_pages() {
  876. self.render_page(page)?;
  877. }
  878. Ok(())
  879. }
  880. /// Renders a list of pages when the section/index is wanting pagination.
  881. pub fn render_paginated(&self, output_path: &Path, paginator: &Paginator) -> Result<()> {
  882. ensure_directory_exists(&self.output_path)?;
  883. let folder_path = output_path.join(&paginator.paginate_path);
  884. create_directory(&folder_path)?;
  885. paginator
  886. .pagers
  887. .par_iter()
  888. .map(|pager| {
  889. let page_path = folder_path.join(&format!("{}", pager.index));
  890. create_directory(&page_path)?;
  891. let output =
  892. paginator.render_pager(pager, &self.config, &self.tera, &self.library.read().unwrap())?;
  893. if pager.index > 1 {
  894. create_file(&page_path.join("index.html"), &self.inject_livereload(output))?;
  895. } else {
  896. create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
  897. create_file(
  898. &page_path.join("index.html"),
  899. &render_redirect_template(&paginator.permalink, &self.tera)?,
  900. )?;
  901. }
  902. Ok(())
  903. })
  904. .collect::<Result<()>>()
  905. }
  906. }