From 4e3d231ca9a482f53cee9fb3e7e17c0d70afa04e Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Wed, 24 Oct 2018 11:40:57 +0200 Subject: [PATCH] Small refactor for serialized page/sections --- components/library/src/content/mod.rs | 6 +- components/library/src/content/page.rs | 127 +------------ components/library/src/content/section.rs | 89 +-------- components/library/src/content/ser.rs | 213 ++++++++++++++++++++++ 4 files changed, 224 insertions(+), 211 deletions(-) create mode 100644 components/library/src/content/ser.rs diff --git a/components/library/src/content/mod.rs b/components/library/src/content/mod.rs index 8fd04736..09ac3860 100644 --- a/components/library/src/content/mod.rs +++ b/components/library/src/content/mod.rs @@ -1,7 +1,9 @@ mod file_info; mod page; mod section; +mod ser; pub use self::file_info::FileInfo; -pub use self::page::{Page, SerializingPage}; -pub use self::section::{Section, SerializingSection}; +pub use self::page::Page; +pub use self::section::Section; +pub use self::ser::{SerializingPage, SerializingSection}; diff --git a/components/library/src/content/page.rs b/components/library/src/content/page.rs index 403a43e4..05f2920b 100644 --- a/components/library/src/content/page.rs +++ b/components/library/src/content/page.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; -use tera::{Tera, Context as TeraContext, Value, Map}; +use tera::{Tera, Context as TeraContext}; use slug::slugify; use slotmap::{Key}; @@ -16,129 +16,8 @@ use rendering::{RenderContext, Header, render_content}; use library::Library; use content::file_info::FileInfo; +use content::ser::SerializingPage; -/// What we are sending to the templates when rendering them -#[derive(Clone, Debug, PartialEq, Serialize)] -pub struct SerializingPage<'a> { - relative_path: &'a str, - content: &'a str, - permalink: &'a str, - slug: &'a str, - ancestors: Vec, - title: &'a Option, - description: &'a Option, - date: &'a Option, - year: Option, - month: Option, - day: Option, - taxonomies: &'a HashMap>, - extra: &'a Map, - path: &'a str, - components: &'a [String], - summary: &'a Option, - word_count: Option, - reading_time: Option, - toc: &'a [Header], - assets: Vec, - draft: bool, - lighter: Option>>, - heavier: Option>>, - earlier: Option>>, - later: Option>>, -} - -impl<'a> SerializingPage<'a> { - /// Grabs all the data from a page, including sibling pages - pub fn from_page(page: &'a Page, library: &'a Library) -> Self { - let mut year = None; - let mut month = None; - let mut day = None; - if let Some(d) = page.meta.datetime_tuple { - year = Some(d.0); - month = Some(d.1); - day = Some(d.2); - } - let pages = library.pages(); - let lighter = page.lighter.map(|k| Box::new(Self::from_page_basic(pages.get(k).unwrap(), Some(library)))); - let heavier = page.heavier.map(|k| Box::new(Self::from_page_basic(pages.get(k).unwrap(), Some(library)))); - let earlier = page.earlier.map(|k| Box::new(Self::from_page_basic(pages.get(k).unwrap(), Some(library)))); - let later = page.later.map(|k| Box::new(Self::from_page_basic(pages.get(k).unwrap(), Some(library)))); - let ancestors = page.ancestors.iter().map(|k| library.get_section_by_key(*k).file.relative.clone()).collect(); - - SerializingPage { - relative_path: &page.file.relative, - ancestors, - content: &page.content, - permalink: &page.permalink, - slug: &page.slug, - title: &page.meta.title, - description: &page.meta.description, - extra: &page.meta.extra, - date: &page.meta.date, - year, - month, - day, - taxonomies: &page.meta.taxonomies, - path: &page.path, - components: &page.components, - summary: &page.summary, - word_count: page.word_count, - reading_time: page.reading_time, - toc: &page.toc, - assets: page.serialize_assets(), - draft: page.is_draft(), - lighter, - heavier, - earlier, - later, - } - } - - /// Same as from_page but does not fill sibling pages - pub fn from_page_basic(page: &'a Page, library: Option<&'a Library>) -> Self { - let mut year = None; - let mut month = None; - let mut day = None; - if let Some(d) = page.meta.datetime_tuple { - year = Some(d.0); - month = Some(d.1); - day = Some(d.2); - } - let ancestors = if let Some(ref lib) = library { - page.ancestors.iter().map(|k| lib.get_section_by_key(*k).file.relative.clone()).collect() - } else { - vec![] - }; - - SerializingPage { - relative_path: &page.file.relative, - ancestors, - content: &page.content, - permalink: &page.permalink, - slug: &page.slug, - title: &page.meta.title, - description: &page.meta.description, - extra: &page.meta.extra, - date: &page.meta.date, - year, - month, - day, - taxonomies: &page.meta.taxonomies, - path: &page.path, - components: &page.components, - summary: &page.summary, - word_count: page.word_count, - reading_time: page.reading_time, - toc: &page.toc, - assets: page.serialize_assets(), - draft: page.is_draft(), - lighter: None, - heavier: None, - earlier: None, - later: None, - } - } -} #[derive(Clone, Debug, PartialEq)] pub struct Page { @@ -343,7 +222,7 @@ impl Page { } /// Creates a vectors of asset URLs. - fn serialize_assets(&self) -> Vec { + pub fn serialize_assets(&self) -> Vec { self.assets.iter() .filter_map(|asset| asset.file_name()) .filter_map(|filename| filename.to_str()) diff --git a/components/library/src/content/section.rs b/components/library/src/content/section.rs index efd788d8..4baea628 100644 --- a/components/library/src/content/section.rs +++ b/components/library/src/content/section.rs @@ -1,8 +1,8 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; -use tera::{Tera, Context as TeraContext, Value}; -use slotmap::{Key}; +use tera::{Tera, Context as TeraContext}; +use slotmap::Key; use config::Config; use front_matter::{SectionFrontMatter, split_section_content}; @@ -13,91 +13,10 @@ use utils::site::get_reading_analytics; use rendering::{RenderContext, Header, render_content}; use content::file_info::FileInfo; -use content::SerializingPage; +use content::ser::SerializingSection; use library::Library; -#[derive(Clone, Debug, PartialEq, Serialize)] -pub struct SerializingSection<'a> { - relative_path: &'a str, - content: &'a str, - permalink: &'a str, - ancestors: Vec, - title: &'a Option, - description: &'a Option, - extra: &'a HashMap, - path: &'a str, - components: &'a [String], - word_count: Option, - reading_time: Option, - toc: &'a [Header], - assets: Vec, - pages: Vec>, - subsections: Vec<&'a str>, -} - -impl<'a> SerializingSection<'a> { - pub fn from_section(section: &'a Section, library: &'a Library) -> Self { - let mut pages = Vec::with_capacity(section.pages.len()); - let mut subsections = Vec::with_capacity(section.subsections.len()); - - for k in §ion.pages { - pages.push(library.get_page_by_key(*k).to_serialized(library)); - } - - for k in §ion.subsections { - subsections.push(library.get_section_path_by_key(*k)); - } - - let ancestors = section.ancestors.iter().map(|k| library.get_section_by_key(*k).file.relative.clone()).collect(); - - SerializingSection { - relative_path: §ion.file.relative, - ancestors, - content: §ion.content, - permalink: §ion.permalink, - title: §ion.meta.title, - description: §ion.meta.description, - extra: §ion.meta.extra, - path: §ion.path, - components: §ion.components, - word_count: section.word_count, - reading_time: section.reading_time, - toc: §ion.toc, - assets: section.serialize_assets(), - pages, - subsections, - } - } - - /// Same as from_section but doesn't fetch pages and sections - pub fn from_section_basic(section: &'a Section, library: Option<&'a Library>) -> Self { - let ancestors = if let Some(ref lib) = library { - section.ancestors.iter().map(|k| lib.get_section_by_key(*k).file.relative.clone()).collect() - } else { - vec![] - }; - - SerializingSection { - relative_path: §ion.file.relative, - ancestors, - content: §ion.content, - permalink: §ion.permalink, - title: §ion.meta.title, - description: §ion.meta.description, - extra: §ion.meta.extra, - path: §ion.path, - components: §ion.components, - word_count: section.word_count, - reading_time: section.reading_time, - toc: §ion.toc, - assets: section.serialize_assets(), - pages: vec![], - subsections: vec![], - } - } -} - #[derive(Clone, Debug, PartialEq)] pub struct Section { /// All info about the actual file @@ -260,7 +179,7 @@ impl Section { } /// Creates a vectors of asset URLs. - fn serialize_assets(&self) -> Vec { + pub fn serialize_assets(&self) -> Vec { self.assets.iter() .filter_map(|asset| asset.file_name()) .filter_map(|filename| filename.to_str()) diff --git a/components/library/src/content/ser.rs b/components/library/src/content/ser.rs new file mode 100644 index 00000000..c45e30dd --- /dev/null +++ b/components/library/src/content/ser.rs @@ -0,0 +1,213 @@ +//! What we are sending to the templates when rendering them +use std::collections::HashMap; + +use tera::{Value, Map}; + +use library::Library; +use content::{Page, Section}; +use rendering::Header; + + +#[derive(Clone, Debug, PartialEq, Serialize)] +pub struct SerializingPage<'a> { + relative_path: &'a str, + content: &'a str, + permalink: &'a str, + slug: &'a str, + ancestors: Vec, + title: &'a Option, + description: &'a Option, + date: &'a Option, + year: Option, + month: Option, + day: Option, + taxonomies: &'a HashMap>, + extra: &'a Map, + path: &'a str, + components: &'a [String], + summary: &'a Option, + word_count: Option, + reading_time: Option, + toc: &'a [Header], + assets: Vec, + draft: bool, + lighter: Option>>, + heavier: Option>>, + earlier: Option>>, + later: Option>>, +} + +impl<'a> SerializingPage<'a> { + /// Grabs all the data from a page, including sibling pages + pub fn from_page(page: &'a Page, library: &'a Library) -> Self { + let mut year = None; + let mut month = None; + let mut day = None; + if let Some(d) = page.meta.datetime_tuple { + year = Some(d.0); + month = Some(d.1); + day = Some(d.2); + } + let pages = library.pages(); + let lighter = page.lighter.map(|k| Box::new(Self::from_page_basic(pages.get(k).unwrap(), Some(library)))); + let heavier = page.heavier.map(|k| Box::new(Self::from_page_basic(pages.get(k).unwrap(), Some(library)))); + let earlier = page.earlier.map(|k| Box::new(Self::from_page_basic(pages.get(k).unwrap(), Some(library)))); + let later = page.later.map(|k| Box::new(Self::from_page_basic(pages.get(k).unwrap(), Some(library)))); + let ancestors = page.ancestors.iter().map(|k| library.get_section_by_key(*k).file.relative.clone()).collect(); + + SerializingPage { + relative_path: &page.file.relative, + ancestors, + content: &page.content, + permalink: &page.permalink, + slug: &page.slug, + title: &page.meta.title, + description: &page.meta.description, + extra: &page.meta.extra, + date: &page.meta.date, + year, + month, + day, + taxonomies: &page.meta.taxonomies, + path: &page.path, + components: &page.components, + summary: &page.summary, + word_count: page.word_count, + reading_time: page.reading_time, + toc: &page.toc, + assets: page.serialize_assets(), + draft: page.is_draft(), + lighter, + heavier, + earlier, + later, + } + } + + /// Same as from_page but does not fill sibling pages + pub fn from_page_basic(page: &'a Page, library: Option<&'a Library>) -> Self { + let mut year = None; + let mut month = None; + let mut day = None; + if let Some(d) = page.meta.datetime_tuple { + year = Some(d.0); + month = Some(d.1); + day = Some(d.2); + } + let ancestors = if let Some(ref lib) = library { + page.ancestors.iter().map(|k| lib.get_section_by_key(*k).file.relative.clone()).collect() + } else { + vec![] + }; + + SerializingPage { + relative_path: &page.file.relative, + ancestors, + content: &page.content, + permalink: &page.permalink, + slug: &page.slug, + title: &page.meta.title, + description: &page.meta.description, + extra: &page.meta.extra, + date: &page.meta.date, + year, + month, + day, + taxonomies: &page.meta.taxonomies, + path: &page.path, + components: &page.components, + summary: &page.summary, + word_count: page.word_count, + reading_time: page.reading_time, + toc: &page.toc, + assets: page.serialize_assets(), + draft: page.is_draft(), + lighter: None, + heavier: None, + earlier: None, + later: None, + } + } +} + + +#[derive(Clone, Debug, PartialEq, Serialize)] +pub struct SerializingSection<'a> { + relative_path: &'a str, + content: &'a str, + permalink: &'a str, + ancestors: Vec, + title: &'a Option, + description: &'a Option, + extra: &'a HashMap, + path: &'a str, + components: &'a [String], + word_count: Option, + reading_time: Option, + toc: &'a [Header], + assets: Vec, + pages: Vec>, + subsections: Vec<&'a str>, +} + +impl<'a> SerializingSection<'a> { + pub fn from_section(section: &'a Section, library: &'a Library) -> Self { + let mut pages = Vec::with_capacity(section.pages.len()); + let mut subsections = Vec::with_capacity(section.subsections.len()); + + for k in §ion.pages { + pages.push(library.get_page_by_key(*k).to_serialized(library)); + } + + for k in §ion.subsections { + subsections.push(library.get_section_path_by_key(*k)); + } + + let ancestors = section.ancestors.iter().map(|k| library.get_section_by_key(*k).file.relative.clone()).collect(); + + SerializingSection { + relative_path: §ion.file.relative, + ancestors, + content: §ion.content, + permalink: §ion.permalink, + title: §ion.meta.title, + description: §ion.meta.description, + extra: §ion.meta.extra, + path: §ion.path, + components: §ion.components, + word_count: section.word_count, + reading_time: section.reading_time, + toc: §ion.toc, + assets: section.serialize_assets(), + pages, + subsections, + } + } + + /// Same as from_section but doesn't fetch pages and sections + pub fn from_section_basic(section: &'a Section, library: Option<&'a Library>) -> Self { + let ancestors = if let Some(ref lib) = library { + section.ancestors.iter().map(|k| lib.get_section_by_key(*k).file.relative.clone()).collect() + } else { + vec![] + }; + + SerializingSection { + relative_path: §ion.file.relative, + ancestors, + content: §ion.content, + permalink: §ion.permalink, + title: §ion.meta.title, + description: §ion.meta.description, + extra: §ion.meta.extra, + path: §ion.path, + components: §ion.components, + word_count: section.word_count, + reading_time: section.reading_time, + toc: §ion.toc, + assets: section.serialize_assets(), + pages: vec![], + subsections: vec![], + } + } +}