-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from Keats/filters
Add markdown and base64 filters
- Loading branch information
Showing
6 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::collections::{HashMap}; | ||
|
||
use base64::{encode, decode}; | ||
use pulldown_cmark as cmark; | ||
use tera::{Value, to_value, Result as TeraResult}; | ||
|
||
// fn(Value, HashMap<String, Value>) -> Result<Value> | ||
|
||
pub fn markdown(value: Value, _: HashMap<String, Value>) -> TeraResult<Value> { | ||
let s = try_get_value!("markdown", "value", String, value); | ||
|
||
let mut html = String::new(); | ||
let parser = cmark::Parser::new(&s); | ||
cmark::html::push_html(&mut html, parser); | ||
|
||
Ok(to_value(&html).unwrap()) | ||
} | ||
|
||
|
||
pub fn base64_encode(value: Value, _: HashMap<String, Value>) -> TeraResult<Value> { | ||
let s = try_get_value!("base64_encode", "value", String, value); | ||
Ok( | ||
to_value(&encode(s.as_bytes())).unwrap() | ||
) | ||
} | ||
|
||
pub fn base64_decode(value: Value, _: HashMap<String, Value>) -> TeraResult<Value> { | ||
let s = try_get_value!("base64_decode", "value", String, value); | ||
Ok( | ||
to_value( | ||
&String::from_utf8( | ||
decode(s.as_bytes()).unwrap() | ||
).unwrap() | ||
).unwrap() | ||
) | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::collections::HashMap; | ||
|
||
use tera::{to_value}; | ||
|
||
use super::{markdown, base64_decode, base64_encode}; | ||
|
||
#[test] | ||
fn test_markdown() { | ||
let result = markdown(to_value(&"# Hey").unwrap(), HashMap::new()); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), to_value(&"<h1>Hey</h1>\n").unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_base64_encode() { | ||
// from https://tools.ietf.org/html/rfc4648#section-10 | ||
let tests = vec![ | ||
("", ""), | ||
("f", "Zg=="), | ||
("fo", "Zm8="), | ||
("foo", "Zm9v"), | ||
("foob", "Zm9vYg=="), | ||
("fooba", "Zm9vYmE="), | ||
("foobar", "Zm9vYmFy") | ||
]; | ||
for (input, expected) in tests { | ||
let args = HashMap::new(); | ||
let result = base64_encode(to_value(input).unwrap(), args); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), to_value(expected).unwrap()); | ||
} | ||
} | ||
|
||
|
||
#[test] | ||
fn test_base64_decode() { | ||
let tests = vec![ | ||
("", ""), | ||
("Zg==", "f"), | ||
("Zm8=", "fo"), | ||
("Zm9v", "foo"), | ||
("Zm9vYg==", "foob"), | ||
("Zm9vYmE=", "fooba"), | ||
("Zm9vYmFy", "foobar") | ||
]; | ||
for (input, expected) in tests { | ||
let args = HashMap::new(); | ||
let result = base64_decode(to_value(input).unwrap(), args); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), to_value(expected).unwrap()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters