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.

1258 lines
45KB

  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 link_checker;
  15. extern crate search;
  16. extern crate templates;
  17. extern crate utils;
  18. #[cfg(test)]
  19. extern crate tempfile;
  20. pub mod sitemap;
  21. use std::collections::HashMap;
  22. use std::fs::{copy, create_dir_all, remove_dir_all};
  23. use std::path::{Path, PathBuf};
  24. use std::sync::{Arc, Mutex, RwLock};
  25. use glob::glob;
  26. use rayon::prelude::*;
  27. use sass_rs::{compile_file, Options as SassOptions, OutputStyle};
  28. use tera::{Context, Tera};
  29. use config::{get_config, Config};
  30. use errors::{Error, ErrorKind, Result};
  31. use front_matter::InsertAnchor;
  32. use library::{
  33. find_taxonomies, sort_actual_pages_by_date, Library, Page, Paginator, Section, Taxonomy,
  34. };
  35. use link_checker::check_url;
  36. use templates::{global_fns, render_redirect_template, ZOLA_TERA};
  37. use utils::fs::{copy_directory, create_directory, create_file, ensure_directory_exists};
  38. use utils::net::get_available_port;
  39. use utils::templates::{render_template, rewrite_theme_paths};
  40. #[derive(Debug)]
  41. pub struct Site {
  42. /// The base path of the zola site
  43. pub base_path: PathBuf,
  44. /// The parsed config for the site
  45. pub config: Config,
  46. pub tera: Tera,
  47. imageproc: Arc<Mutex<imageproc::Processor>>,
  48. // the live reload port to be used if there is one
  49. pub live_reload: Option<u16>,
  50. pub output_path: PathBuf,
  51. content_path: PathBuf,
  52. pub static_path: PathBuf,
  53. pub taxonomies: Vec<Taxonomy>,
  54. /// A map of all .md files (section and pages) and their permalink
  55. /// We need that if there are relative links in the content that need to be resolved
  56. pub permalinks: HashMap<String, String>,
  57. /// Contains all pages and sections of the site
  58. pub library: Arc<RwLock<Library>>,
  59. /// Whether to load draft pages
  60. include_drafts: bool,
  61. }
  62. impl Site {
  63. /// Parse a site at the given path. Defaults to the current dir
  64. /// Passing in a path is only used in tests
  65. pub fn new<P: AsRef<Path>>(path: P, config_file: &str) -> Result<Site> {
  66. let path = path.as_ref();
  67. let mut config = get_config(path, config_file);
  68. config.load_extra_syntaxes(path)?;
  69. let tpl_glob =
  70. format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "templates/**/*.*ml");
  71. // Only parsing as we might be extending templates from themes and that would error
  72. // as we haven't loaded them yet
  73. let mut tera =
  74. Tera::parse(&tpl_glob).map_err(|e| Error::chain("Error parsing templates", e))?;
  75. if let Some(theme) = config.theme.clone() {
  76. // Grab data from the extra section of the theme
  77. config.merge_with_theme(&path.join("themes").join(&theme).join("theme.toml"))?;
  78. // Test that the templates folder exist for that theme
  79. let theme_path = path.join("themes").join(&theme);
  80. if !theme_path.join("templates").exists() {
  81. bail!("Theme `{}` is missing a templates folder", theme);
  82. }
  83. let theme_tpl_glob = format!(
  84. "{}/{}",
  85. path.to_string_lossy().replace("\\", "/"),
  86. format!("themes/{}/templates/**/*.*ml", theme)
  87. );
  88. let mut tera_theme = Tera::parse(&theme_tpl_glob)
  89. .map_err(|e| Error::chain("Error parsing templates from themes", e))?;
  90. rewrite_theme_paths(&mut tera_theme, &theme);
  91. // TODO: we do that twice, make it dry?
  92. if theme_path.join("templates").join("robots.txt").exists() {
  93. tera_theme
  94. .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(&ZOLA_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 =
  110. 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. include_drafts: false,
  123. // We will allocate it properly later on
  124. library: Arc::new(RwLock::new(Library::new(0, 0, false))),
  125. };
  126. Ok(site)
  127. }
  128. /// Set the site to load the drafts.
  129. /// Needs to be called before loading it
  130. pub fn include_drafts(&mut self) {
  131. self.include_drafts = true;
  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 = Arc::new(RwLock::new(Library::new(
  176. page_entries.len(),
  177. section_entries.len(),
  178. self.config.is_multilingual(),
  179. )));
  180. let sections = {
  181. let config = &self.config;
  182. section_entries
  183. .into_par_iter()
  184. .map(|entry| {
  185. let path = entry.as_path();
  186. Section::from_file(path, config, &self.base_path)
  187. })
  188. .collect::<Vec<_>>()
  189. };
  190. let pages = {
  191. let config = &self.config;
  192. page_entries
  193. .into_par_iter()
  194. .filter(|entry| match &config.ignored_content_globset {
  195. Some(gs) => !gs.is_match(entry.as_path()),
  196. None => true,
  197. })
  198. .map(|entry| {
  199. let path = entry.as_path();
  200. Page::from_file(path, config, &self.base_path)
  201. })
  202. .collect::<Vec<_>>()
  203. };
  204. // Kinda duplicated code for add_section/add_page but necessary to do it that
  205. // way because of the borrow checker
  206. for section in sections {
  207. let s = section?;
  208. self.add_section(s, false)?;
  209. }
  210. self.create_default_index_sections()?;
  211. let mut pages_insert_anchors = HashMap::new();
  212. for page in pages {
  213. let p = page?;
  214. // Should draft pages be ignored?
  215. if p.meta.draft && !self.include_drafts {
  216. continue;
  217. }
  218. pages_insert_anchors.insert(
  219. p.file.path.clone(),
  220. self.find_parent_section_insert_anchor(&p.file.parent.clone(), &p.lang),
  221. );
  222. self.add_page(p, false)?;
  223. }
  224. {
  225. let library = self.library.read().unwrap();
  226. let collisions = library.check_for_path_collisions();
  227. if !collisions.is_empty() {
  228. return Err(Error::from_collisions(collisions));
  229. }
  230. }
  231. // taxonomy Tera fns are loaded in `register_early_global_fns`
  232. // so we do need to populate it first.
  233. self.populate_taxonomies()?;
  234. self.register_early_global_fns();
  235. self.populate_sections();
  236. self.render_markdown()?;
  237. self.register_tera_global_fns();
  238. // Needs to be done after rendering markdown as we only get the anchors at that point
  239. self.check_internal_links_with_anchors()?;
  240. if self.config.is_in_check_mode() {
  241. self.check_external_links()?;
  242. }
  243. Ok(())
  244. }
  245. /// Very similar to check_external_links but can't be merged as far as I can see since we always
  246. /// want to check the internal links but only the external in zola check :/
  247. pub fn check_internal_links_with_anchors(&self) -> Result<()> {
  248. let library = self.library.write().expect("Get lock for check_internal_links_with_anchors");
  249. let page_links = library
  250. .pages()
  251. .values()
  252. .map(|p| {
  253. let path = &p.file.path;
  254. p.internal_links_with_anchors.iter().map(move |l| (path.clone(), l))
  255. })
  256. .flatten();
  257. let section_links = library
  258. .sections()
  259. .values()
  260. .map(|p| {
  261. let path = &p.file.path;
  262. p.internal_links_with_anchors.iter().map(move |l| (path.clone(), l))
  263. })
  264. .flatten();
  265. let all_links = page_links.chain(section_links).collect::<Vec<_>>();
  266. if self.config.is_in_check_mode() {
  267. println!("Checking {} internal link(s) with an anchor.", all_links.len());
  268. }
  269. if all_links.is_empty() {
  270. return Ok(());
  271. }
  272. let mut full_path = self.base_path.clone();
  273. full_path.push("content");
  274. let errors: Vec<_> = all_links
  275. .iter()
  276. .filter_map(|(page_path, (md_path, anchor))| {
  277. // There are a few `expect` here since the presence of the .md file will
  278. // already have been checked in the markdown rendering
  279. let mut p = full_path.clone();
  280. for part in md_path.split('/') {
  281. p.push(part);
  282. }
  283. if md_path.contains("_index.md") {
  284. let section = library
  285. .get_section(&p)
  286. .expect("Couldn't find section in check_internal_links_with_anchors");
  287. if section.has_anchor(&anchor) {
  288. None
  289. } else {
  290. Some((page_path, md_path, anchor))
  291. }
  292. } else {
  293. let page = library
  294. .get_page(&p)
  295. .expect("Couldn't find section in check_internal_links_with_anchors");
  296. if page.has_anchor(&anchor) {
  297. None
  298. } else {
  299. Some((page_path, md_path, anchor))
  300. }
  301. }
  302. })
  303. .collect();
  304. if self.config.is_in_check_mode() {
  305. println!(
  306. "> Checked {} internal link(s) with an anchor: {} error(s) found.",
  307. all_links.len(),
  308. errors.len()
  309. );
  310. }
  311. if errors.is_empty() {
  312. return Ok(());
  313. }
  314. let msg = errors
  315. .into_iter()
  316. .map(|(page_path, md_path, anchor)| {
  317. format!(
  318. "The anchor in the link `@/{}#{}` in {} does not exist.",
  319. md_path,
  320. anchor,
  321. page_path.to_string_lossy(),
  322. )
  323. })
  324. .collect::<Vec<_>>()
  325. .join("\n");
  326. Err(Error { kind: ErrorKind::Msg(msg), source: None })
  327. }
  328. pub fn check_external_links(&self) -> Result<()> {
  329. let library = self.library.write().expect("Get lock for check_external_links");
  330. let page_links = library
  331. .pages()
  332. .values()
  333. .map(|p| {
  334. let path = &p.file.path;
  335. p.external_links.iter().map(move |l| (path.clone(), l))
  336. })
  337. .flatten();
  338. let section_links = library
  339. .sections()
  340. .values()
  341. .map(|p| {
  342. let path = &p.file.path;
  343. p.external_links.iter().map(move |l| (path.clone(), l))
  344. })
  345. .flatten();
  346. let all_links = page_links.chain(section_links).collect::<Vec<_>>();
  347. println!("Checking {} external link(s).", all_links.len());
  348. if all_links.is_empty() {
  349. return Ok(());
  350. }
  351. // create thread pool with lots of threads so we can fetch
  352. // (almost) all pages simultaneously
  353. let threads = std::cmp::min(all_links.len(), 32);
  354. let pool = rayon::ThreadPoolBuilder::new()
  355. .num_threads(threads)
  356. .build()
  357. .map_err(|e| Error { kind: ErrorKind::Msg(e.to_string()), source: None })?;
  358. let errors: Vec<_> = pool.install(|| {
  359. all_links
  360. .par_iter()
  361. .filter_map(|(page_path, link)| {
  362. if self
  363. .config
  364. .link_checker
  365. .skip_prefixes
  366. .iter()
  367. .any(|prefix| link.starts_with(prefix))
  368. {
  369. return None;
  370. }
  371. let res = check_url(&link, &self.config.link_checker);
  372. if res.is_valid() {
  373. None
  374. } else {
  375. Some((page_path, link, res))
  376. }
  377. })
  378. .collect()
  379. });
  380. println!(
  381. "> Checked {} external link(s): {} error(s) found.",
  382. all_links.len(),
  383. errors.len()
  384. );
  385. if errors.is_empty() {
  386. return Ok(());
  387. }
  388. let msg = errors
  389. .into_iter()
  390. .map(|(page_path, link, check_res)| {
  391. format!(
  392. "Dead link in {} to {}: {}",
  393. page_path.to_string_lossy(),
  394. link,
  395. check_res.message()
  396. )
  397. })
  398. .collect::<Vec<_>>()
  399. .join("\n");
  400. Err(Error { kind: ErrorKind::Msg(msg), source: None })
  401. }
  402. /// Insert a default index section for each language if necessary so we don't need to create
  403. /// a _index.md to render the index page at the root of the site
  404. pub fn create_default_index_sections(&mut self) -> Result<()> {
  405. for (index_path, lang) in self.index_section_paths() {
  406. if let Some(ref index_section) = self.library.read().unwrap().get_section(&index_path) {
  407. if self.config.build_search_index && !index_section.meta.in_search_index {
  408. bail!(
  409. "You have enabled search in the config but disabled it in the index section: \
  410. either turn off the search in the config or remote `in_search_index = true` from the \
  411. section front-matter."
  412. )
  413. }
  414. }
  415. let mut library = self.library.write().expect("Get lock for load");
  416. // Not in else because of borrow checker
  417. if !library.contains_section(&index_path) {
  418. let mut index_section = Section::default();
  419. index_section.file.parent = self.content_path.clone();
  420. index_section.file.filename =
  421. index_path.file_name().unwrap().to_string_lossy().to_string();
  422. if let Some(ref l) = lang {
  423. index_section.file.name = format!("_index.{}", l);
  424. index_section.path = format!("{}/", l);
  425. index_section.permalink = self.config.make_permalink(l);
  426. let filename = format!("_index.{}.md", l);
  427. index_section.file.path = self.content_path.join(&filename);
  428. index_section.file.relative = filename;
  429. index_section.lang = index_section.file.find_language(&self.config)?;
  430. } else {
  431. index_section.file.name = "_index".to_string();
  432. index_section.permalink = self.config.make_permalink("");
  433. index_section.file.path = self.content_path.join("_index.md");
  434. index_section.file.relative = "_index.md".to_string();
  435. }
  436. library.insert_section(index_section);
  437. }
  438. }
  439. Ok(())
  440. }
  441. /// Render the markdown of all pages/sections
  442. /// Used in a build and in `serve` if a shortcode has changed
  443. pub fn render_markdown(&mut self) -> Result<()> {
  444. // Another silly thing needed to not borrow &self in parallel and
  445. // make the borrow checker happy
  446. let permalinks = &self.permalinks;
  447. let tera = &self.tera;
  448. let config = &self.config;
  449. // This is needed in the first place because of silly borrow checker
  450. let mut pages_insert_anchors = HashMap::new();
  451. for (_, p) in self.library.read().unwrap().pages() {
  452. pages_insert_anchors.insert(
  453. p.file.path.clone(),
  454. self.find_parent_section_insert_anchor(&p.file.parent.clone(), &p.lang),
  455. );
  456. }
  457. let mut library = self.library.write().expect("Get lock for render_markdown");
  458. library
  459. .pages_mut()
  460. .values_mut()
  461. .collect::<Vec<_>>()
  462. .par_iter_mut()
  463. .map(|page| {
  464. let insert_anchor = pages_insert_anchors[&page.file.path];
  465. page.render_markdown(permalinks, tera, config, insert_anchor)
  466. })
  467. .collect::<Result<()>>()?;
  468. library
  469. .sections_mut()
  470. .values_mut()
  471. .collect::<Vec<_>>()
  472. .par_iter_mut()
  473. .map(|section| section.render_markdown(permalinks, tera, config))
  474. .collect::<Result<()>>()?;
  475. Ok(())
  476. }
  477. /// Adds global fns that are to be available to shortcodes while
  478. /// markdown
  479. pub fn register_early_global_fns(&mut self) {
  480. self.tera.register_function(
  481. "get_url",
  482. global_fns::GetUrl::new(self.config.clone(), self.permalinks.clone()),
  483. );
  484. self.tera.register_function(
  485. "resize_image",
  486. global_fns::ResizeImage::new(self.imageproc.clone()),
  487. );
  488. self.tera.register_function(
  489. "get_image_metadata",
  490. global_fns::GetImageMeta::new(self.content_path.clone()),
  491. );
  492. self.tera.register_function("load_data", global_fns::LoadData::new(self.base_path.clone()));
  493. self.tera.register_function("trans", global_fns::Trans::new(self.config.clone()));
  494. self.tera.register_function(
  495. "get_taxonomy_url",
  496. global_fns::GetTaxonomyUrl::new(&self.config.default_language, &self.taxonomies),
  497. );
  498. }
  499. pub fn register_tera_global_fns(&mut self) {
  500. self.tera.register_function(
  501. "get_page",
  502. global_fns::GetPage::new(self.base_path.clone(), self.library.clone()),
  503. );
  504. self.tera.register_function(
  505. "get_section",
  506. global_fns::GetSection::new(self.base_path.clone(), self.library.clone()),
  507. );
  508. self.tera.register_function(
  509. "get_taxonomy",
  510. global_fns::GetTaxonomy::new(
  511. &self.config.default_language,
  512. self.taxonomies.clone(),
  513. self.library.clone(),
  514. ),
  515. );
  516. }
  517. /// Add a page to the site
  518. /// The `render` parameter is used in the serve command, when rebuilding a page.
  519. /// If `true`, it will also render the markdown for that page
  520. /// Returns the previous page struct if there was one at the same path
  521. pub fn add_page(&mut self, mut page: Page, render: bool) -> Result<Option<Page>> {
  522. self.permalinks.insert(page.file.relative.clone(), page.permalink.clone());
  523. if render {
  524. let insert_anchor =
  525. self.find_parent_section_insert_anchor(&page.file.parent, &page.lang);
  526. page.render_markdown(&self.permalinks, &self.tera, &self.config, insert_anchor)?;
  527. }
  528. let mut library = self.library.write().expect("Get lock for add_page");
  529. let prev = library.remove_page(&page.file.path);
  530. library.insert_page(page);
  531. Ok(prev)
  532. }
  533. /// Add a section to the site
  534. /// The `render` parameter is used in the serve command, when rebuilding a page.
  535. /// If `true`, it will also render the markdown for that page
  536. /// Returns the previous section struct if there was one at the same path
  537. pub fn add_section(&mut self, mut section: Section, render: bool) -> Result<Option<Section>> {
  538. self.permalinks.insert(section.file.relative.clone(), section.permalink.clone());
  539. if render {
  540. section.render_markdown(&self.permalinks, &self.tera, &self.config)?;
  541. }
  542. let mut library = self.library.write().expect("Get lock for add_section");
  543. let prev = library.remove_section(&section.file.path);
  544. library.insert_section(section);
  545. Ok(prev)
  546. }
  547. /// Finds the insert_anchor for the parent section of the directory at `path`.
  548. /// Defaults to `AnchorInsert::None` if no parent section found
  549. pub fn find_parent_section_insert_anchor(
  550. &self,
  551. parent_path: &PathBuf,
  552. lang: &str,
  553. ) -> InsertAnchor {
  554. let parent = if lang != self.config.default_language {
  555. parent_path.join(format!("_index.{}.md", lang))
  556. } else {
  557. parent_path.join("_index.md")
  558. };
  559. match self.library.read().unwrap().get_section(&parent) {
  560. Some(s) => s.meta.insert_anchor_links,
  561. None => InsertAnchor::None,
  562. }
  563. }
  564. /// Find out the direct subsections of each subsection if there are some
  565. /// as well as the pages for each section
  566. pub fn populate_sections(&mut self) {
  567. let mut library = self.library.write().expect("Get lock for populate_sections");
  568. library.populate_sections(&self.config);
  569. }
  570. /// Find all the tags and categories if it's asked in the config
  571. pub fn populate_taxonomies(&mut self) -> Result<()> {
  572. if self.config.taxonomies.is_empty() {
  573. return Ok(());
  574. }
  575. self.taxonomies = find_taxonomies(&self.config, &self.library.read().unwrap())?;
  576. Ok(())
  577. }
  578. /// Inject live reload script tag if in live reload mode
  579. fn inject_livereload(&self, html: String) -> String {
  580. if let Some(port) = self.live_reload {
  581. return html.replace(
  582. "</body>",
  583. &format!(
  584. r#"<script src="/livereload.js?port={}&amp;mindelay=10"></script></body>"#,
  585. port
  586. ),
  587. );
  588. }
  589. html
  590. }
  591. /// Copy the main `static` folder and the theme `static` folder if a theme is used
  592. pub fn copy_static_directories(&self) -> Result<()> {
  593. // The user files will overwrite the theme files
  594. if let Some(ref theme) = self.config.theme {
  595. copy_directory(
  596. &self.base_path.join("themes").join(theme).join("static"),
  597. &self.output_path,
  598. false,
  599. )?;
  600. }
  601. // We're fine with missing static folders
  602. if self.static_path.exists() {
  603. copy_directory(&self.static_path, &self.output_path, self.config.hard_link_static)?;
  604. }
  605. Ok(())
  606. }
  607. pub fn num_img_ops(&self) -> usize {
  608. let imageproc = self.imageproc.lock().expect("Couldn't lock imageproc (num_img_ops)");
  609. imageproc.num_img_ops()
  610. }
  611. pub fn process_images(&self) -> Result<()> {
  612. let mut imageproc =
  613. self.imageproc.lock().expect("Couldn't lock imageproc (process_images)");
  614. imageproc.prune()?;
  615. imageproc.do_process()
  616. }
  617. /// Deletes the `public` directory if it exists
  618. pub fn clean(&self) -> Result<()> {
  619. if self.output_path.exists() {
  620. // Delete current `public` directory so we can start fresh
  621. remove_dir_all(&self.output_path)
  622. .map_err(|e| Error::chain("Couldn't delete output directory", e))?;
  623. }
  624. Ok(())
  625. }
  626. /// Renders a single content page
  627. pub fn render_page(&self, page: &Page) -> Result<()> {
  628. ensure_directory_exists(&self.output_path)?;
  629. // Copy the nesting of the content directory if we have sections for that page
  630. let mut current_path = self.output_path.to_path_buf();
  631. for component in page.path.split('/') {
  632. current_path.push(component);
  633. if !current_path.exists() {
  634. create_directory(&current_path)?;
  635. }
  636. }
  637. // Make sure the folder exists
  638. create_directory(&current_path)?;
  639. // Finally, create a index.html file there with the page rendered
  640. let output = page.render_html(&self.tera, &self.config, &self.library.read().unwrap())?;
  641. create_file(&current_path.join("index.html"), &self.inject_livereload(output))?;
  642. // Copy any asset we found previously into the same directory as the index.html
  643. for asset in &page.assets {
  644. let asset_path = asset.as_path();
  645. copy(
  646. &asset_path,
  647. &current_path
  648. .join(asset_path.file_name().expect("Couldn't get filename from page asset")),
  649. )?;
  650. }
  651. Ok(())
  652. }
  653. /// Deletes the `public` directory and builds the site
  654. pub fn build(&self) -> Result<()> {
  655. self.clean()?;
  656. // Generate/move all assets before rendering any content
  657. if let Some(ref theme) = self.config.theme {
  658. let theme_path = self.base_path.join("themes").join(theme);
  659. if theme_path.join("sass").exists() {
  660. self.compile_sass(&theme_path)?;
  661. }
  662. }
  663. if self.config.compile_sass {
  664. self.compile_sass(&self.base_path)?;
  665. }
  666. if self.config.build_search_index {
  667. self.build_search_index()?;
  668. }
  669. // Render aliases first to allow overwriting
  670. self.render_aliases()?;
  671. self.render_sections()?;
  672. self.render_orphan_pages()?;
  673. self.render_sitemap()?;
  674. let library = self.library.read().unwrap();
  675. if self.config.generate_rss {
  676. let pages = if self.config.is_multilingual() {
  677. library
  678. .pages_values()
  679. .iter()
  680. .filter(|p| p.lang == self.config.default_language)
  681. .cloned()
  682. .collect()
  683. } else {
  684. library.pages_values()
  685. };
  686. self.render_rss_feed(pages, None)?;
  687. }
  688. for lang in &self.config.languages {
  689. if !lang.rss {
  690. continue;
  691. }
  692. let pages =
  693. library.pages_values().iter().filter(|p| p.lang == lang.code).cloned().collect();
  694. self.render_rss_feed(pages, Some(&PathBuf::from(lang.code.clone())))?;
  695. }
  696. self.render_404()?;
  697. self.render_robots()?;
  698. self.render_taxonomies()?;
  699. // We process images at the end as we might have picked up images to process from markdown
  700. // or from templates
  701. self.process_images()?;
  702. // Processed images will be in static so the last step is to copy it
  703. self.copy_static_directories()?;
  704. Ok(())
  705. }
  706. pub fn build_search_index(&self) -> Result<()> {
  707. ensure_directory_exists(&self.output_path)?;
  708. // index first
  709. create_file(
  710. &self.output_path.join(&format!("search_index.{}.js", self.config.default_language)),
  711. &format!(
  712. "window.searchIndex = {};",
  713. search::build_index(&self.config.default_language, &self.library.read().unwrap())?
  714. ),
  715. )?;
  716. for language in &self.config.languages {
  717. if language.code != self.config.default_language && language.search {
  718. create_file(
  719. &self.output_path.join(&format!("search_index.{}.js", &language.code)),
  720. &format!(
  721. "window.searchIndex = {};",
  722. search::build_index(&language.code, &self.library.read().unwrap())?
  723. ),
  724. )?;
  725. }
  726. }
  727. // then elasticlunr.min.js
  728. create_file(&self.output_path.join("elasticlunr.min.js"), search::ELASTICLUNR_JS)?;
  729. Ok(())
  730. }
  731. pub fn compile_sass(&self, base_path: &Path) -> Result<()> {
  732. ensure_directory_exists(&self.output_path)?;
  733. let sass_path = {
  734. let mut sass_path = PathBuf::from(base_path);
  735. sass_path.push("sass");
  736. sass_path
  737. };
  738. let mut options = SassOptions::default();
  739. options.output_style = OutputStyle::Compressed;
  740. let mut compiled_paths = self.compile_sass_glob(&sass_path, "scss", &options.clone())?;
  741. options.indented_syntax = true;
  742. compiled_paths.extend(self.compile_sass_glob(&sass_path, "sass", &options)?);
  743. compiled_paths.sort();
  744. for window in compiled_paths.windows(2) {
  745. if window[0].1 == window[1].1 {
  746. bail!(
  747. "SASS path conflict: \"{}\" and \"{}\" both compile to \"{}\"",
  748. window[0].0.display(),
  749. window[1].0.display(),
  750. window[0].1.display(),
  751. );
  752. }
  753. }
  754. Ok(())
  755. }
  756. fn compile_sass_glob(
  757. &self,
  758. sass_path: &Path,
  759. extension: &str,
  760. options: &SassOptions,
  761. ) -> Result<Vec<(PathBuf, PathBuf)>> {
  762. let glob_string = format!("{}/**/*.{}", sass_path.display(), extension);
  763. let files = glob(&glob_string)
  764. .expect("Invalid glob for sass")
  765. .filter_map(|e| e.ok())
  766. .filter(|entry| {
  767. !entry.as_path().file_name().unwrap().to_string_lossy().starts_with('_')
  768. })
  769. .collect::<Vec<_>>();
  770. let mut compiled_paths = Vec::new();
  771. for file in files {
  772. let css = compile_file(&file, options.clone())?;
  773. let path_inside_sass = file.strip_prefix(&sass_path).unwrap();
  774. let parent_inside_sass = path_inside_sass.parent();
  775. let css_output_path = self.output_path.join(path_inside_sass).with_extension("css");
  776. if parent_inside_sass.is_some() {
  777. create_dir_all(&css_output_path.parent().unwrap())?;
  778. }
  779. create_file(&css_output_path, &css)?;
  780. compiled_paths.push((path_inside_sass.to_owned(), css_output_path));
  781. }
  782. Ok(compiled_paths)
  783. }
  784. fn render_alias(&self, alias: &str, permalink: &str) -> Result<()> {
  785. let mut output_path = self.output_path.to_path_buf();
  786. let mut split = alias.split('/').collect::<Vec<_>>();
  787. // If the alias ends with an html file name, use that instead of mapping
  788. // as a path containing an `index.html`
  789. let page_name = match split.pop() {
  790. Some(part) if part.ends_with(".html") => part,
  791. Some(part) => {
  792. split.push(part);
  793. "index.html"
  794. }
  795. None => "index.html",
  796. };
  797. for component in split {
  798. output_path.push(&component);
  799. if !output_path.exists() {
  800. create_directory(&output_path)?;
  801. }
  802. }
  803. create_file(
  804. &output_path.join(page_name),
  805. &render_redirect_template(&permalink, &self.tera)?,
  806. )
  807. }
  808. pub fn render_aliases(&self) -> Result<()> {
  809. ensure_directory_exists(&self.output_path)?;
  810. let library = self.library.read().unwrap();
  811. for (_, page) in library.pages() {
  812. for alias in &page.meta.aliases {
  813. self.render_alias(&alias, &page.permalink)?;
  814. }
  815. }
  816. for (_, section) in library.sections() {
  817. for alias in &section.meta.aliases {
  818. self.render_alias(&alias, &section.permalink)?;
  819. }
  820. }
  821. Ok(())
  822. }
  823. /// Renders 404.html
  824. pub fn render_404(&self) -> Result<()> {
  825. ensure_directory_exists(&self.output_path)?;
  826. let mut context = Context::new();
  827. context.insert("config", &self.config);
  828. let output = render_template("404.html", &self.tera, context, &self.config.theme)?;
  829. create_file(&self.output_path.join("404.html"), &self.inject_livereload(output))
  830. }
  831. /// Renders robots.txt
  832. pub fn render_robots(&self) -> Result<()> {
  833. ensure_directory_exists(&self.output_path)?;
  834. let mut context = Context::new();
  835. context.insert("config", &self.config);
  836. create_file(
  837. &self.output_path.join("robots.txt"),
  838. &render_template("robots.txt", &self.tera, context, &self.config.theme)?,
  839. )
  840. }
  841. /// Renders all taxonomies
  842. pub fn render_taxonomies(&self) -> Result<()> {
  843. for taxonomy in &self.taxonomies {
  844. self.render_taxonomy(taxonomy)?;
  845. }
  846. Ok(())
  847. }
  848. fn render_taxonomy(&self, taxonomy: &Taxonomy) -> Result<()> {
  849. if taxonomy.items.is_empty() {
  850. return Ok(());
  851. }
  852. ensure_directory_exists(&self.output_path)?;
  853. let output_path = if taxonomy.kind.lang != self.config.default_language {
  854. let mid_path = self.output_path.join(&taxonomy.kind.lang);
  855. create_directory(&mid_path)?;
  856. mid_path.join(&taxonomy.kind.name)
  857. } else {
  858. self.output_path.join(&taxonomy.kind.name)
  859. };
  860. let list_output =
  861. taxonomy.render_all_terms(&self.tera, &self.config, &self.library.read().unwrap())?;
  862. create_directory(&output_path)?;
  863. create_file(&output_path.join("index.html"), &self.inject_livereload(list_output))?;
  864. let library = self.library.read().unwrap();
  865. taxonomy
  866. .items
  867. .par_iter()
  868. .map(|item| {
  869. let path = output_path.join(&item.slug);
  870. if taxonomy.kind.is_paginated() {
  871. self.render_paginated(
  872. &path,
  873. &Paginator::from_taxonomy(&taxonomy, item, &library),
  874. )?;
  875. } else {
  876. let single_output =
  877. taxonomy.render_term(item, &self.tera, &self.config, &library)?;
  878. create_directory(&path)?;
  879. create_file(&path.join("index.html"), &self.inject_livereload(single_output))?;
  880. }
  881. if taxonomy.kind.rss {
  882. self.render_rss_feed(
  883. item.pages.iter().map(|p| library.get_page_by_key(*p)).collect(),
  884. Some(&PathBuf::from(format!("{}/{}", taxonomy.kind.name, item.slug))),
  885. )
  886. } else {
  887. Ok(())
  888. }
  889. })
  890. .collect::<Result<()>>()
  891. }
  892. /// What it says on the tin
  893. pub fn render_sitemap(&self) -> Result<()> {
  894. ensure_directory_exists(&self.output_path)?;
  895. let library = self.library.read().unwrap();
  896. let all_sitemap_entries = {
  897. let mut all_sitemap_entries =
  898. sitemap::find_entries(&library, &self.taxonomies[..], &self.config);
  899. all_sitemap_entries.sort();
  900. all_sitemap_entries
  901. };
  902. let sitemap_limit = 30000;
  903. if all_sitemap_entries.len() < sitemap_limit {
  904. // Create single sitemap
  905. let mut context = Context::new();
  906. context.insert("entries", &all_sitemap_entries);
  907. let sitemap = &render_template("sitemap.xml", &self.tera, context, &self.config.theme)?;
  908. create_file(&self.output_path.join("sitemap.xml"), sitemap)?;
  909. return Ok(());
  910. }
  911. // Create multiple sitemaps (max 30000 urls each)
  912. let mut sitemap_index = Vec::new();
  913. for (i, chunk) in
  914. all_sitemap_entries.iter().collect::<Vec<_>>().chunks(sitemap_limit).enumerate()
  915. {
  916. let mut context = Context::new();
  917. context.insert("entries", &chunk);
  918. let sitemap = &render_template("sitemap.xml", &self.tera, context, &self.config.theme)?;
  919. let file_name = format!("sitemap{}.xml", i + 1);
  920. create_file(&self.output_path.join(&file_name), sitemap)?;
  921. let mut sitemap_url: String = self.config.make_permalink(&file_name);
  922. sitemap_url.pop(); // Remove trailing slash
  923. sitemap_index.push(sitemap_url);
  924. }
  925. // Create main sitemap that reference numbered sitemaps
  926. let mut main_context = Context::new();
  927. main_context.insert("sitemaps", &sitemap_index);
  928. let sitemap = &render_template(
  929. "split_sitemap_index.xml",
  930. &self.tera,
  931. main_context,
  932. &self.config.theme,
  933. )?;
  934. create_file(&self.output_path.join("sitemap.xml"), sitemap)?;
  935. Ok(())
  936. }
  937. /// Renders a RSS feed for the given path and at the given path
  938. /// If both arguments are `None`, it will render only the RSS feed for the whole
  939. /// site at the root folder.
  940. pub fn render_rss_feed(
  941. &self,
  942. all_pages: Vec<&Page>,
  943. base_path: Option<&PathBuf>,
  944. ) -> Result<()> {
  945. ensure_directory_exists(&self.output_path)?;
  946. let mut context = Context::new();
  947. let mut pages = all_pages.into_iter().filter(|p| p.meta.date.is_some()).collect::<Vec<_>>();
  948. // Don't generate a RSS feed if none of the pages has a date
  949. if pages.is_empty() {
  950. return Ok(());
  951. }
  952. pages.par_sort_unstable_by(sort_actual_pages_by_date);
  953. context.insert("last_build_date", &pages[0].meta.date.clone());
  954. let library = self.library.read().unwrap();
  955. // limit to the last n elements if the limit is set; otherwise use all.
  956. let num_entries = self.config.rss_limit.unwrap_or_else(|| pages.len());
  957. let p = pages
  958. .iter()
  959. .take(num_entries)
  960. .map(|x| x.to_serialized_basic(&library))
  961. .collect::<Vec<_>>();
  962. context.insert("pages", &p);
  963. context.insert("config", &self.config);
  964. let rss_feed_url = if let Some(ref base) = base_path {
  965. self.config.make_permalink(&base.join("rss.xml").to_string_lossy().replace('\\', "/"))
  966. } else {
  967. self.config.make_permalink("rss.xml")
  968. };
  969. context.insert("feed_url", &rss_feed_url);
  970. let feed = &render_template("rss.xml", &self.tera, context, &self.config.theme)?;
  971. if let Some(ref base) = base_path {
  972. let mut output_path = self.output_path.clone();
  973. for component in base.components() {
  974. output_path.push(component);
  975. if !output_path.exists() {
  976. create_directory(&output_path)?;
  977. }
  978. }
  979. create_file(&output_path.join("rss.xml"), feed)?;
  980. } else {
  981. create_file(&self.output_path.join("rss.xml"), feed)?;
  982. }
  983. Ok(())
  984. }
  985. /// Renders a single section
  986. pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> {
  987. ensure_directory_exists(&self.output_path)?;
  988. let mut output_path = self.output_path.clone();
  989. if section.lang != self.config.default_language {
  990. output_path.push(&section.lang);
  991. if !output_path.exists() {
  992. create_directory(&output_path)?;
  993. }
  994. }
  995. for component in &section.file.components {
  996. output_path.push(component);
  997. if !output_path.exists() {
  998. create_directory(&output_path)?;
  999. }
  1000. }
  1001. // Copy any asset we found previously into the same directory as the index.html
  1002. for asset in &section.assets {
  1003. let asset_path = asset.as_path();
  1004. copy(
  1005. &asset_path,
  1006. &output_path.join(
  1007. asset_path.file_name().expect("Failed to get asset filename for section"),
  1008. ),
  1009. )?;
  1010. }
  1011. if render_pages {
  1012. section
  1013. .pages
  1014. .par_iter()
  1015. .map(|k| self.render_page(self.library.read().unwrap().get_page_by_key(*k)))
  1016. .collect::<Result<()>>()?;
  1017. }
  1018. if !section.meta.render {
  1019. return Ok(());
  1020. }
  1021. if let Some(ref redirect_to) = section.meta.redirect_to {
  1022. let permalink = self.config.make_permalink(redirect_to);
  1023. create_file(
  1024. &output_path.join("index.html"),
  1025. &render_redirect_template(&permalink, &self.tera)?,
  1026. )?;
  1027. return Ok(());
  1028. }
  1029. if section.meta.is_paginated() {
  1030. self.render_paginated(
  1031. &output_path,
  1032. &Paginator::from_section(&section, &self.library.read().unwrap()),
  1033. )?;
  1034. } else {
  1035. let output =
  1036. section.render_html(&self.tera, &self.config, &self.library.read().unwrap())?;
  1037. create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
  1038. }
  1039. Ok(())
  1040. }
  1041. /// Used only on reload
  1042. pub fn render_index(&self) -> Result<()> {
  1043. self.render_section(
  1044. &self
  1045. .library
  1046. .read()
  1047. .unwrap()
  1048. .get_section(&self.content_path.join("_index.md"))
  1049. .expect("Failed to get index section"),
  1050. false,
  1051. )
  1052. }
  1053. /// Renders all sections
  1054. pub fn render_sections(&self) -> Result<()> {
  1055. self.library
  1056. .read()
  1057. .unwrap()
  1058. .sections_values()
  1059. .into_par_iter()
  1060. .map(|s| self.render_section(s, true))
  1061. .collect::<Result<()>>()
  1062. }
  1063. /// Renders all pages that do not belong to any sections
  1064. pub fn render_orphan_pages(&self) -> Result<()> {
  1065. ensure_directory_exists(&self.output_path)?;
  1066. let library = self.library.read().unwrap();
  1067. for page in library.get_all_orphan_pages() {
  1068. self.render_page(page)?;
  1069. }
  1070. Ok(())
  1071. }
  1072. /// Renders a list of pages when the section/index is wanting pagination.
  1073. pub fn render_paginated(&self, output_path: &Path, paginator: &Paginator) -> Result<()> {
  1074. ensure_directory_exists(&self.output_path)?;
  1075. let folder_path = output_path.join(&paginator.paginate_path);
  1076. create_directory(&folder_path)?;
  1077. paginator
  1078. .pagers
  1079. .par_iter()
  1080. .map(|pager| {
  1081. let page_path = folder_path.join(&format!("{}", pager.index));
  1082. create_directory(&page_path)?;
  1083. let output = paginator.render_pager(
  1084. pager,
  1085. &self.config,
  1086. &self.tera,
  1087. &self.library.read().unwrap(),
  1088. )?;
  1089. if pager.index > 1 {
  1090. create_file(&page_path.join("index.html"), &self.inject_livereload(output))?;
  1091. } else {
  1092. create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
  1093. create_file(
  1094. &page_path.join("index.html"),
  1095. &render_redirect_template(&paginator.permalink, &self.tera)?,
  1096. )?;
  1097. }
  1098. Ok(())
  1099. })
  1100. .collect::<Result<()>>()
  1101. }
  1102. }