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.

499 lines
17KB

  1. use std::collections::{BTreeMap, HashMap};
  2. use std::iter::FromIterator;
  3. use std::fs::{remove_dir_all, copy, remove_file};
  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};
  12. use utils::{create_file, create_directory};
  13. use section::{Section};
  14. lazy_static! {
  15. static ref GUTENBERG_TERA: Tera = {
  16. let mut tera = Tera::default();
  17. tera.add_raw_templates(vec![
  18. ("rss.xml", include_str!("templates/rss.xml")),
  19. ("sitemap.xml", include_str!("templates/sitemap.xml")),
  20. ("robots.txt", include_str!("templates/robots.txt")),
  21. ]).unwrap();
  22. tera
  23. };
  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: BTreeMap<PathBuf, Section>,
  52. pub templates: Tera,
  53. live_reload: bool,
  54. output_path: PathBuf,
  55. pub tags: HashMap<String, Vec<PathBuf>>,
  56. pub categories: HashMap<String, Vec<PathBuf>>,
  57. }
  58. impl Site {
  59. /// Parse a site at the given path. Defaults to the current dir
  60. /// Passing in a path is only used in tests
  61. pub fn new<P: AsRef<Path>>(path: P, config_file: &str) -> Result<Site> {
  62. let path = path.as_ref();
  63. let tpl_glob = format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "templates/**/*");
  64. let mut tera = Tera::new(&tpl_glob).chain_err(|| "Error parsing templates")?;
  65. tera.extend(&GUTENBERG_TERA)?;
  66. let site = Site {
  67. base_path: path.to_path_buf(),
  68. config: get_config(path, config_file),
  69. pages: HashMap::new(),
  70. sections: BTreeMap::new(),
  71. templates: tera,
  72. live_reload: false,
  73. output_path: PathBuf::from("public"),
  74. tags: HashMap::new(),
  75. categories: HashMap::new(),
  76. };
  77. Ok(site)
  78. }
  79. /// What the function name says
  80. pub fn enable_live_reload(&mut self) {
  81. self.live_reload = true;
  82. }
  83. /// Used by tests to change the output path to a tmp dir
  84. #[doc(hidden)]
  85. pub fn set_output_path<P: AsRef<Path>>(&mut self, path: P) {
  86. self.output_path = path.as_ref().to_path_buf();
  87. }
  88. /// Reads all .md files in the `content` directory and create pages/sections
  89. /// out of them
  90. pub fn load(&mut self) -> Result<()> {
  91. let path = self.base_path.to_string_lossy().replace("\\", "/");
  92. let content_glob = format!("{}/{}", path, "content/**/*.md");
  93. // TODO: make that parallel, that's the main bottleneck
  94. // `add_section` and `add_page` can't be used in the parallel version afaik
  95. for entry in glob(&content_glob).unwrap().filter_map(|e| e.ok()) {
  96. let path = entry.as_path();
  97. if path.file_name().unwrap() == "_index.md" {
  98. self.add_section(path)?;
  99. } else {
  100. self.add_page(path)?;
  101. }
  102. }
  103. self.populate_sections();
  104. self.populate_tags_and_categories();
  105. Ok(())
  106. }
  107. /// Simple wrapper fn to avoid repeating that code in several places
  108. fn add_page(&mut self, path: &Path) -> Result<()> {
  109. let page = Page::from_file(&path, &self.config)?;
  110. self.pages.insert(page.file_path.clone(), page);
  111. Ok(())
  112. }
  113. /// Simple wrapper fn to avoid repeating that code in several places
  114. fn add_section(&mut self, path: &Path) -> Result<()> {
  115. let section = Section::from_file(path, &self.config)?;
  116. self.sections.insert(section.parent_path.clone(), section);
  117. Ok(())
  118. }
  119. /// Find out the direct subsections of each subsection if there are some
  120. /// as well as the pages for each section
  121. fn populate_sections(&mut self) {
  122. for page in self.pages.values() {
  123. if self.sections.contains_key(&page.parent_path) {
  124. self.sections.get_mut(&page.parent_path).unwrap().pages.push(page.clone());
  125. }
  126. }
  127. let mut grandparent_paths = HashMap::new();
  128. for section in self.sections.values() {
  129. let grand_parent = section.parent_path.parent().unwrap().to_path_buf();
  130. grandparent_paths.entry(grand_parent).or_insert_with(|| vec![]).push(section.clone());
  131. }
  132. for (parent_path, section) in &mut self.sections {
  133. section.pages.sort_by(|a, b| a.partial_cmp(b).unwrap());
  134. section.pages = populate_previous_and_next_pages(section.pages.as_slice(), true);
  135. match grandparent_paths.get(parent_path) {
  136. Some(paths) => section.subsections.extend(paths.clone()),
  137. None => continue,
  138. };
  139. }
  140. }
  141. /// Separated from `parse` for easier testing
  142. pub fn populate_tags_and_categories(&mut self) {
  143. for page in self.pages.values() {
  144. if let Some(ref category) = page.meta.category {
  145. self.categories
  146. .entry(category.to_string())
  147. .or_insert_with(|| vec![])
  148. .push(page.file_path.clone());
  149. }
  150. if let Some(ref tags) = page.meta.tags {
  151. for tag in tags {
  152. self.tags
  153. .entry(tag.to_string())
  154. .or_insert_with(|| vec![])
  155. .push(page.file_path.clone());
  156. }
  157. }
  158. }
  159. }
  160. /// Inject live reload script tag if in live reload mode
  161. fn inject_livereload(&self, html: String) -> String {
  162. if self.live_reload {
  163. return html.replace(
  164. "</body>",
  165. r#"<script src="/livereload.js?port=1112&mindelay=10"></script></body>"#
  166. );
  167. }
  168. html
  169. }
  170. /// Copy the content of the `static` folder into the `public` folder
  171. ///
  172. /// TODO: only copy one file if possible because that would be a waste
  173. /// to do re-copy the whole thing. Benchmark first to see if it's a big difference
  174. pub fn copy_static_directory(&self) -> Result<()> {
  175. let from = Path::new("static");
  176. let target = Path::new("public");
  177. for entry in WalkDir::new(from).into_iter().filter_map(|e| e.ok()) {
  178. let relative_path = entry.path().strip_prefix(&from).unwrap();
  179. let target_path = {
  180. let mut target_path = target.to_path_buf();
  181. target_path.push(relative_path);
  182. target_path
  183. };
  184. if entry.path().is_dir() {
  185. if !target_path.exists() {
  186. create_directory(&target_path)?;
  187. }
  188. } else {
  189. if target_path.exists() {
  190. remove_file(&target_path)?;
  191. }
  192. copy(entry.path(), &target_path)?;
  193. }
  194. }
  195. Ok(())
  196. }
  197. /// Deletes the `public` directory if it exists
  198. pub fn clean(&self) -> Result<()> {
  199. if Path::new("public").exists() {
  200. // Delete current `public` directory so we can start fresh
  201. remove_dir_all("public").chain_err(|| "Couldn't delete `public` directory")?;
  202. }
  203. Ok(())
  204. }
  205. pub fn rebuild_after_content_change(&mut self, path: &Path) -> Result<()> {
  206. if path.exists() {
  207. // file exists, either a new one or updating content
  208. if self.pages.contains_key(path) {
  209. if path.ends_with("_index.md") {
  210. self.add_section(path)?;
  211. } else {
  212. // probably just an update so just re-parse that page
  213. self.add_page(path)?;
  214. }
  215. } else {
  216. // new file?
  217. self.add_page(path)?;
  218. }
  219. } else {
  220. // File doesn't exist -> a deletion so we remove it from
  221. self.pages.remove(path);
  222. }
  223. self.populate_sections();
  224. self.populate_tags_and_categories();
  225. self.build()
  226. }
  227. pub fn rebuild_after_template_change(&mut self) -> Result<()> {
  228. self.templates.full_reload()?;
  229. self.build_pages()
  230. }
  231. pub fn build_pages(&self) -> Result<()> {
  232. let public = self.output_path.clone();
  233. if !public.exists() {
  234. create_directory(&public)?;
  235. }
  236. let mut pages = vec![];
  237. // First we render the pages themselves
  238. for page in self.pages.values() {
  239. // Copy the nesting of the content directory if we have sections for that page
  240. let mut current_path = public.to_path_buf();
  241. for component in page.url.split('/') {
  242. current_path.push(component);
  243. if !current_path.exists() {
  244. create_directory(&current_path)?;
  245. }
  246. }
  247. // Make sure the folder exists
  248. create_directory(&current_path)?;
  249. // Finally, create a index.html file there with the page rendered
  250. let output = page.render_html(&self.templates, &self.config)?;
  251. create_file(current_path.join("index.html"), &self.inject_livereload(output))?;
  252. // Copy any asset we found previously into the same directory as the index.html
  253. for asset in &page.assets {
  254. let asset_path = asset.as_path();
  255. copy(&asset_path, &current_path.join(asset_path.file_name().unwrap()))?;
  256. }
  257. pages.push(page.clone());
  258. }
  259. // Outputting categories and pages
  260. if self.config.generate_categories_pages.unwrap() {
  261. self.render_categories_and_tags(RenderList::Categories)?;
  262. }
  263. if self.config.generate_tags_pages.unwrap() {
  264. self.render_categories_and_tags(RenderList::Tags)?;
  265. }
  266. // And finally the index page
  267. let mut context = Context::new();
  268. pages.sort_by(|a, b| a.partial_cmp(b).unwrap());
  269. context.add("pages", &populate_previous_and_next_pages(&pages, false));
  270. context.add("config", &self.config);
  271. let index = self.templates.render("index.html", &context)?;
  272. create_file(public.join("index.html"), &self.inject_livereload(index))?;
  273. Ok(())
  274. }
  275. /// Builds the site to the `public` directory after deleting it
  276. pub fn build(&self) -> Result<()> {
  277. self.clean()?;
  278. self.build_pages()?;
  279. self.render_sitemap()?;
  280. if self.config.generate_rss.unwrap() {
  281. self.render_rss_feed()?;
  282. }
  283. self.render_robots()?;
  284. self.render_sections()?;
  285. self.copy_static_directory()
  286. }
  287. fn render_robots(&self) -> Result<()> {
  288. create_file(
  289. self.output_path.join("robots.txt"),
  290. &self.templates.render("robots.txt", &Context::new())?
  291. )
  292. }
  293. /// Render the /{categories, list} pages and each individual category/tag page
  294. /// They are the same thing fundamentally, a list of pages with something in common
  295. fn render_categories_and_tags(&self, kind: RenderList) -> Result<()> {
  296. let items = match kind {
  297. RenderList::Categories => &self.categories,
  298. RenderList::Tags => &self.tags,
  299. };
  300. if items.is_empty() {
  301. return Ok(());
  302. }
  303. let (list_tpl_name, single_tpl_name, name, var_name) = if kind == RenderList::Categories {
  304. ("categories.html", "category.html", "categories", "category")
  305. } else {
  306. ("tags.html", "tag.html", "tags", "tag")
  307. };
  308. // Create the categories/tags directory first
  309. let public = self.output_path.clone();
  310. let mut output_path = public.to_path_buf();
  311. output_path.push(name);
  312. create_directory(&output_path)?;
  313. // Then render the index page for that kind.
  314. // We sort by number of page in that category/tag
  315. let mut sorted_items = vec![];
  316. for (item, count) in Vec::from_iter(items).into_iter().map(|(a, b)| (a, b.len())) {
  317. sorted_items.push(ListItem::new(item, count));
  318. }
  319. sorted_items.sort_by(|a, b| b.count.cmp(&a.count));
  320. let mut context = Context::new();
  321. context.add(name, &sorted_items);
  322. context.add("config", &self.config);
  323. // And render it immediately
  324. let list_output = self.templates.render(list_tpl_name, &context)?;
  325. create_file(output_path.join("index.html"), &self.inject_livereload(list_output))?;
  326. // Now, each individual item
  327. for (item_name, pages_paths) in items.iter() {
  328. let mut pages: Vec<&Page> = self.pages
  329. .iter()
  330. .filter(|&(path, _)| pages_paths.contains(path))
  331. .map(|(_, page)| page)
  332. .collect();
  333. pages.sort_by(|a, b| a.partial_cmp(b).unwrap());
  334. let mut context = Context::new();
  335. let slug = slugify(&item_name);
  336. context.add(var_name, &item_name);
  337. context.add(&format!("{}_slug", var_name), &slug);
  338. context.add("pages", &pages);
  339. context.add("config", &self.config);
  340. let single_output = self.templates.render(single_tpl_name, &context)?;
  341. create_directory(&output_path.join(&slug))?;
  342. create_file(
  343. output_path.join(&slug).join("index.html"),
  344. &self.inject_livereload(single_output)
  345. )?;
  346. }
  347. Ok(())
  348. }
  349. fn render_sitemap(&self) -> Result<()> {
  350. let mut context = Context::new();
  351. context.add("pages", &self.pages.values().collect::<Vec<&Page>>());
  352. context.add("sections", &self.sections.values().collect::<Vec<&Section>>());
  353. let mut categories = vec![];
  354. if self.config.generate_categories_pages.unwrap() && !self.categories.is_empty() {
  355. categories.push(self.config.make_permalink("categories"));
  356. for category in self.categories.keys() {
  357. categories.push(
  358. self.config.make_permalink(&format!("categories/{}", slugify(category)))
  359. );
  360. }
  361. }
  362. context.add("categories", &categories);
  363. let mut tags = vec![];
  364. if self.config.generate_tags_pages.unwrap() && !self.tags.is_empty() {
  365. tags.push(self.config.make_permalink("tags"));
  366. for tag in self.tags.keys() {
  367. tags.push(
  368. self.config.make_permalink(&format!("tags/{}", slugify(tag)))
  369. );
  370. }
  371. }
  372. context.add("tags", &tags);
  373. let sitemap = self.templates.render("sitemap.xml", &context)?;
  374. create_file(self.output_path.join("sitemap.xml"), &sitemap)?;
  375. Ok(())
  376. }
  377. fn render_rss_feed(&self) -> Result<()> {
  378. let mut context = Context::new();
  379. let mut pages = self.pages.values()
  380. .filter(|p| p.meta.date.is_some())
  381. .take(15) // limit to the last 15 elements
  382. .collect::<Vec<&Page>>();
  383. // Don't generate a RSS feed if none of the pages has a date
  384. if pages.is_empty() {
  385. return Ok(());
  386. }
  387. pages.sort_by(|a, b| a.partial_cmp(b).unwrap());
  388. context.add("pages", &pages);
  389. context.add("last_build_date", &pages[0].meta.date);
  390. context.add("config", &self.config);
  391. let rss_feed_url = if self.config.base_url.ends_with('/') {
  392. format!("{}{}", self.config.base_url, "feed.xml")
  393. } else {
  394. format!("{}/{}", self.config.base_url, "feed.xml")
  395. };
  396. context.add("feed_url", &rss_feed_url);
  397. let sitemap = self.templates.render("rss.xml", &context)?;
  398. create_file(self.output_path.join("rss.xml"), &sitemap)?;
  399. Ok(())
  400. }
  401. fn render_sections(&self) -> Result<()> {
  402. let public = self.output_path.clone();
  403. for section in self.sections.values() {
  404. let mut output_path = public.to_path_buf();
  405. for component in &section.components {
  406. output_path.push(component);
  407. if !output_path.exists() {
  408. create_directory(&output_path)?;
  409. }
  410. }
  411. let output = section.render_html(&self.templates, &self.config)?;
  412. create_file(output_path.join("index.html"), &self.inject_livereload(output))?;
  413. }
  414. Ok(())
  415. }
  416. }