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.

622 lines
22KB

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