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.

101 lines
3.5KB

  1. use std::path::{Path, PathBuf};
  2. use std::result::Result as StdResult;
  3. use tera::{Tera, Context};
  4. use serde::ser::{SerializeStruct, self};
  5. use config::Config;
  6. use front_matter::{FrontMatter, split_content};
  7. use errors::{Result, ResultExt};
  8. use utils::{read_file, find_content_components};
  9. use page::Page;
  10. #[derive(Clone, Debug, PartialEq)]
  11. pub struct Section {
  12. /// The _index.md full path
  13. pub file_path: PathBuf,
  14. /// The .md path, starting from the content directory, with / slashes
  15. pub relative_path: String,
  16. /// Path of the directory containing the _index.md file
  17. pub parent_path: PathBuf,
  18. /// The folder names from `content` to this section file
  19. pub components: Vec<String>,
  20. /// The relative URL of the page
  21. pub url: String,
  22. /// The full URL for that page
  23. pub permalink: String,
  24. /// The front matter meta-data
  25. pub meta: FrontMatter,
  26. /// All direct pages of that section
  27. pub pages: Vec<Page>,
  28. /// All direct subsections
  29. pub subsections: Vec<Section>,
  30. }
  31. impl Section {
  32. pub fn new(file_path: &Path, meta: FrontMatter) -> Section {
  33. Section {
  34. file_path: file_path.to_path_buf(),
  35. relative_path: "".to_string(),
  36. parent_path: file_path.parent().unwrap().to_path_buf(),
  37. components: vec![],
  38. url: "".to_string(),
  39. permalink: "".to_string(),
  40. meta: meta,
  41. pages: vec![],
  42. subsections: vec![],
  43. }
  44. }
  45. pub fn parse(file_path: &Path, content: &str, config: &Config) -> Result<Section> {
  46. let (meta, _) = split_content(file_path, content)?;
  47. let mut section = Section::new(file_path, meta);
  48. section.components = find_content_components(&section.file_path);
  49. section.url = section.components.join("/");
  50. section.permalink = config.make_permalink(&section.url);
  51. section.relative_path = format!("{}/_index.md", section.components.join("/"));
  52. Ok(section)
  53. }
  54. /// Read and parse a .md file into a Page struct
  55. pub fn from_file<P: AsRef<Path>>(path: P, config: &Config) -> Result<Section> {
  56. let path = path.as_ref();
  57. let content = read_file(path)?;
  58. Section::parse(path, &content, config)
  59. }
  60. /// Renders the page using the default layout, unless specified in front-matter
  61. pub fn render_html(&self, tera: &Tera, config: &Config) -> Result<String> {
  62. let tpl_name = match self.meta.template {
  63. Some(ref l) => l.to_string(),
  64. None => "section.html".to_string()
  65. };
  66. // TODO: create a helper to create context to ensure all contexts
  67. // have the same names
  68. let mut context = Context::new();
  69. context.add("config", config);
  70. context.add("section", self);
  71. tera.render(&tpl_name, &context)
  72. .chain_err(|| format!("Failed to render section '{}'", self.file_path.display()))
  73. }
  74. }
  75. impl ser::Serialize for Section {
  76. fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> where S: ser::Serializer {
  77. let mut state = serializer.serialize_struct("section", 6)?;
  78. state.serialize_field("title", &self.meta.title)?;
  79. state.serialize_field("description", &self.meta.description)?;
  80. state.serialize_field("url", &format!("/{}", self.url))?;
  81. state.serialize_field("permalink", &self.permalink)?;
  82. state.serialize_field("pages", &self.pages)?;
  83. state.serialize_field("subsections", &self.subsections)?;
  84. state.end()
  85. }
  86. }