Add year, month and day to page context with a date

This commit is contained in:
Vincent Prouillet 2018-05-30 14:08:35 +02:00
parent 4a6244adcf
commit 8e8cdfeb7f
6 changed files with 20 additions and 4 deletions

View file

@ -10,6 +10,7 @@
- Co-located assets are now permalinks - Co-located assets are now permalinks
- Words are now counted using unicode rather than whitespaces - Words are now counted using unicode rather than whitespaces
- Aliases can now be pointing directly to specific HTML files - Aliases can now be pointing directly to specific HTML files
- Add `year`, `month` and `day` variables to pages with a date
## 0.3.4 (2018-06-22) ## 0.3.4 (2018-06-22)

1
Cargo.lock generated
View file

@ -210,6 +210,7 @@ dependencies = [
name = "content" name = "content"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"config 0.1.0", "config 0.1.0",
"errors 0.1.0", "errors 0.1.0",
"front_matter 0.1.0", "front_matter 0.1.0",

View file

@ -8,6 +8,7 @@ tera = "0.11"
serde = "1" serde = "1"
slug = "0.1" slug = "0.1"
rayon = "1" rayon = "1"
chrono = "0.4"
errors = { path = "../errors" } errors = { path = "../errors" }
config = { path = "../config" } config = { path = "../config" }

View file

@ -2,6 +2,7 @@ extern crate tera;
extern crate slug; extern crate slug;
extern crate serde; extern crate serde;
extern crate rayon; extern crate rayon;
extern crate chrono;
extern crate errors; extern crate errors;
extern crate config; extern crate config;

View file

@ -3,7 +3,7 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::result::Result as StdResult; use std::result::Result as StdResult;
use chrono::Datelike;
use tera::{Tera, Context as TeraContext}; use tera::{Tera, Context as TeraContext};
use serde::ser::{SerializeStruct, self}; use serde::ser::{SerializeStruct, self};
use slug::slugify; use slug::slugify;
@ -247,11 +247,21 @@ impl Default for Page {
impl ser::Serialize for Page { impl ser::Serialize for Page {
fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> where S: ser::Serializer { fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> where S: ser::Serializer {
let mut state = serializer.serialize_struct("page", 18)?; let mut state = serializer.serialize_struct("page", 21)?;
state.serialize_field("content", &self.content)?; state.serialize_field("content", &self.content)?;
state.serialize_field("title", &self.meta.title)?; state.serialize_field("title", &self.meta.title)?;
state.serialize_field("description", &self.meta.description)?; state.serialize_field("description", &self.meta.description)?;
state.serialize_field("date", &self.meta.date)?; state.serialize_field("date", &self.meta.date)?;
if let Some(chrono_datetime) = self.meta.date() {
let d = chrono_datetime.date();
state.serialize_field("year", &d.year())?;
state.serialize_field("month", &d.month())?;
state.serialize_field("day", &d.day())?;
} else {
state.serialize_field::<Option<usize>>("year", &None)?;
state.serialize_field::<Option<usize>>("month", &None)?;
state.serialize_field::<Option<usize>>("day", &None)?;
}
state.serialize_field("slug", &self.slug)?; state.serialize_field("slug", &self.slug)?;
state.serialize_field("path", &self.path)?; state.serialize_field("path", &self.path)?;
state.serialize_field("components", &self.components)?; state.serialize_field("components", &self.components)?;

View file

@ -39,8 +39,10 @@ next: Page?;
toc: Array<Header>; toc: Array<Header>;
// Paths of colocated assets, relative to the content directory // Paths of colocated assets, relative to the content directory
assets: Array<String>; assets: Array<String>;
// Paths of colocated image assets, ie. files with an extension of "jpg", "jpeg", "png", "gif", or "bmp" // Year/month/day is only set if the page has a date and month/day are 1-indexed
images: Array<String>; year: Number?;
month: Number?;
day: Number?;
``` ```
## Section variables ## Section variables