2018-07-16 08:54:05 +00:00
|
|
|
use std::collections::HashMap;
|
2018-07-31 13:17:31 +00:00
|
|
|
use std::result::Result as StdResult;
|
2017-05-13 04:01:38 +00:00
|
|
|
|
|
|
|
use chrono::prelude::*;
|
2018-02-25 18:25:15 +00:00
|
|
|
use tera::{Map, Value};
|
|
|
|
use serde::{Deserialize, Deserializer};
|
2017-05-13 04:01:38 +00:00
|
|
|
use toml;
|
|
|
|
|
2017-07-01 07:47:41 +00:00
|
|
|
use errors::Result;
|
2017-05-13 04:01:38 +00:00
|
|
|
|
2018-01-14 17:03:57 +00:00
|
|
|
|
2018-02-25 18:25:15 +00:00
|
|
|
fn from_toml_datetime<'de, D>(deserializer: D) -> StdResult<Option<String>, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
toml::value::Datetime::deserialize(deserializer)
|
|
|
|
.map(|s| Some(s.to_string()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns key/value for a converted date from TOML.
|
|
|
|
/// If the table itself is the TOML struct, only return its value without the key
|
|
|
|
fn convert_toml_date(table: Map<String, Value>) -> Value {
|
|
|
|
let mut new = Map::new();
|
|
|
|
|
2018-05-06 20:58:39 +00:00
|
|
|
for (k, v) in table {
|
2018-02-25 18:25:15 +00:00
|
|
|
if k == "$__toml_private_datetime" {
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
match v {
|
|
|
|
Value::Object(mut o) => {
|
|
|
|
// that was a toml datetime object, just return the date
|
|
|
|
if let Some(toml_date) = o.remove("$__toml_private_datetime") {
|
|
|
|
new.insert(k, toml_date);
|
|
|
|
return Value::Object(new);
|
|
|
|
}
|
|
|
|
new.insert(k, convert_toml_date(o));
|
2018-07-31 13:17:31 +00:00
|
|
|
}
|
2018-02-25 18:25:15 +00:00
|
|
|
_ => { new.insert(k, v); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Object(new)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// TOML datetimes will be serialized as a struct but we want the
|
|
|
|
/// stringified version for json, otherwise they are going to be weird
|
|
|
|
fn fix_toml_dates(table: Map<String, Value>) -> Value {
|
|
|
|
let mut new = Map::new();
|
|
|
|
|
|
|
|
for (key, value) in table {
|
|
|
|
match value {
|
|
|
|
Value::Object(mut o) => {
|
|
|
|
new.insert(key, convert_toml_date(o));
|
2018-07-31 13:17:31 +00:00
|
|
|
}
|
|
|
|
_ => { new.insert(key, value); }
|
2018-02-25 18:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Object(new)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-13 04:01:38 +00:00
|
|
|
/// The front matter of every page
|
2018-09-21 07:40:52 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
2018-03-15 17:58:32 +00:00
|
|
|
#[serde(default)]
|
2017-05-13 04:01:38 +00:00
|
|
|
pub struct PageFrontMatter {
|
|
|
|
/// <title> of the page
|
|
|
|
pub title: Option<String>,
|
|
|
|
/// Description in <meta> that appears when linked, e.g. on twitter
|
|
|
|
pub description: Option<String>,
|
|
|
|
/// Date if we want to order pages (ie blog post)
|
2018-02-25 18:25:15 +00:00
|
|
|
#[serde(default, deserialize_with = "from_toml_datetime")]
|
|
|
|
pub date: Option<String>,
|
2018-09-21 07:40:52 +00:00
|
|
|
/// Chrono converted datetime
|
|
|
|
#[serde(default, skip_deserializing)]
|
|
|
|
pub datetime: Option<NaiveDateTime>,
|
|
|
|
/// The converted date into a (year, month, day) tuple
|
|
|
|
#[serde(default, skip_deserializing)]
|
|
|
|
pub datetime_tuple: Option<(i32, u32, u32)>,
|
2017-09-25 09:55:43 +00:00
|
|
|
/// Whether this page is a draft and should be ignored for pagination etc
|
2018-03-21 15:18:24 +00:00
|
|
|
pub draft: bool,
|
2017-05-13 04:01:38 +00:00
|
|
|
/// The page slug. Will be used instead of the filename if present
|
|
|
|
/// Can't be an empty string if present
|
|
|
|
pub slug: Option<String>,
|
2017-10-04 00:35:37 +00:00
|
|
|
/// The path the page appears at, overrides the slug if set in the front-matter
|
2017-05-13 04:01:38 +00:00
|
|
|
/// otherwise is set after parsing front matter and sections
|
|
|
|
/// Can't be an empty string if present
|
2017-10-04 00:35:37 +00:00
|
|
|
pub path: Option<String>,
|
2018-07-16 08:54:05 +00:00
|
|
|
pub taxonomies: HashMap<String, Vec<String>>,
|
2017-05-13 04:01:38 +00:00
|
|
|
/// Integer to use to order content. Lowest is at the bottom, highest first
|
|
|
|
pub order: Option<usize>,
|
2017-07-01 10:13:21 +00:00
|
|
|
/// Integer to use to order content. Highest is at the bottom, lowest first
|
|
|
|
pub weight: Option<usize>,
|
2017-06-16 14:09:01 +00:00
|
|
|
/// All aliases for that page. Gutenberg will create HTML templates that will
|
2017-09-27 14:37:17 +00:00
|
|
|
/// redirect to this
|
2017-06-16 14:09:01 +00:00
|
|
|
#[serde(skip_serializing)]
|
2018-03-21 15:18:24 +00:00
|
|
|
pub aliases: Vec<String>,
|
2017-05-13 13:37:01 +00:00
|
|
|
/// Specify a template different from `page.html` to use for that page
|
2017-05-13 04:01:38 +00:00
|
|
|
#[serde(skip_serializing)]
|
|
|
|
pub template: Option<String>,
|
2018-03-14 21:03:06 +00:00
|
|
|
/// Whether the page is included in the search index
|
|
|
|
/// Defaults to `true` but is only used if search if explicitly enabled in the config.
|
2018-03-15 17:58:32 +00:00
|
|
|
#[serde(skip_serializing)]
|
2018-03-14 21:03:06 +00:00
|
|
|
pub in_search_index: bool,
|
2017-05-13 04:01:38 +00:00
|
|
|
/// Any extra parameter present in the front matter
|
2018-02-25 18:25:15 +00:00
|
|
|
pub extra: Map<String, Value>,
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PageFrontMatter {
|
|
|
|
pub fn parse(toml: &str) -> Result<PageFrontMatter> {
|
2018-02-25 18:25:15 +00:00
|
|
|
let mut f: PageFrontMatter = match toml::from_str(toml) {
|
2017-05-13 04:01:38 +00:00
|
|
|
Ok(d) => d,
|
|
|
|
Err(e) => bail!(e),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(ref slug) = f.slug {
|
|
|
|
if slug == "" {
|
|
|
|
bail!("`slug` can't be empty if present")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 00:35:37 +00:00
|
|
|
if let Some(ref path) = f.path {
|
|
|
|
if path == "" {
|
|
|
|
bail!("`path` can't be empty if present")
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 18:25:15 +00:00
|
|
|
f.extra = match fix_toml_dates(f.extra) {
|
|
|
|
Value::Object(o) => o,
|
|
|
|
_ => unreachable!("Got something other than a table in page extra"),
|
|
|
|
};
|
2018-09-21 07:40:52 +00:00
|
|
|
|
|
|
|
f.date_to_datetime();
|
|
|
|
|
2017-05-13 04:01:38 +00:00
|
|
|
Ok(f)
|
|
|
|
}
|
|
|
|
|
2018-01-14 17:03:57 +00:00
|
|
|
/// Converts the TOML datetime to a Chrono naive datetime
|
2018-09-21 07:40:52 +00:00
|
|
|
/// Also grabs the year/month/day tuple that will be used in serialization
|
|
|
|
pub fn date_to_datetime(&mut self) {
|
|
|
|
self.datetime = if let Some(ref d) = self.date {
|
2018-02-25 18:25:15 +00:00
|
|
|
if d.contains('T') {
|
|
|
|
DateTime::parse_from_rfc3339(&d).ok().and_then(|s| Some(s.naive_local()))
|
2018-01-14 17:03:57 +00:00
|
|
|
} else {
|
2018-02-25 18:25:15 +00:00
|
|
|
NaiveDate::parse_from_str(&d, "%Y-%m-%d").ok().and_then(|s| Some(s.and_hms(0, 0, 0)))
|
2018-01-14 17:03:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
2018-09-21 07:40:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.datetime_tuple = if let Some(ref dt) = self.datetime {
|
|
|
|
Some((dt.year(), dt.month(), dt.day()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn order(&self) -> usize {
|
|
|
|
self.order.unwrap()
|
|
|
|
}
|
2017-05-13 13:37:01 +00:00
|
|
|
|
2017-07-01 10:13:21 +00:00
|
|
|
pub fn weight(&self) -> usize {
|
|
|
|
self.weight.unwrap()
|
|
|
|
}
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PageFrontMatter {
|
|
|
|
fn default() -> PageFrontMatter {
|
|
|
|
PageFrontMatter {
|
|
|
|
title: None,
|
|
|
|
description: None,
|
|
|
|
date: None,
|
2018-09-21 07:40:52 +00:00
|
|
|
datetime: None,
|
|
|
|
datetime_tuple: None,
|
2018-03-21 15:18:24 +00:00
|
|
|
draft: false,
|
2017-05-13 04:01:38 +00:00
|
|
|
slug: None,
|
2017-10-04 00:35:37 +00:00
|
|
|
path: None,
|
2018-07-16 08:54:05 +00:00
|
|
|
taxonomies: HashMap::new(),
|
2017-05-13 04:01:38 +00:00
|
|
|
order: None,
|
2017-07-01 10:13:21 +00:00
|
|
|
weight: None,
|
2018-03-21 15:18:24 +00:00
|
|
|
aliases: Vec::new(),
|
2018-03-14 21:03:06 +00:00
|
|
|
in_search_index: true,
|
2017-05-13 04:01:38 +00:00
|
|
|
template: None,
|
2018-02-25 18:25:15 +00:00
|
|
|
extra: Map::new(),
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 17:03:57 +00:00
|
|
|
|
2017-05-13 04:01:38 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-02-25 18:25:15 +00:00
|
|
|
use tera::to_value;
|
2017-05-13 04:01:38 +00:00
|
|
|
use super::PageFrontMatter;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_have_empty_front_matter() {
|
|
|
|
let content = r#" "#;
|
|
|
|
let res = PageFrontMatter::parse(content);
|
2018-02-25 18:25:15 +00:00
|
|
|
println!("{:?}", res);
|
2017-05-13 04:01:38 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_parse_valid_front_matter() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there""#;
|
|
|
|
let res = PageFrontMatter::parse(content);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let res = res.unwrap();
|
|
|
|
assert_eq!(res.title.unwrap(), "Hello".to_string());
|
|
|
|
assert_eq!(res.description.unwrap(), "hey there".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn errors_with_invalid_front_matter() {
|
|
|
|
let content = r#"title = 1\n"#;
|
|
|
|
let res = PageFrontMatter::parse(content);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn errors_on_present_but_empty_slug() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
slug = """#;
|
|
|
|
let res = PageFrontMatter::parse(content);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-10-04 00:35:37 +00:00
|
|
|
fn errors_on_present_but_empty_path() {
|
2017-05-13 04:01:38 +00:00
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
2017-10-04 00:35:37 +00:00
|
|
|
path = """#;
|
2017-05-13 04:01:38 +00:00
|
|
|
let res = PageFrontMatter::parse(content);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_parse_date_yyyy_mm_dd() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
2018-01-14 17:03:57 +00:00
|
|
|
date = 2016-10-10
|
|
|
|
"#;
|
2017-05-13 04:01:38 +00:00
|
|
|
let res = PageFrontMatter::parse(content).unwrap();
|
2018-01-14 17:03:57 +00:00
|
|
|
assert!(res.date.is_some());
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_parse_date_rfc3339() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
2018-01-14 17:03:57 +00:00
|
|
|
date = 2002-10-02T15:00:00Z
|
|
|
|
"#;
|
2017-05-13 04:01:38 +00:00
|
|
|
let res = PageFrontMatter::parse(content).unwrap();
|
2018-01-14 17:03:57 +00:00
|
|
|
assert!(res.date.is_some());
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cannot_parse_random_date_format() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
2018-01-14 17:03:57 +00:00
|
|
|
date = 2002/10/12"#;
|
|
|
|
let res = PageFrontMatter::parse(content);
|
|
|
|
assert!(res.is_err());
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:03:57 +00:00
|
|
|
#[test]
|
|
|
|
fn cannot_parse_invalid_date_format() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
date = 2002-14-01"#;
|
|
|
|
let res = PageFrontMatter::parse(content);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
2018-01-22 12:46:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cannot_parse_date_as_string() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
date = "2002-14-01""#;
|
|
|
|
let res = PageFrontMatter::parse(content);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
2018-02-25 18:25:15 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_parse_dates_in_extra() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
|
|
|
|
[extra]
|
|
|
|
some-date = 2002-14-01"#;
|
|
|
|
let res = PageFrontMatter::parse(content);
|
|
|
|
println!("{:?}", res);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap().extra["some-date"], to_value("2002-14-01").unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_parse_nested_dates_in_extra() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
|
|
|
|
[extra.something]
|
|
|
|
some-date = 2002-14-01"#;
|
|
|
|
let res = PageFrontMatter::parse(content);
|
|
|
|
println!("{:?}", res);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap().extra["something"]["some-date"], to_value("2002-14-01").unwrap());
|
|
|
|
}
|
2018-07-16 08:54:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_parse_taxonomies() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello World"
|
|
|
|
|
|
|
|
[taxonomies]
|
|
|
|
tags = ["Rust", "JavaScript"]
|
|
|
|
categories = ["Dev"]
|
|
|
|
"#;
|
|
|
|
let res = PageFrontMatter::parse(content);
|
|
|
|
println!("{:?}", res);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let res2 = res.unwrap();
|
|
|
|
assert_eq!(res2.taxonomies["categories"], vec!["Dev"]);
|
|
|
|
assert_eq!(res2.taxonomies["tags"], vec!["Rust", "JavaScript"]);
|
|
|
|
}
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|