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.

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