diff options
author | João Oliveira <hello@jxs.pt> | 2020-03-30 15:11:10 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-03-30 16:49:57 +0200 |
commit | 68964fa8d15d6d503ad010e94a6d7a54037ecb40 (patch) | |
tree | a2f9c7a2b7c49304f8cb2a643258a6183223f231 /askama_shared/src | |
parent | 3f202b32f270e8945a07962ca4413e374e2aadee (diff) | |
download | askama-68964fa8d15d6d503ad010e94a6d7a54037ecb40.tar.gz askama-68964fa8d15d6d503ad010e94a6d7a54037ecb40.tar.bz2 askama-68964fa8d15d6d503ad010e94a6d7a54037ecb40.zip |
add urlencode filter
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/filters/mod.rs | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs index 1667f40..d5a7c88 100644 --- a/askama_shared/src/filters/mod.rs +++ b/askama_shared/src/filters/mod.rs @@ -24,6 +24,8 @@ use askama_escape::{Escaper, MarkupDisplay}; use humansize::{file_size_opts, FileSize}; #[cfg(feature = "num_traits")] use num_traits::{cast::NumCast, Signed}; +#[cfg(feature = "percent-encoding")] +use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; use super::Result; @@ -31,7 +33,7 @@ use super::Result; // 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; 23] = [ +pub const BUILT_IN_FILTERS: [&str; 24] = [ "abs", "capitalize", "center", @@ -52,6 +54,7 @@ pub const BUILT_IN_FILTERS: [&str; 23] = [ "truncate", "upper", "uppercase", + "urlencode", "wordcount", "json", // Optional feature; reserve the name anyway "yaml", // Optional feature; reserve the name anyway @@ -102,6 +105,13 @@ pub fn filesizeformat(b: usize) -> Result<String> { .map_err(|_| Fmt(fmt::Error)) } +#[cfg(feature = "percent-encoding")] +/// Returns the the UTF-8 encoded String of the given input. +pub fn urlencode(s: &dyn fmt::Display) -> Result<String> { + let s = s.to_string(); + Ok(utf8_percent_encode(&s, NON_ALPHANUMERIC).to_string()) +} + /// Formats arguments according to the specified format /// /// The first argument to this filter must be a string literal (as in normal @@ -308,6 +318,15 @@ mod tests { assert_eq!(filesizeformat(1024).unwrap(), "1.02 KB"); } + #[cfg(feature = "percent-encoding")] + #[test] + fn test_urlencoding() { + assert_eq!( + urlencode(&"/path/to/dir with spaces").unwrap(), + "%2Fpath%2Fto%2Fdir%20with%20spaces" + ); + } + #[test] fn test_linebreaks() { assert_eq!( |