Skip to content

Commit 9f2d8ec

Browse files
committed
Add defalte filters
1 parent 7a0cf26 commit 9f2d8ec

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/templates/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ sha2 = "0.9"
1717
url = "2"
1818
nom-bibtex = "0.3"
1919
num-format = "0.4"
20+
flate2 = "1.0"
2021

2122
errors = { path = "../errors" }
2223
utils = { path = "../utils" }

components/templates/src/filters.rs

+66-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::borrow::Cow;
22
use std::collections::HashMap;
33
use std::hash::BuildHasher;
4+
use std::io::prelude::*;
45
use std::path::PathBuf;
56

67
use base64::{decode, encode};
78
use config::Config;
9+
use flate2::{read::ZlibDecoder, write::ZlibEncoder, Compression};
810
use rendering::{render_content, RenderContext};
911
use tera::{
1012
to_value, try_get_value, Error as TeraError, Filter as TeraFilter, Result as TeraResult, Tera,
@@ -109,13 +111,36 @@ impl TeraFilter for NumFormatFilter {
109111
}
110112
}
111113

114+
pub fn deflate_compress<S: BuildHasher>(
115+
value: &Value,
116+
_: &HashMap<String, Value, S>,
117+
) -> TeraResult<Value> {
118+
let s = try_get_value!("deflate_compress", "value", String, value);
119+
let mut e = ZlibEncoder::new(Vec::new(), Compression::new(9));
120+
e.write_all(s.as_bytes())?;
121+
let result = e.finish()?;
122+
Ok(to_value(&encode(result))?)
123+
}
124+
125+
pub fn deflate_decompress<S: BuildHasher>(
126+
value: &Value,
127+
_: &HashMap<String, Value, S>,
128+
) -> TeraResult<Value> {
129+
let base64 = try_get_value!("deflate_decompress", "value", String, value);
130+
let s = decode(base64).unwrap();
131+
let mut d = ZlibDecoder::new(s.as_slice());
132+
let mut result = String::new();
133+
d.read_to_string(&mut result)?;
134+
Ok(to_value(result)?)
135+
}
136+
112137
#[cfg(test)]
113138
mod tests {
114139
use std::{collections::HashMap, path::PathBuf};
115140

116141
use tera::{to_value, Filter};
117142

118-
use super::{base64_decode, base64_encode, MarkdownFilter, NumFormatFilter};
143+
use super::{base64_decode, base64_encode, deflate_compress, deflate_decompress, MarkdownFilter, NumFormatFilter};
119144
use config::Config;
120145

121146
#[test]
@@ -135,7 +160,8 @@ mod tests {
135160
let args = HashMap::new();
136161
let config = Config::default();
137162
let permalinks = HashMap::new();
138-
let mut tera = super::load_tera(&PathBuf::new(), &config).map_err(tera::Error::msg).unwrap();
163+
let mut tera =
164+
super::load_tera(&PathBuf::new(), &config).map_err(tera::Error::msg).unwrap();
139165
tera.add_raw_template("shortcodes/explicitlang.html", "a{{ lang }}a").unwrap();
140166
let filter = MarkdownFilter { config, permalinks, tera };
141167
let result = filter.filter(&to_value(&"{{ explicitlang(lang='jp') }}").unwrap(), &args);
@@ -294,4 +320,42 @@ mod tests {
294320
assert_eq!(result.unwrap(), to_value(expected).unwrap());
295321
}
296322
}
323+
324+
#[test]
325+
fn deflate_compress_filter() {
326+
let tests = vec![
327+
("", "eNoDAAAAAAE="),
328+
("f", "eNpLAwAAZwBn"),
329+
("fo", "eNpLywcAAT0A1g=="),
330+
("foo", "eNpLy88HAAKCAUU="),
331+
("foob", "eNpLy89PAgAEKQGn"),
332+
("fooba", "eNpLy89PSgQABjECCA=="),
333+
("foobar", "eNpLy89PSiwCAAirAno="),
334+
];
335+
for (input, expected) in tests {
336+
let args = HashMap::new();
337+
let result = deflate_compress(&to_value(input).unwrap(), &args);
338+
assert!(result.is_ok());
339+
assert_eq!(result.unwrap(), to_value(expected).unwrap());
340+
}
341+
}
342+
343+
#[test]
344+
fn deflate_decompress_filter() {
345+
let tests = vec![
346+
("eNoDAAAAAAE=", ""),
347+
("eNpLAwAAZwBn", "f"),
348+
("eNpLywcAAT0A1g==", "fo"),
349+
("eNpLy88HAAKCAUU=", "foo"),
350+
("eNpLy89PAgAEKQGn", "foob"),
351+
("eNpLy89PSgQABjECCA==", "fooba"),
352+
("eNpLy89PSiwCAAirAno=", "foobar"),
353+
];
354+
for (input, expected) in tests {
355+
let args = HashMap::new();
356+
let result = deflate_decompress(&to_value(input).unwrap(), &args);
357+
assert!(result.is_ok());
358+
assert_eq!(result.unwrap(), to_value(expected).unwrap());
359+
}
360+
}
297361
}

components/templates/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ lazy_static! {
4242
.unwrap();
4343
tera.register_filter("base64_encode", filters::base64_encode);
4444
tera.register_filter("base64_decode", filters::base64_decode);
45+
tera.register_filter("deflate_compress", filters::deflate_compress);
46+
tera.register_filter("deflate_decompress", filters::deflate_decompress);
4547
tera
4648
};
4749
}

docs/content/documentation/templates/overview.md

+8
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ To format a number for a specific locale, you can use the `locale` argument and
101101
<!-- 10,00,000 -->
102102
```
103103

104+
### deflate_compress
105+
106+
Compresses the variable using deflate algorithm and encode the output as base64
107+
108+
### deflate_decompress
109+
110+
Decode the variable using base64 and then decompress using deflate algorithm
111+
104112
## Built-in functions
105113

106114
Zola adds a few Tera functions to [those built-in in Tera](https://tera.netlify.com/docs#built-in-functions)

0 commit comments

Comments
 (0)