From 791fbe685bc435da7b68b1524d97088a8bd4bdb0 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 31 Oct 2017 16:41:31 +0100 Subject: [PATCH] Add page and section components --- CHANGELOG.md | 2 +- components/content/src/page.rs | 14 +++++++++++++- components/content/src/section.rs | 13 +++++++++++-- .../documentation/templates/pages-sections.md | 4 ++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 269716d3..59f92cc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ - Add Nim syntax highlighting - Allow static folder to be missing - Fix shortcodes args being only passed as strings - +- Add `page.components` and `section.components` that are equivalent to `path.split('/')` ## 0.2.1 (2017-10-17) diff --git a/components/content/src/page.rs b/components/content/src/page.rs index 4c12ccc2..5880817f 100644 --- a/components/content/src/page.rs +++ b/components/content/src/page.rs @@ -36,6 +36,8 @@ pub struct Page { pub slug: String, /// The URL path of the page pub path: String, + /// The components of the path of the page + pub components: Vec, /// The full URL for that page pub permalink: String, /// The summary for the article, defaults to None @@ -63,6 +65,7 @@ impl Page { content: "".to_string(), slug: "".to_string(), path: "".to_string(), + components: vec![], permalink: "".to_string(), summary: None, previous: None, @@ -112,6 +115,10 @@ impl Page { page.path = format!("{}/", page.path); } + page.components = page.path.split('/') + .map(|p| p.to_string()) + .filter(|p| !p.is_empty()) + .collect::>(); page.permalink = config.make_permalink(&page.path); Ok(page) @@ -184,6 +191,7 @@ impl Default for Page { content: "".to_string(), slug: "".to_string(), path: "".to_string(), + components: vec![], permalink: "".to_string(), summary: None, previous: None, @@ -195,13 +203,14 @@ impl Default for Page { impl ser::Serialize for Page { fn serialize(&self, serializer: S) -> StdResult where S: ser::Serializer { - let mut state = serializer.serialize_struct("page", 16)?; + let mut state = serializer.serialize_struct("page", 17)?; state.serialize_field("content", &self.content)?; state.serialize_field("title", &self.meta.title)?; state.serialize_field("description", &self.meta.description)?; state.serialize_field("date", &self.meta.date)?; state.serialize_field("slug", &self.slug)?; state.serialize_field("path", &self.path)?; + state.serialize_field("components", &self.components)?; state.serialize_field("permalink", &self.permalink)?; state.serialize_field("summary", &self.summary)?; state.serialize_field("tags", &self.meta.tags)?; @@ -265,6 +274,7 @@ Hello world"#; assert!(res.is_ok()); let page = res.unwrap(); assert_eq!(page.path, "posts/intro/hello-world/"); + assert_eq!(page.components, vec!["posts", "intro", "hello-world"]); assert_eq!(page.permalink, "http://hello.com/posts/intro/hello-world/"); } @@ -280,6 +290,7 @@ Hello world"#; assert!(res.is_ok()); let page = res.unwrap(); assert_eq!(page.path, "hello-world/"); + assert_eq!(page.components, vec!["hello-world"]); assert_eq!(page.permalink, config.make_permalink("hello-world")); } @@ -295,6 +306,7 @@ Hello world"#; assert!(res.is_ok()); let page = res.unwrap(); assert_eq!(page.path, "hello-world/"); + assert_eq!(page.components, vec!["hello-world"]); assert_eq!(page.permalink, config.make_permalink("hello-world")); } diff --git a/components/content/src/section.rs b/components/content/src/section.rs index 9de49492..df94d328 100644 --- a/components/content/src/section.rs +++ b/components/content/src/section.rs @@ -25,6 +25,8 @@ pub struct Section { pub meta: SectionFrontMatter, /// The URL path of the page pub path: String, + /// The components for the path of that page + pub components: Vec, /// The full URL for that page pub permalink: String, /// The actual content of the page, in markdown @@ -47,8 +49,9 @@ impl Section { Section { file: FileInfo::new_section(file_path), - meta: meta, + meta, path: "".to_string(), + components: vec![], permalink: "".to_string(), raw_content: "".to_string(), content: "".to_string(), @@ -64,6 +67,10 @@ impl Section { let mut section = Section::new(file_path, meta); section.raw_content = content.clone(); section.path = format!("{}/", section.file.components.join("/")); + section.components = section.path.split('/') + .map(|p| p.to_string()) + .filter(|p| !p.is_empty()) + .collect::>(); section.permalink = config.make_permalink(§ion.path); Ok(section) } @@ -140,13 +147,14 @@ impl Section { impl ser::Serialize for Section { fn serialize(&self, serializer: S) -> StdResult where S: ser::Serializer { - let mut state = serializer.serialize_struct("section", 12)?; + let mut state = serializer.serialize_struct("section", 13)?; state.serialize_field("content", &self.content)?; state.serialize_field("permalink", &self.permalink)?; state.serialize_field("title", &self.meta.title)?; state.serialize_field("description", &self.meta.description)?; state.serialize_field("extra", &self.meta.extra)?; state.serialize_field("path", &self.path)?; + state.serialize_field("components", &self.components)?; state.serialize_field("permalink", &self.permalink)?; state.serialize_field("pages", &self.pages)?; state.serialize_field("subsections", &self.subsections)?; @@ -165,6 +173,7 @@ impl Default for Section { file: FileInfo::default(), meta: SectionFrontMatter::default(), path: "".to_string(), + components: vec![], permalink: "".to_string(), raw_content: "".to_string(), content: "".to_string(), diff --git a/docs/content/documentation/templates/pages-sections.md b/docs/content/documentation/templates/pages-sections.md index a53b3b98..93da906a 100644 --- a/docs/content/documentation/templates/pages-sections.md +++ b/docs/content/documentation/templates/pages-sections.md @@ -20,6 +20,8 @@ description: String?; date: String?; slug: String; path: String; +// the path, split on '/' +components: Array; permalink: String; summary: String?; tags: Array; @@ -51,6 +53,8 @@ description: String?; date: String?; slug: String; path: String; +// the path, split on '/' +components: Array; permalink: String; extra: HashMap; // Pages directly in this section, sorted if asked