theme.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use pulldown_cmark::{html, Parser};
  2. pub struct Theme {
  3. root_path: String,
  4. parser_options: pulldown_cmark::Options,
  5. }
  6. impl Theme {
  7. pub fn new(root_path: String) -> Self {
  8. let mut parser_options = pulldown_cmark::Options::empty();
  9. parser_options.insert(pulldown_cmark::Options::ENABLE_STRIKETHROUGH);
  10. parser_options.insert(pulldown_cmark::Options::ENABLE_TABLES);
  11. parser_options.insert(pulldown_cmark::Options::ENABLE_FOOTNOTES);
  12. Theme {
  13. root_path,
  14. parser_options,
  15. }
  16. }
  17. // Renders markdown as html
  18. pub fn markdown_as_html(&self, path: &String) -> Result<String, String> {
  19. log::trace!("Rendering file '{}'", path);
  20. let path = format!("{}.md", path);
  21. let file = std::fs::read_to_string(&path);
  22. match file {
  23. Ok(content) => {
  24. let parser = Parser::new_ext(content.as_str(), self.parser_options);
  25. let mut html_output = String::new();
  26. html::push_html(&mut html_output, parser);
  27. Ok(html_output)
  28. }
  29. Err(error) => {
  30. log::error!("File located at path {} was not found.", path);
  31. Err(error.to_string())
  32. }
  33. }
  34. }
  35. }