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.

524 lines
18KB

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