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.

108 lines
3.3KB

  1. use std::collections::HashMap;
  2. use std::fs::{create_dir, remove_dir_all};
  3. use std::path::Path;
  4. use glob::glob;
  5. use tera::{Tera, Context};
  6. use errors::{Result, ResultExt};
  7. use config::{Config, get_config};
  8. use page::Page;
  9. use utils::create_file;
  10. #[derive(Debug)]
  11. pub struct Site {
  12. config: Config,
  13. pages: HashMap<String, Page>,
  14. sections: HashMap<String, Vec<String>>,
  15. templates: Tera,
  16. }
  17. impl Site {
  18. pub fn new() -> Result<Site> {
  19. let tera = Tera::new("templates/**/*").chain_err(|| "Error parsing templates")?;
  20. let mut site = Site {
  21. config: get_config(),
  22. pages: HashMap::new(),
  23. sections: HashMap::new(),
  24. templates: tera,
  25. };
  26. site.parse_site()?;
  27. Ok(site)
  28. }
  29. /// Reads all .md files in the `content` directory and create pages
  30. /// out of them
  31. fn parse_site(&mut self) -> Result<()> {
  32. // First step: do all the articles and group article by sections
  33. // hardcoded pattern so can't error
  34. for entry in glob("content/**/*.md").unwrap().filter_map(|e| e.ok()) {
  35. let page = Page::from_file(&entry.as_path())?;
  36. for section in &page.sections {
  37. self.sections.entry(section.clone()).or_insert(vec![]).push(page.get_slug());
  38. }
  39. self.pages.insert(page.get_slug(), page);
  40. }
  41. Ok(())
  42. }
  43. /// Builds the site to the `public` directory after deleting it
  44. pub fn build(&self) -> Result<()> {
  45. if Path::new("public").exists() {
  46. // Delete current `public` directory so we can start fresh
  47. remove_dir_all("public").chain_err(|| "Couldn't delete `public` directory")?;
  48. }
  49. // Start from scratch
  50. create_dir("public")?;
  51. let public = Path::new("public");
  52. let mut pages = vec![];
  53. // First we render the pages themselves
  54. for page in self.pages.values() {
  55. // Copy the nesting of the content directory if we have sections for that page
  56. let mut current_path = public.to_path_buf();
  57. // This loop happens when the page doesn't have a set URL
  58. for section in &page.sections {
  59. current_path.push(section);
  60. if !current_path.exists() {
  61. create_dir(&current_path)?;
  62. }
  63. }
  64. // if we have a url already set, use that as base
  65. if let Some(ref url) = page.meta.url {
  66. current_path.push(url);
  67. }
  68. // Make sure the folder exists
  69. create_dir(&current_path)?;
  70. // Finally, create a index.html file there with the page rendered
  71. let output = page.render_html(&self.templates, &self.config)?;
  72. create_file(current_path.join("index.html"), &output)?;
  73. pages.push(page);
  74. }
  75. // Then the section pages
  76. // The folders have already been created in the page loop so no need to `create_dir` here
  77. // for (section, slugs) in &self.sections {
  78. // // TODO
  79. // }
  80. // And finally the index page
  81. let mut context = Context::new();
  82. context.add("pages", &pages);
  83. context.add("config", &self.config);
  84. create_file(public.join("index.html"), &self.templates.render("index.html", &context)?)?;
  85. Ok(())
  86. }
  87. }