Merge pull request #220 from Keats/markdown-inline

Markdown inline
This commit is contained in:
Vincent Prouillet 2018-02-02 17:29:16 +01:00 committed by GitHub
commit 54841b19bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 64 deletions

View file

@ -3,6 +3,7 @@
## 0.3.1 (unreleased) ## 0.3.1 (unreleased)
- Update Tera to fix regression - Update Tera to fix regression
- Add option for inline in markdown filter
## 0.3.0 (2018-01-25) ## 0.3.0 (2018-01-25)

View file

@ -14,7 +14,7 @@ CATEGORIES = ["c1", "c2", "c3", "c4"]
PAGE = """ PAGE = """
+++ +++
title = "Hello" title = "Hello"
date = "REPLACE_DATE" date = REPLACE_DATE
tags = REPLACE_TAG tags = REPLACE_TAG
category = "REPLACE_CATEGORY" category = "REPLACE_CATEGORY"
+++ +++
@ -105,6 +105,7 @@ title = "My site"
base_url = "https://replace-this-with-your-url.com" base_url = "https://replace-this-with-your-url.com"
generate_tags_pages = true generate_tags_pages = true
generate_categories_pages = true generate_categories_pages = true
theme = "sample"
[extra.author] [extra.author]
name = "Vincent Prouillet" name = "Vincent Prouillet"
@ -113,14 +114,15 @@ name = "Vincent Prouillet"
f.write(""" f.write("""
title = "My site" title = "My site"
base_url = "https://replace-this-with-your-url.com" base_url = "https://replace-this-with-your-url.com"
theme = "sample"
[extra.author] [extra.author]
name = "Vincent Prouillet" name = "Vincent Prouillet"
""") """)
# Re-use the test templates # Re-use the test templates
shutil.copytree("../test_site/templates", os.path.join(name, "templates")) shutil.copytree("../test_site/templates", os.path.join(name, "templates"))
shutil.copytree("../test_site/themes", os.path.join(name, "themes"))
def gen_section(path, num_pages, is_blog): def gen_section(path, num_pages, is_blog):

View file

@ -30,47 +30,47 @@ fn bench_loading_small_blog_with_syntax_highlighting(b: &mut test::Bencher) {
b.iter(|| site.load().unwrap()); b.iter(|| site.load().unwrap());
} }
#[bench] //#[bench]
fn bench_loading_medium_blog(b: &mut test::Bencher) { //fn bench_loading_medium_blog(b: &mut test::Bencher) {
let mut path = env::current_dir().unwrap().to_path_buf(); // let mut path = env::current_dir().unwrap().to_path_buf();
path.push("benches"); // path.push("benches");
path.push("medium-blog"); // path.push("medium-blog");
let mut site = Site::new(&path, "config.toml").unwrap(); // let mut site = Site::new(&path, "config.toml").unwrap();
//
b.iter(|| site.load().unwrap()); // b.iter(|| site.load().unwrap());
} //}
//
#[bench] //#[bench]
fn bench_loading_medium_blog_with_syntax_highlighting(b: &mut test::Bencher) { //fn bench_loading_medium_blog_with_syntax_highlighting(b: &mut test::Bencher) {
let mut path = env::current_dir().unwrap().to_path_buf(); // let mut path = env::current_dir().unwrap().to_path_buf();
path.push("benches"); // path.push("benches");
path.push("medium-blog"); // path.push("medium-blog");
let mut site = Site::new(&path, "config.toml").unwrap(); // let mut site = Site::new(&path, "config.toml").unwrap();
site.config.highlight_code = Some(true); // site.config.highlight_code = Some(true);
//
b.iter(|| site.load().unwrap()); // b.iter(|| site.load().unwrap());
} //}
//
#[bench] //#[bench]
fn bench_loading_big_blog(b: &mut test::Bencher) { //fn bench_loading_big_blog(b: &mut test::Bencher) {
let mut path = env::current_dir().unwrap().to_path_buf(); // let mut path = env::current_dir().unwrap().to_path_buf();
path.push("benches"); // path.push("benches");
path.push("big-blog"); // path.push("big-blog");
let mut site = Site::new(&path, "config.toml").unwrap(); // let mut site = Site::new(&path, "config.toml").unwrap();
//
b.iter(|| site.load().unwrap()); // b.iter(|| site.load().unwrap());
} //}
//
#[bench] //#[bench]
fn bench_loading_big_blog_with_syntax_highlighting(b: &mut test::Bencher) { //fn bench_loading_big_blog_with_syntax_highlighting(b: &mut test::Bencher) {
let mut path = env::current_dir().unwrap().to_path_buf(); // let mut path = env::current_dir().unwrap().to_path_buf();
path.push("benches"); // path.push("benches");
path.push("big-blog"); // path.push("big-blog");
let mut site = Site::new(&path, "config.toml").unwrap(); // let mut site = Site::new(&path, "config.toml").unwrap();
site.config.highlight_code = Some(true); // site.config.highlight_code = Some(true);
//
b.iter(|| site.load().unwrap()); // b.iter(|| site.load().unwrap());
} //}
//#[bench] //#[bench]
//fn bench_loading_huge_blog(b: &mut test::Bencher) { //fn bench_loading_huge_blog(b: &mut test::Bencher) {
@ -114,26 +114,26 @@ fn bench_loading_small_kb_with_syntax_highlighting(b: &mut test::Bencher) {
b.iter(|| site.load().unwrap()); b.iter(|| site.load().unwrap());
} }
#[bench] //#[bench]
fn bench_loading_medium_kb(b: &mut test::Bencher) { //fn bench_loading_medium_kb(b: &mut test::Bencher) {
let mut path = env::current_dir().unwrap().to_path_buf(); // let mut path = env::current_dir().unwrap().to_path_buf();
path.push("benches"); // path.push("benches");
path.push("medium-kb"); // path.push("medium-kb");
let mut site = Site::new(&path, "config.toml").unwrap(); // let mut site = Site::new(&path, "config.toml").unwrap();
//
b.iter(|| site.load().unwrap()); // b.iter(|| site.load().unwrap());
} //}
//
#[bench] //#[bench]
fn bench_loading_medium_kb_with_syntax_highlighting(b: &mut test::Bencher) { //fn bench_loading_medium_kb_with_syntax_highlighting(b: &mut test::Bencher) {
let mut path = env::current_dir().unwrap().to_path_buf(); // let mut path = env::current_dir().unwrap().to_path_buf();
path.push("benches"); // path.push("benches");
path.push("medium-kb"); // path.push("medium-kb");
let mut site = Site::new(&path, "config.toml").unwrap(); // let mut site = Site::new(&path, "config.toml").unwrap();
site.config.highlight_code = Some(true); // site.config.highlight_code = Some(true);
//
b.iter(|| site.load().unwrap()); // b.iter(|| site.load().unwrap());
} //}
//#[bench] //#[bench]
//fn bench_loading_huge_kb(b: &mut test::Bencher) { //fn bench_loading_huge_kb(b: &mut test::Bencher) {

View file

@ -5,13 +5,25 @@ use pulldown_cmark as cmark;
use tera::{Value, to_value, Result as TeraResult}; use tera::{Value, to_value, Result as TeraResult};
pub fn markdown(value: Value, _: HashMap<String, Value>) -> TeraResult<Value> { pub fn markdown(value: Value, args: HashMap<String, Value>) -> TeraResult<Value> {
let s = try_get_value!("markdown", "value", String, value); let s = try_get_value!("markdown", "value", String, value);
let inline = match args.get("inline") {
Some(val) => try_get_value!("markdown", "inline", bool, val),
None => false,
};
let mut html = String::new(); let mut html = String::new();
let parser = cmark::Parser::new(&s); let parser = cmark::Parser::new(&s);
cmark::html::push_html(&mut html, parser); cmark::html::push_html(&mut html, parser);
if inline {
html = html
.trim_left_matches("<p>")
// pulldown_cmark finishes a paragraph with `</p>\n`
.trim_right_matches("</p>\n")
.to_string();
}
Ok(to_value(&html).unwrap()) Ok(to_value(&html).unwrap())
} }
@ -50,6 +62,15 @@ mod tests {
assert_eq!(result.unwrap(), to_value(&"<h1>Hey</h1>\n").unwrap()); assert_eq!(result.unwrap(), to_value(&"<h1>Hey</h1>\n").unwrap());
} }
#[test]
fn markdown_filter_inline() {
let mut args = HashMap::new();
args.insert("inline".to_string(), to_value(true).unwrap());
let result = markdown(to_value(&"Using `map`, `filter`, and `fold` instead of `for`").unwrap(), args);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(&"Using <code>map</code>, <code>filter</code>, and <code>fold</code> instead of <code>for</code>").unwrap());
}
#[test] #[test]
fn base64_encode_filter() { fn base64_encode_filter() {
// from https://tools.ietf.org/html/rfc4648#section-10 // from https://tools.ietf.org/html/rfc4648#section-10

View file

@ -30,6 +30,13 @@ Gutenberg adds a few filters, in addition of the ones already present in Tera.
Converts the given variable to HTML using Markdown. This doesn't apply any of the Converts the given variable to HTML using Markdown. This doesn't apply any of the
features that Gutenberg adds to Markdown: internal links, shortcodes etc won't work. features that Gutenberg adds to Markdown: internal links, shortcodes etc won't work.
By default, the filter will wrap all text into a paragraph. To disable that, you can
pass `true` to the inline argument:
```jinja2
{{ some_text | markdown(inline=true) }}
```
### base64_encode ### base64_encode
Encode the variable to base64. Encode the variable to base64.