Skip to content

Commit

Permalink
clippy + fmt + fix toml dates in extra arrays
Browse files Browse the repository at this point in the history
Closes #1048
  • Loading branch information
Keats committed Jun 18, 2020
1 parent 5e31a32 commit ade442a
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 96 deletions.
104 changes: 68 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ name = "zola"

[dependencies]
atty = "0.2.11"
clap = "2"
clap = { version = "2", default-features = false }
chrono = "0.4"
lazy_static = "1.1.0"
termcolor = "1.0.4"
Expand Down
2 changes: 1 addition & 1 deletion components/config/examples/generate_sublime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
builder.add_plain_text_syntax();
match builder.add_from_folder(package_dir, true) {
Ok(_) => (),
Err(e) => println!("Loading error: {:?}", e)
Err(e) => println!("Loading error: {:?}", e),
};
let ss = builder.build();
dump_to_file(&ss, packpath_newlines).unwrap();
Expand Down
1 change: 0 additions & 1 deletion components/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::path::{Path, PathBuf};
use globset::{Glob, GlobSet, GlobSetBuilder};
use serde_derive::{Deserialize, Serialize};
use syntect::parsing::{SyntaxSet, SyntaxSetBuilder};
use toml;
use toml::Value as Toml;

use crate::highlighting::THEME_SET;
Expand Down
36 changes: 27 additions & 9 deletions components/front_matter/src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::HashMap;
use chrono::prelude::*;
use serde_derive::Deserialize;
use tera::{Map, Value};
use toml;

use errors::{bail, Result};
use utils::de::{fix_toml_dates, from_toml_datetime};
Expand Down Expand Up @@ -57,15 +56,17 @@ pub struct PageFrontMatter {
pub extra: Map<String, Value>,
}

/// Parse a TOML datetime from a string.
/// There are three alternatives: an offset datetime (plain RFC3339), a local datetime
/// (RFC3339 with timezone omitted) and a local date (YYYY-MM-DD). This tries each in
/// order.
fn parse_datetime(d: &String) -> Option<NaiveDateTime> {
/// Parse a string for a datetime coming from one of the supported TOML format
/// There are three alternatives:
/// 1. an offset datetime (plain RFC3339)
/// 2. a local datetime (RFC3339 with timezone omitted)
/// 3. a local date (YYYY-MM-DD).
/// This tries each in order.
fn parse_datetime(d: &str) -> Option<NaiveDateTime> {
DateTime::parse_from_rfc3339(d)
.or(DateTime::parse_from_rfc3339(format!("{}Z", d).as_ref()))
.or_else(|_| DateTime::parse_from_rfc3339(format!("{}Z", d).as_ref()))
.map(|s| s.naive_local())
.or(NaiveDate::parse_from_str(d, "%Y-%m-%d").map(|s| s.and_hms(0, 0, 0)))
.or_else(|_| NaiveDate::parse_from_str(d, "%Y-%m-%d").map(|s| s.and_hms(0, 0, 0)))
.ok()
}

Expand Down Expand Up @@ -107,7 +108,7 @@ impl PageFrontMatter {
/// Converts the TOML datetime to a Chrono naive datetime
/// Also grabs the year/month/day tuple that will be used in serialization
pub fn date_to_datetime(&mut self) {
self.datetime = self.date.as_ref().and_then(parse_datetime);
self.datetime = self.date.as_ref().map(|s| s.as_ref()).and_then(parse_datetime);
self.datetime_tuple = self.datetime.map(|dt| (dt.year(), dt.month(), dt.day()));
}

Expand Down Expand Up @@ -319,6 +320,23 @@ mod tests {
assert_eq!(res.unwrap().extra["something"]["some-date"], to_value("2002-14-01").unwrap());
}

#[test]
fn can_parse_fully_nested_dates_in_extra() {
let content = r#"
title = "Hello"
description = "hey there"
[extra]
date_example = 2020-05-04
[[extra.questions]]
date = 2020-05-03
name = "Who is the prime minister of Uganda?""#;
let res = PageFrontMatter::parse(content);
println!("{:?}", res);
assert!(res.is_ok());
assert_eq!(res.unwrap().extra["questions"][0]["date"], to_value("2020-05-03").unwrap());
}

#[test]
fn can_parse_taxonomies() {
let content = r#"
Expand Down
1 change: 0 additions & 1 deletion components/front_matter/src/section.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use serde_derive::{Deserialize, Serialize};
use tera::{Map, Value};
use toml;

use super::{InsertAnchor, SortBy};
use errors::{bail, Result};
Expand Down
18 changes: 13 additions & 5 deletions components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,11 @@ impl Site {
pub fn register_early_global_fns(&mut self) {
self.tera.register_function(
"get_url",
global_fns::GetUrl::new(self.config.clone(), self.permalinks.clone(),
vec![self.static_path.clone(), self.output_path.clone(), self.content_path.clone()]),
global_fns::GetUrl::new(
self.config.clone(),
self.permalinks.clone(),
vec![self.static_path.clone(), self.output_path.clone(), self.content_path.clone()],
),
);
self.tera.register_function(
"resize_image",
Expand All @@ -551,9 +554,14 @@ impl Site {
"get_taxonomy_url",
global_fns::GetTaxonomyUrl::new(&self.config.default_language, &self.taxonomies),
);
self.tera.register_function("get_file_hash", global_fns::GetFileHash::new(
vec![self.static_path.clone(), self.output_path.clone(), self.content_path.clone()]
));
self.tera.register_function(
"get_file_hash",
global_fns::GetFileHash::new(vec![
self.static_path.clone(),
self.output_path.clone(),
self.content_path.clone(),
]),
);
}

pub fn register_tera_global_fns(&mut self) {
Expand Down
7 changes: 5 additions & 2 deletions components/site/tests/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,11 @@ fn can_cachebust_static_files() {
#[test]
fn can_get_hash_for_static_files() {
let (_, _tmp_dir, public) = build_site("test_site");
assert!(file_contains!(public, "index.html",
"src=\"https://replace-this-with-your-url.com/scripts/hello.js\""));
assert!(file_contains!(
public,
"index.html",
"src=\"https://replace-this-with-your-url.com/scripts/hello.js\""
));
assert!(file_contains!(public, "index.html",
"integrity=\"sha384-01422f31eaa721a6c4ac8c6fa09a27dd9259e0dfcf3c7593d7810d912a9de5ca2f582df978537bcd10f76896db61fbb9\""));
}
Expand Down
Loading

0 comments on commit ade442a

Please sign in to comment.