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.

129 lines
3.9KB

  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. live_reload: bool,
  17. }
  18. impl Site {
  19. pub fn new(livereload: bool) -> Result<Site> {
  20. let tera = Tera::new("templates/**/*").chain_err(|| "Error parsing templates")?;
  21. let mut site = Site {
  22. config: get_config(),
  23. pages: HashMap::new(),
  24. sections: HashMap::new(),
  25. templates: tera,
  26. live_reload: livereload,
  27. };
  28. site.parse_site()?;
  29. Ok(site)
  30. }
  31. /// Reads all .md files in the `content` directory and create pages
  32. /// out of them
  33. fn parse_site(&mut self) -> Result<()> {
  34. // First step: do all the articles and group article by sections
  35. // hardcoded pattern so can't error
  36. for entry in glob("content/**/*.md").unwrap().filter_map(|e| e.ok()) {
  37. let page = Page::from_file(&entry.as_path())?;
  38. for section in &page.sections {
  39. self.sections.entry(section.clone()).or_insert(vec![]).push(page.get_slug());
  40. }
  41. self.pages.insert(page.get_slug(), page);
  42. }
  43. Ok(())
  44. }
  45. // Inject live reload script tag if in live reload mode
  46. fn inject_livereload(&self, html: String) -> String {
  47. if self.live_reload {
  48. return html.replace(
  49. "</body>",
  50. r#"<script src="/livereload.js?port=1112&mindelay=10"></script></body>"#
  51. );
  52. }
  53. html
  54. }
  55. pub fn rebuild(&mut self) -> Result<()> {
  56. self.parse_site()?;
  57. self.build()
  58. }
  59. /// Builds the site to the `public` directory after deleting it
  60. pub fn build(&self) -> Result<()> {
  61. if Path::new("public").exists() {
  62. // Delete current `public` directory so we can start fresh
  63. remove_dir_all("public").chain_err(|| "Couldn't delete `public` directory")?;
  64. }
  65. // Start from scratch
  66. create_dir("public")?;
  67. let public = Path::new("public");
  68. let mut pages = vec![];
  69. // First we render the pages themselves
  70. for page in self.pages.values() {
  71. // Copy the nesting of the content directory if we have sections for that page
  72. let mut current_path = public.to_path_buf();
  73. // This loop happens when the page doesn't have a set URL
  74. for section in &page.sections {
  75. current_path.push(section);
  76. if !current_path.exists() {
  77. create_dir(&current_path)?;
  78. }
  79. }
  80. // if we have a url already set, use that as base
  81. if let Some(ref url) = page.meta.url {
  82. current_path.push(url);
  83. }
  84. // Make sure the folder exists
  85. create_dir(&current_path)?;
  86. // Finally, create a index.html file there with the page rendered
  87. let output = page.render_html(&self.templates, &self.config)?;
  88. create_file(current_path.join("index.html"), &self.inject_livereload(output))?;
  89. pages.push(page);
  90. }
  91. // Then the section pages
  92. // The folders have already been created in the page loop so no need to `create_dir` here
  93. // for (section, slugs) in &self.sections {
  94. // // TODO
  95. // }
  96. // And finally the index page
  97. let mut context = Context::new();
  98. context.add("pages", &pages);
  99. context.add("config", &self.config);
  100. let index = self.templates.render("index.html", &context)?;
  101. create_file(public.join("index.html"), &self.inject_livereload(index))?;
  102. Ok(())
  103. }
  104. }