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.

604 lines
21KB

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