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.

683 lines
24KB

  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, filters, global_fns};
  17. /// Renders the `internal/alias.html` template that will redirect
  18. /// via refresh to the url given
  19. fn render_alias(url: &str, tera: &Tera) -> Result<String> {
  20. let mut context = Context::new();
  21. context.add("url", &url);
  22. tera.render("internal/alias.html", &context)
  23. .chain_err(|| format!("Failed to render alias for '{}'", url))
  24. }
  25. #[derive(Debug, PartialEq)]
  26. enum RenderList {
  27. Tags,
  28. Categories,
  29. }
  30. /// A tag or category
  31. #[derive(Debug, Serialize, PartialEq)]
  32. struct ListItem {
  33. name: String,
  34. slug: String,
  35. count: usize,
  36. }
  37. impl ListItem {
  38. pub fn new(name: &str, count: usize) -> ListItem {
  39. ListItem {
  40. name: name.to_string(),
  41. slug: slugify(name),
  42. count: count,
  43. }
  44. }
  45. }
  46. #[derive(Debug)]
  47. pub struct Site {
  48. pub base_path: PathBuf,
  49. pub config: Config,
  50. pub pages: HashMap<PathBuf, Page>,
  51. pub sections: HashMap<PathBuf, Section>,
  52. pub tera: Tera,
  53. live_reload: bool,
  54. output_path: PathBuf,
  55. static_path: PathBuf,
  56. pub tags: HashMap<String, Vec<PathBuf>>,
  57. pub categories: HashMap<String, Vec<PathBuf>>,
  58. pub permalinks: HashMap<String, String>,
  59. }
  60. impl Site {
  61. /// Parse a site at the given path. Defaults to the current dir
  62. /// Passing in a path is only used in tests
  63. pub fn new<P: AsRef<Path>>(path: P, config_file: &str) -> Result<Site> {
  64. let path = path.as_ref();
  65. let tpl_glob = format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "templates/**/*.*ml");
  66. let mut tera = Tera::new(&tpl_glob).chain_err(|| "Error parsing templates")?;
  67. tera.extend(&GUTENBERG_TERA)?;
  68. tera.register_filter("markdown", filters::markdown);
  69. tera.register_filter("base64_encode", filters::base64_encode);
  70. tera.register_filter("base64_decode", filters::base64_decode);
  71. let site = Site {
  72. base_path: path.to_path_buf(),
  73. config: get_config(path, config_file),
  74. pages: HashMap::new(),
  75. sections: HashMap::new(),
  76. tera: tera,
  77. live_reload: false,
  78. output_path: path.join("public"),
  79. static_path: path.join("static"),
  80. tags: HashMap::new(),
  81. categories: HashMap::new(),
  82. permalinks: HashMap::new(),
  83. };
  84. Ok(site)
  85. }
  86. /// What the function name says
  87. pub fn enable_live_reload(&mut self) {
  88. self.live_reload = true;
  89. }
  90. /// Gets the path of all ignored pages in the site
  91. pub fn get_ignored_pages(&self) -> Vec<PathBuf> {
  92. self.sections
  93. .values()
  94. .flat_map(|s| s.ignored_pages.iter().map(|p| p.file_path.clone()))
  95. .collect()
  96. }
  97. /// Get all the orphan (== without section) pages in the site
  98. pub fn get_all_orphan_pages(&self) -> Vec<&Page> {
  99. let mut pages_in_sections = vec![];
  100. let mut orphans = vec![];
  101. for s in self.sections.values() {
  102. pages_in_sections.extend(s.all_pages_path());
  103. }
  104. for page in self.pages.values() {
  105. if !pages_in_sections.contains(&page.file_path) {
  106. orphans.push(page);
  107. }
  108. }
  109. orphans
  110. }
  111. /// Used by tests to change the output path to a tmp dir
  112. #[doc(hidden)]
  113. pub fn set_output_path<P: AsRef<Path>>(&mut self, path: P) {
  114. self.output_path = path.as_ref().to_path_buf();
  115. }
  116. /// Reads all .md files in the `content` directory and create pages/sections
  117. /// out of them
  118. pub fn load(&mut self) -> Result<()> {
  119. let base_path = self.base_path.to_string_lossy().replace("\\", "/");
  120. let content_glob = format!("{}/{}", base_path, "content/**/*.md");
  121. for entry in glob(&content_glob).unwrap().filter_map(|e| e.ok()) {
  122. let path = entry.as_path();
  123. if path.file_name().unwrap() == "_index.md" {
  124. self.add_section(path)?;
  125. } else {
  126. self.add_page(path)?;
  127. }
  128. }
  129. // Insert a default index section so we don't need to create a _index.md to render
  130. // the index page
  131. let index_path = self.base_path.join("content").join("_index.md");
  132. if !self.sections.contains_key(&index_path) {
  133. let mut index_section = Section::default();
  134. index_section.permalink = self.config.make_permalink("");
  135. self.sections.insert(index_path, index_section);
  136. }
  137. // A map of all .md files (section and pages) and their permalink
  138. // We need that if there are relative links in the content that need to be resolved
  139. let mut permalinks = HashMap::new();
  140. for page in self.pages.values() {
  141. permalinks.insert(page.relative_path.clone(), page.permalink.clone());
  142. }
  143. for section in self.sections.values() {
  144. permalinks.insert(section.relative_path.clone(), section.permalink.clone());
  145. }
  146. for page in self.pages.values_mut() {
  147. page.render_markdown(&permalinks, &self.tera, &self.config)?;
  148. }
  149. for section in self.sections.values_mut() {
  150. section.render_markdown(&permalinks, &self.tera, &self.config)?;
  151. }
  152. self.permalinks = permalinks;
  153. self.populate_sections();
  154. self.populate_tags_and_categories();
  155. self.tera.register_global_function("get_page", global_fns::make_get_page(&self.pages));
  156. Ok(())
  157. }
  158. /// Simple wrapper fn to avoid repeating that code in several places
  159. fn add_page(&mut self, path: &Path) -> Result<()> {
  160. let page = Page::from_file(&path, &self.config)?;
  161. self.pages.insert(page.file_path.clone(), page);
  162. Ok(())
  163. }
  164. /// Simple wrapper fn to avoid repeating that code in several places
  165. fn add_section(&mut self, path: &Path) -> Result<()> {
  166. let section = Section::from_file(path, &self.config)?;
  167. self.sections.insert(section.file_path.clone(), section);
  168. Ok(())
  169. }
  170. /// Called in serve, add the section and render it
  171. fn add_section_and_render(&mut self, path: &Path) -> Result<()> {
  172. self.add_section(path)?;
  173. let mut section = self.sections.get_mut(path).unwrap();
  174. self.permalinks.insert(section.relative_path.clone(), section.permalink.clone());
  175. section.render_markdown(&self.permalinks, &self.tera, &self.config)?;
  176. Ok(())
  177. }
  178. /// Called in serve, add a page again updating permalinks and its content
  179. /// The bool in the result is whether the front matter has been updated or not
  180. /// TODO: the above is very confusing, change that
  181. fn add_page_and_render(&mut self, path: &Path) -> Result<(bool, Page)> {
  182. let existing_page = self.pages.get(path).cloned();
  183. self.add_page(path)?;
  184. let mut page = self.pages.get_mut(path).unwrap();
  185. self.permalinks.insert(page.relative_path.clone(), page.permalink.clone());
  186. page.render_markdown(&self.permalinks, &self.tera, &self.config)?;
  187. if let Some(prev_page) = existing_page {
  188. return Ok((prev_page.meta != page.meta, page.clone()));
  189. }
  190. Ok((true, page.clone()))
  191. }
  192. /// Find out the direct subsections of each subsection if there are some
  193. /// as well as the pages for each section
  194. pub fn populate_sections(&mut self) {
  195. for page in self.pages.values() {
  196. if self.sections.contains_key(&page.parent_path.join("_index.md")) {
  197. self.sections.get_mut(&page.parent_path.join("_index.md")).unwrap().pages.push(page.clone());
  198. }
  199. }
  200. let mut grandparent_paths = HashMap::new();
  201. for section in self.sections.values() {
  202. if let Some(grand_parent) = section.parent_path.parent() {
  203. grandparent_paths.entry(grand_parent.to_path_buf()).or_insert_with(|| vec![]).push(section.clone());
  204. }
  205. }
  206. for section in self.sections.values_mut() {
  207. // TODO: avoid this clone
  208. let (mut sorted_pages, cannot_be_sorted_pages) = sort_pages(section.pages.clone(), section.meta.sort_by());
  209. sorted_pages = populate_previous_and_next_pages(&sorted_pages);
  210. section.pages = sorted_pages;
  211. section.ignored_pages = cannot_be_sorted_pages;
  212. match grandparent_paths.get(&section.parent_path) {
  213. Some(paths) => section.subsections.extend(paths.clone()),
  214. None => continue,
  215. };
  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. pub 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. pub fn rebuild_after_content_change(&mut self, path: &Path) -> Result<()> {
  289. let is_section = path.ends_with("_index.md");
  290. if path.exists() {
  291. // file exists, either a new one or updating content
  292. if is_section {
  293. self.add_section_and_render(path)?;
  294. self.render_sections()?;
  295. } else {
  296. // probably just an update so just re-parse that page
  297. let (frontmatter_changed, page) = self.add_page_and_render(path)?;
  298. // TODO: can probably be smarter and check what changed
  299. if frontmatter_changed {
  300. self.populate_sections();
  301. self.populate_tags_and_categories();
  302. self.build()?;
  303. } else {
  304. self.render_page(&page)?;
  305. }
  306. }
  307. } else {
  308. // File doesn't exist -> a deletion so we remove it from everything
  309. let relative_path = if is_section {
  310. self.sections[path].relative_path.clone()
  311. } else {
  312. self.pages[path].relative_path.clone()
  313. };
  314. self.permalinks.remove(&relative_path);
  315. if is_section {
  316. self.sections.remove(path);
  317. } else {
  318. self.pages.remove(path);
  319. }
  320. // TODO: probably no need to do that, we should be able to only re-render a page or a section.
  321. self.populate_sections();
  322. self.populate_tags_and_categories();
  323. self.build()?;
  324. }
  325. Ok(())
  326. }
  327. pub fn rebuild_after_template_change(&mut self, path: &Path) -> Result<()> {
  328. self.tera.full_reload()?;
  329. match path.file_name().unwrap().to_str().unwrap() {
  330. "sitemap.xml" => self.render_sitemap(),
  331. "rss.xml" => self.render_rss_feed(),
  332. _ => self.build() // TODO: change that
  333. }
  334. }
  335. /// Renders a single content page
  336. pub fn render_page(&self, page: &Page) -> Result<()> {
  337. self.ensure_public_directory_exists()?;
  338. // Copy the nesting of the content directory if we have sections for that page
  339. let mut current_path = self.output_path.to_path_buf();
  340. for component in page.path.split('/') {
  341. current_path.push(component);
  342. if !current_path.exists() {
  343. create_directory(&current_path)?;
  344. }
  345. }
  346. // Make sure the folder exists
  347. create_directory(&current_path)?;
  348. // Finally, create a index.html file there with the page rendered
  349. let output = page.render_html(&self.tera, &self.config)?;
  350. create_file(current_path.join("index.html"), &self.inject_livereload(output))?;
  351. // Copy any asset we found previously into the same directory as the index.html
  352. for asset in &page.assets {
  353. let asset_path = asset.as_path();
  354. copy(&asset_path, &current_path.join(asset_path.file_name().unwrap()))?;
  355. }
  356. Ok(())
  357. }
  358. /// Builds the site to the `public` directory after deleting it
  359. pub fn build(&self) -> Result<()> {
  360. self.clean()?;
  361. self.render_sections()?;
  362. self.render_orphan_pages()?;
  363. self.render_sitemap()?;
  364. if self.config.generate_rss.unwrap() {
  365. self.render_rss_feed()?;
  366. }
  367. self.render_robots()?;
  368. if self.config.generate_categories_pages.unwrap() {
  369. self.render_categories_and_tags(RenderList::Categories)?;
  370. }
  371. if self.config.generate_tags_pages.unwrap() {
  372. self.render_categories_and_tags(RenderList::Tags)?;
  373. }
  374. self.copy_static_directory()
  375. }
  376. /// Renders robots.txt
  377. fn render_robots(&self) -> Result<()> {
  378. self.ensure_public_directory_exists()?;
  379. create_file(
  380. self.output_path.join("robots.txt"),
  381. &self.tera.render("robots.txt", &Context::new())?
  382. )
  383. }
  384. /// Render the /{categories, list} pages and each individual category/tag page
  385. /// They are the same thing fundamentally, a list of pages with something in common
  386. fn render_categories_and_tags(&self, kind: RenderList) -> Result<()> {
  387. let items = match kind {
  388. RenderList::Categories => &self.categories,
  389. RenderList::Tags => &self.tags,
  390. };
  391. if items.is_empty() {
  392. return Ok(());
  393. }
  394. let (list_tpl_name, single_tpl_name, name, var_name) = if kind == RenderList::Categories {
  395. ("categories.html", "category.html", "categories", "category")
  396. } else {
  397. ("tags.html", "tag.html", "tags", "tag")
  398. };
  399. self.ensure_public_directory_exists()?;
  400. // Create the categories/tags directory first
  401. let public = self.output_path.clone();
  402. let mut output_path = public.to_path_buf();
  403. output_path.push(name);
  404. create_directory(&output_path)?;
  405. // Then render the index page for that kind.
  406. // We sort by number of page in that category/tag
  407. let mut sorted_items = vec![];
  408. for (item, count) in Vec::from_iter(items).into_iter().map(|(a, b)| (a, b.len())) {
  409. sorted_items.push(ListItem::new(item, count));
  410. }
  411. sorted_items.sort_by(|a, b| b.count.cmp(&a.count));
  412. let mut context = Context::new();
  413. context.add(name, &sorted_items);
  414. context.add("config", &self.config);
  415. context.add("current_url", &self.config.make_permalink(name));
  416. context.add("current_path", &format!("/{}", name));
  417. // And render it immediately
  418. let list_output = self.tera.render(list_tpl_name, &context)?;
  419. create_file(output_path.join("index.html"), &self.inject_livereload(list_output))?;
  420. // Now, each individual item
  421. for (item_name, pages_paths) in items.iter() {
  422. let pages: Vec<&Page> = self.pages
  423. .iter()
  424. .filter(|&(path, _)| pages_paths.contains(path))
  425. .map(|(_, page)| page)
  426. .collect();
  427. // TODO: how to sort categories and tag content?
  428. // Have a setting in config.toml or a _category.md and _tag.md
  429. // The latter is more in line with the rest of Gutenberg but order ordering
  430. // doesn't really work across sections.
  431. let mut context = Context::new();
  432. let slug = slugify(&item_name);
  433. context.add(var_name, &item_name);
  434. context.add(&format!("{}_slug", var_name), &slug);
  435. context.add("pages", &pages);
  436. context.add("config", &self.config);
  437. context.add("current_url", &self.config.make_permalink(&format!("{}/{}", name, slug)));
  438. context.add("current_path", &format!("/{}/{}", name, slug));
  439. let single_output = self.tera.render(single_tpl_name, &context)?;
  440. create_directory(&output_path.join(&slug))?;
  441. create_file(
  442. output_path.join(&slug).join("index.html"),
  443. &self.inject_livereload(single_output)
  444. )?;
  445. }
  446. Ok(())
  447. }
  448. fn render_sitemap(&self) -> Result<()> {
  449. self.ensure_public_directory_exists()?;
  450. let mut context = Context::new();
  451. context.add("pages", &self.pages.values().collect::<Vec<&Page>>());
  452. context.add("sections", &self.sections.values().collect::<Vec<&Section>>());
  453. let mut categories = vec![];
  454. if self.config.generate_categories_pages.unwrap() && !self.categories.is_empty() {
  455. categories.push(self.config.make_permalink("categories"));
  456. for category in self.categories.keys() {
  457. categories.push(
  458. self.config.make_permalink(&format!("categories/{}", slugify(category)))
  459. );
  460. }
  461. }
  462. context.add("categories", &categories);
  463. let mut tags = vec![];
  464. if self.config.generate_tags_pages.unwrap() && !self.tags.is_empty() {
  465. tags.push(self.config.make_permalink("tags"));
  466. for tag in self.tags.keys() {
  467. tags.push(
  468. self.config.make_permalink(&format!("tags/{}", slugify(tag)))
  469. );
  470. }
  471. }
  472. context.add("tags", &tags);
  473. let sitemap = self.tera.render("sitemap.xml", &context)?;
  474. create_file(self.output_path.join("sitemap.xml"), &sitemap)?;
  475. Ok(())
  476. }
  477. fn render_rss_feed(&self) -> Result<()> {
  478. self.ensure_public_directory_exists()?;
  479. let mut context = Context::new();
  480. let pages = self.pages.values()
  481. .filter(|p| p.meta.date.is_some())
  482. .take(15) // limit to the last 15 elements
  483. .cloned()
  484. .collect::<Vec<Page>>();
  485. // Don't generate a RSS feed if none of the pages has a date
  486. if pages.is_empty() {
  487. return Ok(());
  488. }
  489. context.add("last_build_date", &pages[0].meta.date);
  490. let (sorted_pages, _) = sort_pages(pages, SortBy::Date);
  491. context.add("pages", &sorted_pages);
  492. context.add("config", &self.config);
  493. let rss_feed_url = if self.config.base_url.ends_with('/') {
  494. format!("{}{}", self.config.base_url, "rss.xml")
  495. } else {
  496. format!("{}/{}", self.config.base_url, "rss.xml")
  497. };
  498. context.add("feed_url", &rss_feed_url);
  499. let sitemap = self.tera.render("rss.xml", &context)?;
  500. create_file(self.output_path.join("rss.xml"), &sitemap)?;
  501. Ok(())
  502. }
  503. /// Create a hashmap of paths to section
  504. /// For example `content/posts/_index.md` key will be `posts`
  505. fn get_sections_map(&self) -> HashMap<String, Section> {
  506. self.sections
  507. .values()
  508. .map(|s| (s.components.join("/"), s.clone()))
  509. .collect()
  510. }
  511. /// Renders a single section
  512. fn render_section(&self, section: &Section) -> Result<()> {
  513. self.ensure_public_directory_exists()?;
  514. let public = self.output_path.clone();
  515. let mut output_path = public.to_path_buf();
  516. for component in &section.components {
  517. output_path.push(component);
  518. if !output_path.exists() {
  519. create_directory(&output_path)?;
  520. }
  521. }
  522. for page in &section.pages {
  523. self.render_page(page)?;
  524. }
  525. if !section.meta.should_render() {
  526. return Ok(());
  527. }
  528. if section.meta.is_paginated() {
  529. self.render_paginated(&output_path, section)?;
  530. } else {
  531. let output = section.render_html(
  532. if section.is_index() { self.get_sections_map() } else { HashMap::new() },
  533. &self.tera,
  534. &self.config,
  535. )?;
  536. create_file(output_path.join("index.html"), &self.inject_livereload(output))?;
  537. }
  538. Ok(())
  539. }
  540. /// Renders all sections
  541. fn render_sections(&self) -> Result<()> {
  542. for section in self.sections.values() {
  543. self.render_section(section)?;
  544. }
  545. Ok(())
  546. }
  547. /// Renders all pages that do not belong to any sections
  548. fn render_orphan_pages(&self) -> Result<()> {
  549. self.ensure_public_directory_exists()?;
  550. for page in self.get_all_orphan_pages() {
  551. self.render_page(page)?;
  552. }
  553. Ok(())
  554. }
  555. /// Renders a list of pages when the section/index is wanting pagination.
  556. fn render_paginated(&self, output_path: &Path, section: &Section) -> Result<()> {
  557. self.ensure_public_directory_exists()?;
  558. let paginate_path = match section.meta.paginate_path {
  559. Some(ref s) => s.clone(),
  560. None => unreachable!()
  561. };
  562. let paginator = Paginator::new(&section.pages, section);
  563. for (i, pager) in paginator.pagers.iter().enumerate() {
  564. let folder_path = output_path.join(&paginate_path);
  565. let page_path = folder_path.join(&format!("{}", i + 1));
  566. create_directory(&folder_path)?;
  567. create_directory(&page_path)?;
  568. let output = paginator.render_pager(pager, self)?;
  569. if i > 0 {
  570. create_file(page_path.join("index.html"), &self.inject_livereload(output))?;
  571. } else {
  572. create_file(output_path.join("index.html"), &self.inject_livereload(output))?;
  573. create_file(page_path.join("index.html"), &render_alias(&section.permalink, &self.tera)?)?;
  574. }
  575. }
  576. Ok(())
  577. }
  578. }