From 598472d5c18ba2f5a11b7dbfcf77b7ba6989145e Mon Sep 17 00:00:00 2001 From: bott Date: Mon, 24 Sep 2018 18:10:09 +0200 Subject: Add wordcount filter --- askama_shared/src/filters/mod.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs index 546977a..60d17f1 100644 --- a/askama_shared/src/filters/mod.rs +++ b/askama_shared/src/filters/mod.rs @@ -20,7 +20,7 @@ use escaping::{self, MarkupDisplay}; // Askama or should refer to a local `filters` module. It should contain all the // filters shipped with Askama, even the optional ones (since optional inclusion // in the const vector based on features seems impossible right now). -pub const BUILT_IN_FILTERS: [&str; 16] = [ +pub const BUILT_IN_FILTERS: [&str; 17] = [ "abs", "capitalize", "e", @@ -36,6 +36,7 @@ pub const BUILT_IN_FILTERS: [&str; 16] = [ "truncate", "upper", "uppercase", + "wordcount", "json", // Optional feature; reserve the name anyway ]; @@ -187,6 +188,13 @@ pub fn capitalize(s: &fmt::Display) -> Result { } } +/// Count the words in that string. +pub fn wordcount(s: &fmt::Display) -> Result { + let s = format!("{}", s); + + Ok(s.split_whitespace().count()) +} + #[cfg(test)] mod tests { use super::*; @@ -291,4 +299,12 @@ mod tests { assert_eq!(capitalize(&"FoO").unwrap(), "Foo".to_string()); assert_eq!(capitalize(&"foO BAR").unwrap(), "Foo bar".to_string()); } + + #[test] + fn test_wordcount() { + assert_eq!(wordcount(&"").unwrap(), 0); + assert_eq!(wordcount(&" \n\t").unwrap(), 0); + assert_eq!(wordcount(&"foo").unwrap(), 1); + assert_eq!(wordcount(&"foo bar").unwrap(), 2); + } } -- cgit