Skip to content

Commit

Permalink
Add in 'truncate' and 'date' filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmiebfulton committed Aug 3, 2021
1 parent f6cc2e8 commit c3493ef
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
1 change: 1 addition & 0 deletions archetect-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 48,7 @@ url = "2"
humansize = "1"
# used in date format filter
chrono = "0.4"
chrono-tz = "0.5"
# used in truncate filter
unic-segment = "0.9"
thiserror = "1.0.26"
Expand Down
13 changes: 13 additions & 0 deletions archetect-core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 271,17 @@ mod tests {
std::fs::create_dir_all(archetect.layout().configs_dir()).expect("Error creating directory");
std::fs::create_dir_all(archetect.layout().git_cache_dir()).expect("Error creating directory");
}

mod templating {
use crate::Archetect;
use crate::vendor::tera::Context;

#[test]
fn test_truncate_filter() {
let mut archetect = Archetect::build().unwrap();
let template = "{{ 'Jimmie' | truncate(length=1, end='') }}";
let result = archetect.render_string(template, &Context::new()).unwrap();
assert_eq!(&result, "J");
}
}
}
16 changes: 2 additions & 14 deletions archetect-core/src/vendor/tera/builtins/filters/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 4,12 @@ use std::iter::FromIterator;

use crate::vendor::tera::errors::{Error, Result};
use crate::vendor::tera::utils::render_to_string;
#[cfg(feature = "builtins")]

use chrono::{
format::{Item, StrftimeItems},
DateTime, FixedOffset, NaiveDate, NaiveDateTime, Utc,
};
#[cfg(feature = "builtins")]

use chrono_tz::Tz;
use serde_json::value::{to_value, Value};
use serde_json::{to_string, to_string_pretty};
Expand Down Expand Up @@ -65,7 65,6 @@ pub fn json_encode(value: &Value, args: &HashMap<String, Value>) -> Result<Value
///
/// a full reference for the time formatting syntax is available
/// on [chrono docs](https://lifthrasiir.github.io/rust-chrono/chrono/format/strftime/index.html)
#[cfg(feature = "builtins")]
pub fn date(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
let format = match args.get("format") {
Some(val) => try_get_value!("date", "format", String, val),
Expand Down Expand Up @@ -151,7 150,6 @@ pub fn as_str(value: &Value, _: &HashMap<String, Value>) -> Result<Value> {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "builtins")]
use chrono::{DateTime, Local};
use serde_json;
use serde_json::value::to_value;
Expand Down Expand Up @@ -232,7 230,6 @@ mod tests {
);
}

#[cfg(feature = "builtins")]
#[test]
fn date_default() {
let args = HashMap::new();
Expand All @@ -241,7 238,6 @@ mod tests {
assert_eq!(result.unwrap(), to_value("2016-12-26").unwrap());
}

#[cfg(feature = "builtins")]
#[test]
fn date_custom_format() {
let mut args = HashMap::new();
Expand All @@ -253,7 249,6 @@ mod tests {

// https://zola.discourse.group/t/can-i-generate-a-random-number-within-a-range/238?u=keats
// https://github.com/chronotope/chrono/issues/47
#[cfg(feature = "builtins")]
#[test]
fn date_errors_on_incorrect_format() {
let mut args = HashMap::new();
Expand All @@ -262,7 257,6 @@ mod tests {
assert!(result.is_err());
}

#[cfg(feature = "builtins")]
#[test]
fn date_rfc3339() {
let args = HashMap::new();
Expand All @@ -272,7 266,6 @@ mod tests {
assert_eq!(result.unwrap(), to_value(dt.format("%Y-%m-%d").to_string()).unwrap());
}

#[cfg(feature = "builtins")]
#[test]
fn date_rfc3339_preserves_timezone() {
let mut args = HashMap::new();
Expand All @@ -282,7 275,6 @@ mod tests {
assert_eq!(result.unwrap(), to_value("1996-12-19 -0800").unwrap());
}

#[cfg(feature = "builtins")]
#[test]
fn date_yyyy_mm_dd() {
let mut args = HashMap::new();
Expand All @@ -292,7 284,6 @@ mod tests {
assert_eq!(result.unwrap(), to_value("Sun, 05 Mar 2017 00:00:00 0000").unwrap());
}

#[cfg(feature = "builtins")]
#[test]
fn date_from_naive_datetime() {
let mut args = HashMap::new();
Expand All @@ -304,7 295,6 @@ mod tests {
}

// https://github.com/getzola/zola/issues/1279
#[cfg(feature = "builtins")]
#[test]
fn date_format_doesnt_panic() {
let mut args = HashMap::new();
Expand All @@ -313,7 303,6 @@ mod tests {
assert!(result.is_ok());
}

#[cfg(feature = "builtins")]
#[test]
fn date_with_timezone() {
let mut args = HashMap::new();
Expand All @@ -323,7 312,6 @@ mod tests {
assert_eq!(result.unwrap(), to_value("2019-09-18").unwrap());
}

#[cfg(feature = "builtins")]
#[test]
fn date_with_invalid_timezone() {
let mut args = HashMap::new();
Expand Down
6 changes: 0 additions & 6 deletions archetect-core/src/vendor/tera/builtins/filters/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 8,6 @@ use crate::try_get_value;

#[cfg(feature = "builtins")]
use percent_encoding::{percent_encode, AsciiSet, NON_ALPHANUMERIC};
#[cfg(feature = "builtins")]
use unic_segment::GraphemeIndices;

use crate::vendor::tera::errors::{Error, Result};
Expand Down Expand Up @@ -155,7 154,6 @@ pub fn trim_end_matches(value: &Value, args: &HashMap<String, Value>) -> Result<
/// The return value of this function might be longer than `length`: the `end`
/// string is *added* after the truncation occurs.
///
#[cfg(feature = "builtins")]
pub fn truncate(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
let s = try_get_value!("truncate", "value", String, value);
let length = match args.get("length") {
Expand Down Expand Up @@ -464,7 462,6 @@ mod tests {
}
}

#[cfg(feature = "builtins")]
#[test]
fn test_truncate_smaller_than_length() {
let mut args = HashMap::new();
Expand All @@ -474,7 471,6 @@ mod tests {
assert_eq!(result.unwrap(), to_value("hello").unwrap());
}

#[cfg(feature = "builtins")]
#[test]
fn test_truncate_when_required() {
let mut args = HashMap::new();
Expand All @@ -484,7 480,6 @@ mod tests {
assert_eq!(result.unwrap(), to_value("日本…").unwrap());
}

#[cfg(feature = "builtins")]
#[test]
fn test_truncate_custom_end() {
let mut args = HashMap::new();
Expand All @@ -495,7 490,6 @@ mod tests {
assert_eq!(result.unwrap(), to_value("日本").unwrap());
}

#[cfg(feature = "builtins")]
#[test]
fn test_truncate_multichar_grapheme() {
let mut args = HashMap::new();
Expand Down
2 changes: 0 additions & 2 deletions archetect-core/src/vendor/tera/tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 544,6 @@ impl Tera {
self.register_filter("trim_end", string::trim_end);
self.register_filter("trim_start_matches", string::trim_start_matches);
self.register_filter("trim_end_matches", string::trim_end_matches);
#[cfg(feature = "builtins")]
self.register_filter("truncate", string::truncate);
self.register_filter("wordcount", string::wordcount);
self.register_filter("replace", string::replace);
Expand Down Expand Up @@ -585,7 584,6 @@ impl Tera {

self.register_filter("length", common::length);
self.register_filter("reverse", common::reverse);
#[cfg(feature = "builtins")]
self.register_filter("date", common::date);
self.register_filter("json_encode", common::json_encode);
self.register_filter("as_str", common::as_str);
Expand Down

0 comments on commit c3493ef

Please sign in to comment.