Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add defalte filters #1622

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions components/templates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sha2 = "0.9"
url = "2"
nom-bibtex = "0.3"
num-format = "0.4"
flate2 = "1.0"

errors = { path = "../errors" }
utils = { path = "../utils" }
Expand Down
74 changes: 71 additions & 3 deletions components/templates/src/filters.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::hash::BuildHasher;
use std::io::prelude::*;
use std::path::PathBuf;

use base64::{decode, encode};
use base64::{decode, decode_config, encode, encode_config, URL_SAFE};
use config::Config;
use flate2::{read::ZlibDecoder, write::ZlibEncoder, Compression};
use rendering::{render_content, RenderContext};
use tera::{
to_value, try_get_value, Error as TeraError, Filter as TeraFilter, Result as TeraResult, Tera,
Expand Down Expand Up @@ -109,13 +111,39 @@ impl TeraFilter for NumFormatFilter {
}
}

pub fn deflate_compress<S: BuildHasher>(
value: &Value,
_: &HashMap<String, Value, S>,
) -> TeraResult<Value> {
let s = try_get_value!("deflate_compress", "value", String, value);
let mut e = ZlibEncoder::new(Vec::new(), Compression::new(9));
e.write_all(s.as_bytes())?;
let result = e.finish()?;
Ok(to_value(&encode_config(result, URL_SAFE))?)
}

pub fn deflate_decompress<S: BuildHasher>(
value: &Value,
_: &HashMap<String, Value, S>,
) -> TeraResult<Value> {
let base64 = try_get_value!("deflate_decompress", "value", String, value);
let s = decode_config(base64, URL_SAFE).unwrap();
let mut d = ZlibDecoder::new(s.as_slice());
let mut result = String::new();
d.read_to_string(&mut result)?;
Ok(to_value(result)?)
}

#[cfg(test)]
mod tests {
use std::{collections::HashMap, path::PathBuf};

use tera::{to_value, Filter};

use super::{base64_decode, base64_encode, MarkdownFilter, NumFormatFilter};
use super::{
base64_decode, base64_encode, deflate_compress, deflate_decompress, MarkdownFilter,
NumFormatFilter,
};
use config::Config;

#[test]
Expand All @@ -135,7 +163,8 @@ mod tests {
let args = HashMap::new();
let config = Config::default();
let permalinks = HashMap::new();
let mut tera = super::load_tera(&PathBuf::new(), &config).map_err(tera::Error::msg).unwrap();
let mut tera =
super::load_tera(&PathBuf::new(), &config).map_err(tera::Error::msg).unwrap();
tera.add_raw_template("shortcodes/explicitlang.html", "a{{ lang }}a").unwrap();
let filter = MarkdownFilter { config, permalinks, tera };
let result = filter.filter(&to_value(&"{{ explicitlang(lang='jp') }}").unwrap(), &args);
Expand Down Expand Up @@ -294,4 +323,43 @@ mod tests {
assert_eq!(result.unwrap(), to_value(expected).unwrap());
}
}

#[test]
fn deflate_compress_filter() {
let tests = vec![
("", "eNoDAAAAAAE="),
("f", "eNpLAwAAZwBn"),
("fo", "eNpLywcAAT0A1g=="),
("foo", "eNpLy88HAAKCAUU="),
("foob", "eNpLy89PAgAEKQGn"),
("fooba", "eNpLy89PSgQABjECCA=="),
("foobar", "eNpLy89PSiwCAAirAno="),
];
for (input, expected) in tests {
let args = HashMap::new();
let result = deflate_compress(&to_value(input).unwrap(), &args);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(expected).unwrap());
}
}

#[test]
fn deflate_decompress_filter() {
let tests = vec![
("eNoDAAAAAAE=", ""),
("eNpLAwAAZwBn", "f"),
("eNpLywcAAT0A1g==", "fo"),
("eNpLy88HAAKCAUU=", "foo"),
("eNpLy89PAgAEKQGn", "foob"),
("eNpLy89PSgQABjECCA==", "fooba"),
("eNpLy89PSiwCAAirAno=", "foobar"),
("eNpLyUwvSizIUHBXqPZIzcnJ17ULzy_KSanlAgB1EAjQ", "digraph G {Hello->World}\n"),
];
for (input, expected) in tests {
let args = HashMap::new();
let result = deflate_decompress(&to_value(input).unwrap(), &args);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(expected).unwrap());
}
}
}
2 changes: 2 additions & 0 deletions components/templates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ lazy_static! {
.unwrap();
tera.register_filter("base64_encode", filters::base64_encode);
tera.register_filter("base64_decode", filters::base64_decode);
tera.register_filter("deflate_compress", filters::deflate_compress);
tera.register_filter("deflate_decompress", filters::deflate_decompress);
tera
};
}
Expand Down
8 changes: 8 additions & 0 deletions docs/content/documentation/templates/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ To format a number for a specific locale, you can use the `locale` argument and
<!-- 10,00,000 -->
```

### deflate_compress

Compresses the variable using deflate algorithm and encode the output as base64

### deflate_decompress

Decode the variable using base64 and then decompress using deflate algorithm

## Built-in functions

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