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.

652 lines
23KB

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