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.

654 lines
23KB

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