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.

51 lines
1.6KB

  1. use std::fs::{create_dir, remove_dir_all};
  2. use std::path::Path;
  3. use glob::glob;
  4. use tera::Tera;
  5. use config:: Config;
  6. use errors::{Result, ResultExt};
  7. use page::Page;
  8. use utils::create_file;
  9. pub fn build(config: Config) -> Result<()> {
  10. if Path::new("public").exists() {
  11. // Delete current `public` directory so we can start fresh
  12. remove_dir_all("public").chain_err(|| "Couldn't delete `public` directory")?;
  13. }
  14. let tera = Tera::new("layouts/**/*").chain_err(|| "Error parsing templates")?;
  15. // let mut pages: Vec<Page> = vec![];
  16. // ok we got all the pages HTML, time to write them down to disk
  17. create_dir("public")?;
  18. let public = Path::new("public");
  19. // hardcoded pattern so can't error
  20. for entry in glob("content/**/*.md").unwrap().filter_map(|e| e.ok()) {
  21. let path = entry.as_path();
  22. let mut page = Page::from_file(&path)?;
  23. let mut current_path = public.clone().to_path_buf();
  24. for section in &page.sections {
  25. current_path.push(section);
  26. //current_path = current_path.join(section).as_path();
  27. if !current_path.exists() {
  28. println!("Creating {:?} folder", current_path);
  29. create_dir(&current_path)?;
  30. // TODO: create section index.html
  31. // create_file(current_path.join("index.html"), "");
  32. }
  33. }
  34. current_path.push(&page.filename);
  35. create_dir(&current_path)?;
  36. create_file(current_path.join("index.html"), &page.render_html(&tera, &config)?)?;
  37. }
  38. Ok(())
  39. }