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.

626 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.tera.register_global_function("get_section", global_fns::make_get_section(&self.sections));
  116. self.register_get_url_fn();
  117. Ok(())
  118. }
  119. /// Separate fn as it can be called in the serve command
  120. pub fn register_get_url_fn(&mut self) {
  121. self.tera.register_global_function("get_url", global_fns::make_get_url(self.permalinks.clone()));
  122. }
  123. /// Add a page to the site
  124. /// The `render` parameter is used in the serve command, when rebuilding a page.
  125. /// If `true`, it will also render the markdown for that page
  126. /// Returns the previous page struct if there was one
  127. pub fn add_page(&mut self, path: &Path, render: bool) -> Result<Option<Page>> {
  128. let page = Page::from_file(&path, &self.config)?;
  129. self.permalinks.insert(page.file.relative.clone(), page.permalink.clone());
  130. let prev = self.pages.insert(page.file.path.clone(), page);
  131. if render {
  132. let insert_anchor = self.find_parent_section_insert_anchor(&self.pages[path].file.parent);
  133. let mut page = self.pages.get_mut(path).unwrap();
  134. page.render_markdown(&self.permalinks, &self.tera, &self.config, insert_anchor)?;
  135. }
  136. Ok(prev)
  137. }
  138. /// Add a section to the site
  139. /// The `render` parameter is used in the serve command, when rebuilding a page.
  140. /// If `true`, it will also render the markdown for that page
  141. /// Returns the previous section struct if there was one
  142. pub fn add_section(&mut self, path: &Path, render: bool) -> Result<Option<Section>> {
  143. let section = Section::from_file(path, &self.config)?;
  144. self.permalinks.insert(section.file.relative.clone(), section.permalink.clone());
  145. let prev = self.sections.insert(section.file.path.clone(), section);
  146. if render {
  147. let mut section = self.sections.get_mut(path).unwrap();
  148. section.render_markdown(&self.permalinks, &self.tera, &self.config)?;
  149. }
  150. Ok(prev)
  151. }
  152. /// Finds the insert_anchor for the parent section of the directory at `path`.
  153. /// Defaults to `AnchorInsert::None` if no parent section found
  154. pub fn find_parent_section_insert_anchor(&self, parent_path: &PathBuf) -> InsertAnchor {
  155. match self.sections.get(&parent_path.join("_index.md")) {
  156. Some(s) => s.meta.insert_anchor.unwrap(),
  157. None => InsertAnchor::None
  158. }
  159. }
  160. /// Find out the direct subsections of each subsection if there are some
  161. /// as well as the pages for each section
  162. pub fn populate_sections(&mut self) {
  163. let mut grandparent_paths = HashMap::new();
  164. for section in self.sections.values_mut() {
  165. if let Some(ref grand_parent) = section.file.grand_parent {
  166. grandparent_paths.entry(grand_parent.to_path_buf()).or_insert_with(|| vec![]).push(section.clone());
  167. }
  168. // Make sure the pages of a section are empty since we can call that many times on `serve`
  169. section.pages = vec![];
  170. section.ignored_pages = vec![];
  171. }
  172. for page in self.pages.values() {
  173. let parent_section_path = page.file.parent.join("_index.md");
  174. if self.sections.contains_key(&parent_section_path) {
  175. self.sections.get_mut(&parent_section_path).unwrap().pages.push(page.clone());
  176. }
  177. }
  178. for section in self.sections.values_mut() {
  179. match grandparent_paths.get(&section.file.parent) {
  180. Some(paths) => section.subsections.extend(paths.clone()),
  181. None => continue,
  182. };
  183. }
  184. self.sort_sections_pages(None);
  185. }
  186. /// Sorts the pages of the section at the given path
  187. /// By default will sort all sections but can be made to only sort a single one by providing a path
  188. pub fn sort_sections_pages(&mut self, only: Option<&Path>) {
  189. for (path, section) in &mut self.sections {
  190. if let Some(p) = only {
  191. if p != path {
  192. continue;
  193. }
  194. }
  195. let (sorted_pages, cannot_be_sorted_pages) = sort_pages(section.pages.clone(), section.meta.sort_by());
  196. section.pages = populate_previous_and_next_pages(&sorted_pages);
  197. section.ignored_pages = cannot_be_sorted_pages;
  198. }
  199. }
  200. /// Find all the tags and categories if it's asked in the config
  201. pub fn populate_tags_and_categories(&mut self) {
  202. let generate_tags_pages = self.config.generate_tags_pages.unwrap();
  203. let generate_categories_pages = self.config.generate_categories_pages.unwrap();
  204. if !generate_tags_pages && !generate_categories_pages {
  205. return;
  206. }
  207. // TODO: can we pass a reference?
  208. let (tags, categories) = Taxonomy::find_tags_and_categories(
  209. self.pages.values().cloned().collect::<Vec<_>>()
  210. );
  211. if generate_tags_pages {
  212. self.tags = Some(tags);
  213. }
  214. if generate_categories_pages {
  215. self.categories = Some(categories);
  216. }
  217. }
  218. /// Inject live reload script tag if in live reload mode
  219. fn inject_livereload(&self, html: String) -> String {
  220. if self.live_reload {
  221. return html.replace(
  222. "</body>",
  223. r#"<script src="/livereload.js?port=1112&mindelay=10"></script></body>"#
  224. );
  225. }
  226. html
  227. }
  228. /// Copy static file to public directory.
  229. pub fn copy_static_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
  230. let relative_path = path.as_ref().strip_prefix(&self.static_path).unwrap();
  231. let target_path = self.output_path.join(relative_path);
  232. if let Some(parent_directory) = target_path.parent() {
  233. create_dir_all(parent_directory)?;
  234. }
  235. copy(path.as_ref(), &target_path)?;
  236. Ok(())
  237. }
  238. /// Copy the content of the `static` folder into the `public` folder
  239. pub fn copy_static_directory(&self) -> Result<()> {
  240. for entry in WalkDir::new(&self.static_path).into_iter().filter_map(|e| e.ok()) {
  241. let relative_path = entry.path().strip_prefix(&self.static_path).unwrap();
  242. let target_path = self.output_path.join(relative_path);
  243. if entry.path().is_dir() {
  244. if !target_path.exists() {
  245. create_directory(&target_path)?;
  246. }
  247. } else {
  248. let entry_fullpath = self.base_path.join(entry.path());
  249. self.copy_static_file(entry_fullpath)?;
  250. }
  251. }
  252. Ok(())
  253. }
  254. /// Deletes the `public` directory if it exists
  255. pub fn clean(&self) -> Result<()> {
  256. if self.output_path.exists() {
  257. // Delete current `public` directory so we can start fresh
  258. remove_dir_all(&self.output_path).chain_err(|| "Couldn't delete `public` directory")?;
  259. }
  260. Ok(())
  261. }
  262. /// Renders a single content page
  263. pub fn render_page(&self, page: &Page) -> Result<()> {
  264. ensure_directory_exists(&self.output_path)?;
  265. // Copy the nesting of the content directory if we have sections for that page
  266. let mut current_path = self.output_path.to_path_buf();
  267. for component in page.path.split('/') {
  268. current_path.push(component);
  269. if !current_path.exists() {
  270. create_directory(&current_path)?;
  271. }
  272. }
  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. if taxonomy.items.is_empty() {
  325. return Ok(())
  326. }
  327. ensure_directory_exists(&self.output_path)?;
  328. let output_path = self.output_path.join(&taxonomy.get_list_name());
  329. let list_output = taxonomy.render_list(&self.tera, &self.config)?;
  330. create_directory(&output_path)?;
  331. create_file(&output_path.join("index.html"), &self.inject_livereload(list_output))?;
  332. for item in &taxonomy.items {
  333. let single_output = taxonomy.render_single_item(item, &self.tera, &self.config)?;
  334. create_directory(&output_path.join(&item.slug))?;
  335. create_file(
  336. &output_path.join(&item.slug).join("index.html"),
  337. &self.inject_livereload(single_output)
  338. )?;
  339. }
  340. Ok(())
  341. }
  342. /// What it says on the tin
  343. pub fn render_sitemap(&self) -> Result<()> {
  344. ensure_directory_exists(&self.output_path)?;
  345. let mut context = Context::new();
  346. context.add("pages", &self.pages.values().collect::<Vec<&Page>>());
  347. context.add("sections", &self.sections.values().collect::<Vec<&Section>>());
  348. let mut categories = vec![];
  349. if let Some(ref c) = self.categories {
  350. let name = c.get_list_name();
  351. categories.push(self.config.make_permalink(&name));
  352. for item in &c.items {
  353. categories.push(
  354. self.config.make_permalink(&format!("{}/{}", &name, item.slug))
  355. );
  356. }
  357. }
  358. context.add("categories", &categories);
  359. let mut tags = vec![];
  360. if let Some(ref t) = self.tags {
  361. let name = t.get_list_name();
  362. tags.push(self.config.make_permalink(&name));
  363. for item in &t.items {
  364. tags.push(
  365. self.config.make_permalink(&format!("{}/{}", &name, item.slug))
  366. );
  367. }
  368. }
  369. context.add("tags", &tags);
  370. let sitemap = self.tera.render("sitemap.xml", &context)?;
  371. create_file(&self.output_path.join("sitemap.xml"), &sitemap)?;
  372. Ok(())
  373. }
  374. pub fn render_rss_feed(&self) -> Result<()> {
  375. ensure_directory_exists(&self.output_path)?;
  376. let mut context = Context::new();
  377. let pages = self.pages.values()
  378. .filter(|p| p.meta.date.is_some())
  379. .take(self.config.rss_limit.unwrap()) // limit to the last n elements
  380. .cloned()
  381. .collect::<Vec<Page>>();
  382. // Don't generate a RSS feed if none of the pages has a date
  383. if pages.is_empty() {
  384. return Ok(());
  385. }
  386. context.add("last_build_date", &pages[0].meta.date);
  387. let (sorted_pages, _) = sort_pages(pages, SortBy::Date);
  388. context.add("pages", &sorted_pages);
  389. context.add("config", &self.config);
  390. let rss_feed_url = if self.config.base_url.ends_with('/') {
  391. format!("{}{}", self.config.base_url, "rss.xml")
  392. } else {
  393. format!("{}/{}", self.config.base_url, "rss.xml")
  394. };
  395. context.add("feed_url", &rss_feed_url);
  396. let sitemap = self.tera.render("rss.xml", &context)?;
  397. create_file(&self.output_path.join("rss.xml"), &sitemap)?;
  398. Ok(())
  399. }
  400. /// Create a hashmap of paths to section
  401. /// For example `content/posts/_index.md` key will be `posts`
  402. fn get_sections_map(&self) -> HashMap<String, Section> {
  403. self.sections
  404. .values()
  405. .map(|s| (s.file.components.join("/"), s.clone()))
  406. .collect()
  407. }
  408. /// Renders a single section
  409. pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> {
  410. ensure_directory_exists(&self.output_path)?;
  411. let public = self.output_path.clone();
  412. let mut output_path = public.to_path_buf();
  413. for component in &section.file.components {
  414. output_path.push(component);
  415. if !output_path.exists() {
  416. create_directory(&output_path)?;
  417. }
  418. }
  419. if render_pages {
  420. for page in &section.pages {
  421. self.render_page(page)?;
  422. }
  423. }
  424. if !section.meta.should_render() {
  425. return Ok(());
  426. }
  427. if section.meta.is_paginated() {
  428. self.render_paginated(&output_path, section)?;
  429. } else {
  430. let output = section.render_html(
  431. if section.is_index() { self.get_sections_map() } else { HashMap::new() },
  432. &self.tera,
  433. &self.config,
  434. )?;
  435. create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
  436. }
  437. Ok(())
  438. }
  439. pub fn render_index(&self) -> Result<()> {
  440. self.render_section(&self.sections[&self.base_path.join("content").join("_index.md")], false)
  441. }
  442. /// Renders all sections
  443. pub fn render_sections(&self) -> Result<()> {
  444. for section in self.sections.values() {
  445. self.render_section(section, true)?;
  446. }
  447. Ok(())
  448. }
  449. /// Renders all pages that do not belong to any sections
  450. pub fn render_orphan_pages(&self) -> Result<()> {
  451. ensure_directory_exists(&self.output_path)?;
  452. for page in self.get_all_orphan_pages() {
  453. self.render_page(page)?;
  454. }
  455. Ok(())
  456. }
  457. /// Renders a list of pages when the section/index is wanting pagination.
  458. fn render_paginated(&self, output_path: &Path, section: &Section) -> Result<()> {
  459. ensure_directory_exists(&self.output_path)?;
  460. let paginate_path = match section.meta.paginate_path {
  461. Some(ref s) => s.clone(),
  462. None => unreachable!()
  463. };
  464. let paginator = Paginator::new(&section.pages, section);
  465. for (i, pager) in paginator.pagers.iter().enumerate() {
  466. let folder_path = output_path.join(&paginate_path);
  467. let page_path = folder_path.join(&format!("{}", i + 1));
  468. create_directory(&folder_path)?;
  469. create_directory(&page_path)?;
  470. let output = paginator.render_pager(pager, self)?;
  471. if i > 0 {
  472. create_file(&page_path.join("index.html"), &self.inject_livereload(output))?;
  473. } else {
  474. create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
  475. create_file(&page_path.join("index.html"), &render_redirect_template(&section.permalink, &self.tera)?)?;
  476. }
  477. }
  478. Ok(())
  479. }
  480. }
  481. /// Resolves an internal link (of the `./posts/something.md#hey` sort) to its absolute link
  482. pub fn resolve_internal_link(link: &str, permalinks: &HashMap<String, String>) -> Result<String> {
  483. // First we remove the ./ since that's gutenberg specific
  484. let clean_link = link.replacen("./", "", 1);
  485. // Then we remove any potential anchor
  486. // parts[0] will be the file path and parts[1] the anchor if present
  487. let parts = clean_link.split('#').collect::<Vec<_>>();
  488. match permalinks.get(parts[0]) {
  489. Some(p) => {
  490. if parts.len() > 1 {
  491. Ok(format!("{}#{}", p, parts[1]))
  492. } else {
  493. Ok(p.to_string())
  494. }
  495. },
  496. None => bail!(format!("Relative link {} not found.", link)),
  497. }
  498. }
  499. #[cfg(test)]
  500. mod tests {
  501. use std::collections::HashMap;
  502. use super::resolve_internal_link;
  503. #[test]
  504. fn can_resolve_valid_internal_link() {
  505. let mut permalinks = HashMap::new();
  506. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
  507. let res = resolve_internal_link("./pages/about.md", &permalinks).unwrap();
  508. assert_eq!(res, "https://vincent.is/about");
  509. }
  510. #[test]
  511. fn can_resolve_internal_links_with_anchors() {
  512. let mut permalinks = HashMap::new();
  513. permalinks.insert("pages/about.md".to_string(), "https://vincent.is/about".to_string());
  514. let res = resolve_internal_link("./pages/about.md#hello", &permalinks).unwrap();
  515. assert_eq!(res, "https://vincent.is/about#hello");
  516. }
  517. #[test]
  518. fn errors_resolve_inexistant_internal_link() {
  519. let res = resolve_internal_link("./pages/about.md#hello", &HashMap::new());
  520. assert!(res.is_err());
  521. }
  522. }