From 8e8cdfeb7f53ba3b195350791a027072844905a8 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Wed, 30 May 2018 14:08:35 +0200 Subject: [PATCH] Add year, month and day to page context with a date --- CHANGELOG.md | 1 + Cargo.lock | 1 + components/content/Cargo.toml | 1 + components/content/src/lib.rs | 1 + components/content/src/page.rs | 14 ++++++++++++-- .../documentation/templates/pages-sections.md | 6 ++++-- 6 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e9384b..fb50842 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Co-located assets are now permalinks - Words are now counted using unicode rather than whitespaces - 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) diff --git a/Cargo.lock b/Cargo.lock index ddc608e..4d495c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -210,6 +210,7 @@ dependencies = [ name = "content" version = "0.1.0" dependencies = [ + "chrono 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "config 0.1.0", "errors 0.1.0", "front_matter 0.1.0", diff --git a/components/content/Cargo.toml b/components/content/Cargo.toml index 2e7724e..e913873 100644 --- a/components/content/Cargo.toml +++ b/components/content/Cargo.toml @@ -8,6 +8,7 @@ tera = "0.11" serde = "1" slug = "0.1" rayon = "1" +chrono = "0.4" errors = { path = "../errors" } config = { path = "../config" } diff --git a/components/content/src/lib.rs b/components/content/src/lib.rs index dc5cf9c..4cd93fa 100644 --- a/components/content/src/lib.rs +++ b/components/content/src/lib.rs @@ -2,6 +2,7 @@ extern crate tera; extern crate slug; extern crate serde; extern crate rayon; +extern crate chrono; extern crate errors; extern crate config; diff --git a/components/content/src/page.rs b/components/content/src/page.rs index 10bcf26..704b068 100644 --- a/components/content/src/page.rs +++ b/components/content/src/page.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::result::Result as StdResult; - +use chrono::Datelike; use tera::{Tera, Context as TeraContext}; use serde::ser::{SerializeStruct, self}; use slug::slugify; @@ -247,11 +247,21 @@ 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", 18)?; + let mut state = serializer.serialize_struct("page", 21)?; 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)?; + 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::>("year", &None)?; + state.serialize_field::>("month", &None)?; + state.serialize_field::>("day", &None)?; + } state.serialize_field("slug", &self.slug)?; state.serialize_field("path", &self.path)?; state.serialize_field("components", &self.components)?; diff --git a/docs/content/documentation/templates/pages-sections.md b/docs/content/documentation/templates/pages-sections.md index b8fa644..b4caa1f 100644 --- a/docs/content/documentation/templates/pages-sections.md +++ b/docs/content/documentation/templates/pages-sections.md @@ -39,8 +39,10 @@ next: Page?; toc: Array
; // Paths of colocated assets, relative to the content directory assets: Array; -// Paths of colocated image assets, ie. files with an extension of "jpg", "jpeg", "png", "gif", or "bmp" -images: Array; +// Year/month/day is only set if the page has a date and month/day are 1-indexed +year: Number?; +month: Number?; +day: Number?; ``` ## Section variables