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.

1236 lines
45KB

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