Add page and section components

This commit is contained in:
Vincent Prouillet 2017-10-31 16:41:31 +01:00
parent 744e0f8df1
commit 791fbe685b
4 changed files with 29 additions and 4 deletions

View file

@ -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)

View file

@ -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<String>,
/// 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::<Vec<_>>();
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<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> 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"));
}

View file

@ -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<String>,
/// 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::<Vec<_>>();
section.permalink = config.make_permalink(&section.path);
Ok(section)
}
@ -140,13 +147,14 @@ impl Section {
impl ser::Serialize for Section {
fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> 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(),

View file

@ -20,6 +20,8 @@ description: String?;
date: String?;
slug: String;
path: String;
// the path, split on '/'
components: Array<String>;
permalink: String;
summary: String?;
tags: Array<String>;
@ -51,6 +53,8 @@ description: String?;
date: String?;
slug: String;
path: String;
// the path, split on '/'
components: Array<String>;
permalink: String;
extra: HashMap<String, Any>;
// Pages directly in this section, sorted if asked