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.

590 lines
21KB

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