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.

1215 lines
44KB

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