Skip to content

Commit

Permalink
Merge pull request #29 from Keats/filters
Browse files Browse the repository at this point in the history
Add markdown and base64 filters
  • Loading branch information
Keats authored Apr 4, 2017
2 parents edd41a5 + b86a30f commit 5cd5884
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
- Fix page rendering not working when containing `+++`
- Add shortcodes (see README for details)
- Allow relative links to other content in markdown links
- Add `markdown`, `base64_encode` and `base64_decode` filters to the Tera instance of Gutenberg
10 changes: 10 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ syntect = "1"
chrono = "0.3"
toml = { version = "0.3", default-features = false, features = ["serde"]}
term-painter = "0.2"
base64 = "0.4"

# Below is for the serve cmd
staticfile = "0.4"
Expand Down
93 changes: 93 additions & 0 deletions src/filters.rs
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());
}
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ extern crate toml;
extern crate walkdir;
extern crate pulldown_cmark;
extern crate regex;
#[macro_use]
extern crate tera;
extern crate glob;
extern crate syntect;
extern crate slug;
extern crate chrono;
extern crate base64;
#[cfg(test)]
extern crate tempdir;

Expand All @@ -25,6 +27,8 @@ mod front_matter;
mod site;
mod markdown;
mod section;
/// Additional filters for Tera
mod filters;

pub use site::{Site, GUTENBERG_TERA};
pub use config::{Config, get_config};
Expand Down
4 changes: 4 additions & 0 deletions src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use config::{Config, get_config};
use page::{Page, populate_previous_and_next_pages};
use utils::{create_file, create_directory};
use section::{Section};
use filters;


lazy_static! {
Expand Down Expand Up @@ -78,6 +79,9 @@ impl Site {
let tpl_glob = format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "templates/**/*");
let mut tera = Tera::new(&tpl_glob).chain_err(|| "Error parsing templates")?;
tera.extend(&GUTENBERG_TERA)?;
tera.register_filter("markdown", filters::markdown);
tera.register_filter("base64_encode", filters::base64_encode);
tera.register_filter("base64_decode", filters::base64_decode);

let site = Site {
base_path: path.to_path_buf(),
Expand Down

0 comments on commit 5cd5884

Please sign in to comment.