From c565f10cb33f9394e65fe61febd411e2cdf91d86 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Fri, 12 May 2017 18:05:00 +0900 Subject: [PATCH] Let sections have their own body --- src/section.rs | 22 ++++++++++++++++++++-- src/site.rs | 12 ++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/section.rs b/src/section.rs index b6650e70..cab8a90c 100644 --- a/src/section.rs +++ b/src/section.rs @@ -9,6 +9,7 @@ use config::Config; use front_matter::{FrontMatter, split_content}; use errors::{Result, ResultExt}; use utils::{read_file, find_content_components}; +use markdown::markdown_to_html; use page::{Page}; @@ -26,6 +27,10 @@ pub struct Section { pub path: String, /// The full URL for that page pub permalink: String, + /// The actual content of the page, in markdown + pub raw_content: String, + /// The HTML rendered of the page + pub content: String, /// The front matter meta-data pub meta: FrontMatter, /// All direct pages of that section @@ -47,6 +52,8 @@ impl Section { components: vec![], path: "".to_string(), permalink: "".to_string(), + raw_content: "".to_string(), + content: "".to_string(), meta: meta, pages: vec![], ignored_pages: vec![], @@ -55,8 +62,9 @@ impl Section { } pub fn parse(file_path: &Path, content: &str, config: &Config) -> Result
{ - let (meta, _) = split_content(file_path, content)?; + let (meta, content) = split_content(file_path, content)?; let mut section = Section::new(file_path, meta); + section.raw_content = content.clone(); section.components = find_content_components(§ion.file_path); section.path = section.components.join("/"); section.permalink = config.make_permalink(§ion.path); @@ -89,6 +97,13 @@ impl Section { } } + /// We need access to all pages url to render links relative to content + /// so that can't happen at the same time as parsing + pub fn render_markdown(&mut self, permalinks: &HashMap, tera: &Tera, config: &Config) -> Result<()> { + self.content = markdown_to_html(&self.raw_content, permalinks, tera, config)?; + Ok(()) + } + /// Renders the page using the default layout, unless specified in front-matter pub fn render_html(&self, sections: &HashMap, tera: &Tera, config: &Config) -> Result { let tpl_name = self.get_template_name(); @@ -121,7 +136,8 @@ impl Section { impl ser::Serialize for Section { fn serialize(&self, serializer: S) -> StdResult where S: ser::Serializer { - let mut state = serializer.serialize_struct("section", 6)?; + let mut state = serializer.serialize_struct("section", 7)?; + state.serialize_field("content", &self.content)?; state.serialize_field("title", &self.meta.title)?; state.serialize_field("description", &self.meta.description)?; state.serialize_field("path", &format!("/{}", self.path))?; @@ -142,6 +158,8 @@ impl Default for Section { components: vec![], path: "".to_string(), permalink: "".to_string(), + raw_content: "".to_string(), + content: "".to_string(), meta: FrontMatter::default(), pages: vec![], ignored_pages: vec![], diff --git a/src/site.rs b/src/site.rs index 67bd478f..35eea9ac 100644 --- a/src/site.rs +++ b/src/site.rs @@ -194,6 +194,10 @@ impl Site { page.render_markdown(&permalinks, &self.tera, &self.config)?; } + for section in self.sections.values_mut() { + section.render_markdown(&permalinks, &self.tera, &self.config)?; + } + self.permalinks = permalinks; self.populate_sections(); self.populate_tags_and_categories(); @@ -219,14 +223,18 @@ impl Site { /// Called in serve, add a page again updating permalinks and its content /// The bool in the result is whether the front matter has been updated or not + /// TODO: the above is very confusing, change that fn add_page_and_render(&mut self, path: &Path) -> Result<(bool, Page)> { - let existing_page = self.pages.get(path).expect("Page was supposed to exist in add_page_and_render").clone(); + let existing_page = self.pages.get(path).cloned(); self.add_page(path)?; let mut page = self.pages.get_mut(path).unwrap(); self.permalinks.insert(page.relative_path.clone(), page.permalink.clone()); page.render_markdown(&self.permalinks, &self.tera, &self.config)?; - Ok((existing_page.meta != page.meta, page.clone())) + if let Some(prev_page) = existing_page { + return Ok((prev_page.meta != page.meta, page.clone())); + } + Ok((true, page.clone())) } /// Find out the direct subsections of each subsection if there are some