diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8b2f77b..2fd926b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,3 +4,5 @@
- Add some colours in console
- Allow using a file other than config.toml for config
- Add sections to the index page context
+- Fix page rendering not working when containing `+++`
+- Add shortcodes (see README for details)
diff --git a/Cargo.lock b/Cargo.lock
index a03f1e6..fcaa5d1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7,13 +7,13 @@ dependencies = [
"error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"iron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"mount 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"notify 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"pulldown-cmark 0.0.14 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_derive 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 0.9.12 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 0.9.12 (registry+https://github.com/rust-lang/crates.io-index)",
"slug 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"staticfile 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntect 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -325,7 +325,7 @@ dependencies = [
"conduit-mime-types 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"error 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper 0.10.5 (registry+https://github.com/rust-lang/crates.io-index)",
- "lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"modifier 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -355,7 +355,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
-version = "0.2.4"
+version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@@ -555,7 +555,7 @@ version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
"onig_sys 61.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -588,7 +588,7 @@ dependencies = [
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.2.25 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 0.9.12 (registry+https://github.com/rust-lang/crates.io-index)",
"xml-rs 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -688,12 +688,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
-version = "0.9.11"
+version = "0.9.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde_codegen_internals"
-version = "0.14.1"
+version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"syn 0.11.9 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -701,11 +701,11 @@ dependencies = [
[[package]]
name = "serde_derive"
-version = "0.9.11"
+version = "0.9.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_codegen_internals 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_codegen_internals 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.11.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -717,7 +717,7 @@ dependencies = [
"dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 0.9.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -786,7 +786,7 @@ dependencies = [
"bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"flate2 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
"fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
- "lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"onig 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"plist 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -812,10 +812,10 @@ dependencies = [
"error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"humansize 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"pest 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 0.9.12 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 0.9.9 (registry+https://github.com/rust-lang/crates.io-index)",
"slug 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -882,7 +882,7 @@ name = "toml"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
- "serde 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 0.9.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -1093,7 +1093,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eb2f404fbc66fd9aac13e998248505e7ecb2ad8e44ab6388684c5fb11c6c251c"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"
-"checksum lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7291b1dd97d331f752620b02dfdbc231df7fc01bf282a00769e1cdb963c460dc"
+"checksum lazy_static 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "4732c563b9a21a406565c4747daa7b46742f082911ae4753f390dc9ec7ee1a97"
"checksum lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce12306c4739d86ee97c23139f3a34ddf0387bbf181bc7929d287025a8c3ef6b"
"checksum libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "88ee81885f9f04bff991e306fea7c1c60a5f0f9e409e99f6b40e3311a3363135"
"checksum log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "5141eca02775a762cc6cd564d8d2c50f67c0ea3a372cbf1c51592b3e029e10ad"
@@ -1134,9 +1134,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)" = "d4f410fedcf71af0345d7607d246e7ad15faaadd49d240ee3b24e5dc21a820ac"
"checksum sequence_trie 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c915714ca833b1d4d6b8f6a9d72a3ff632fe45b40a8d184ef79c81bec6327eed"
"checksum serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "9dad3f759919b92c3068c696c15c3d17238234498bbdcc80f2c469606f948ac8"
-"checksum serde 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)" = "a702319c807c016e51f672e5c77d6f0b46afddd744b5e437d6b8436b888b458f"
-"checksum serde_codegen_internals 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4d52006899f910528a10631e5b727973fe668f3228109d1707ccf5bad5490b6e"
-"checksum serde_derive 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)" = "f15ea24bd037b2d64646b4d934fa99c649be66e3f7b29fb595a5543b212b1452"
+"checksum serde 0.9.12 (registry+https://github.com/rust-lang/crates.io-index)" = "f023838e7e1878c679322dc7f66c3648bd33763a215fad752f378a623856898d"
+"checksum serde_codegen_internals 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bc888bd283bd2420b16ad0d860e35ad8acb21941180a83a189bb2046f9d00400"
+"checksum serde_derive 0.9.12 (registry+https://github.com/rust-lang/crates.io-index)" = "ebb753639f6d55ba1acbcd330ccaf4d9f5862353ac2851e43eac63c2a5343a11"
"checksum serde_json 0.9.9 (registry+https://github.com/rust-lang/crates.io-index)" = "dbc45439552eb8fb86907a2c41c1fd0ef97458efb87ff7f878db466eb581824e"
"checksum sha1 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cc30b1e1e8c40c121ca33b86c23308a090d19974ef001b4bf6e61fd1a0fb095c"
"checksum slab 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d807fd58c4181bbabed77cb3b891ba9748241a552bcc5be698faaebefc54f46e"
diff --git a/README.md b/README.md
index f8e52d0..98984a5 100644
--- a/README.md
+++ b/README.md
@@ -100,6 +100,9 @@ Gutenberg supports that pattern out of the box: you can create a folder, put a f
along with it that are NOT markdown.
Those assets will be copied in the same folder when building so you can just use a relative path to use them.
+A summary is only defined if you put `` in the content. If present in a page, the summary will be from
+the start up to that tag.s
+
### Sections
Sections represent a group of pages, for example a `tutorials` section of your site.
Sections are only created in Gutenberg when a file named `_index.md` is found in the `content` directory.
diff --git a/src/markdown.rs b/src/markdown.rs
index 0c17a86..5e49acd 100644
--- a/src/markdown.rs
+++ b/src/markdown.rs
@@ -1,13 +1,18 @@
use std::borrow::Cow::Owned;
+use std::collections::HashMap;
use pulldown_cmark as cmark;
use self::cmark::{Parser, Event, Tag};
-
+use regex::Regex;
use syntect::dumps::from_binary;
use syntect::easy::HighlightLines;
use syntect::parsing::SyntaxSet;
use syntect::highlighting::ThemeSet;
use syntect::html::{start_coloured_html_snippet, styles_to_coloured_html, IncludeBackground};
+use tera::{Tera, Context};
+
+use config::Config;
+use errors::{Result, ResultExt};
// We need to put those in a struct to impl Send and sync
@@ -20,117 +25,272 @@ unsafe impl Send for Setup {}
unsafe impl Sync for Setup {}
lazy_static!{
+ static ref SHORTCODE_RE: Regex = Regex::new(r#"\{(?:%|\{)\s+([[:alnum:]]+?)\(([[:alnum:]]+?="?.+?"?)\)\s+(?:%|\})\}"#).unwrap();
pub static ref SETUP: Setup = Setup {
syntax_set: SyntaxSet::load_defaults_newlines(),
theme_set: from_binary(include_bytes!("../sublime_themes/all.themedump"))
};
}
-
-struct CodeHighlightingParser<'a> {
- // The block we're currently highlighting
- highlighter: Option
".to_owned()));
+ }
+ let theme = &SETUP.theme_set.themes[&highlight_theme];
let syntax = info
.split(' ')
.next()
.and_then(|lang| SETUP.syntax_set.find_syntax_by_token(lang))
.unwrap_or_else(|| SETUP.syntax_set.find_syntax_plain_text());
- self.highlighter = Some(
- HighlightLines::new(syntax, theme)
- );
+ highlighter = Some(HighlightLines::new(syntax, theme));
let snippet = start_coloured_html_snippet(theme);
- Some(Event::Html(Owned(snippet)))
+ Event::Html(Owned(snippet))
},
Event::End(Tag::CodeBlock(_)) => {
+ in_code_block = false;
+ if !should_highlight{
+ return Event::Html(Owned("
\n".to_owned()))
+ }
// reset highlight and close the code block
- self.highlighter = None;
- Some(Event::Html(Owned("".to_owned())))
+ highlighter = None;
+ Event::Html(Owned("".to_owned()))
},
- _ => Some(item)
- }
+ Event::Start(Tag::Code) => {
+ in_code_block = true;
+ event
+ },
+ Event::End(Tag::Code) => {
+ in_code_block = false;
+ event
+ },
+ Event::End(Tag::Paragraph) => {
+ if added_shortcode {
+ added_shortcode = false;
+ return Event::Html(Owned("".to_owned()));
+ }
+ event
+ },
+ Event::SoftBreak => {
+ if shortcode_block.is_some() {
+ return Event::Html(Owned("".to_owned()));
+ }
+ event
+ },
+ _ => {
+ // println!("event = {:?}", event);
+ event
+ },
+ });
+ cmark::html::push_html(&mut html, parser);
}
-}
-
-pub fn markdown_to_html(content: &str, highlight_code: bool, highlight_theme: &str) -> String {
- // We try to be smart about highlighting code as it can be time-consuming
- // If the global config disables it, then we do nothing. However,
- // if we see a code block in the content, we assume that this page needs
- // to be highlighted. It could potentially have false positive if the content
- // has ``` in it but that seems kind of unlikely
- let should_highlight = if highlight_code {
- content.contains("```")
- } else {
- false
- };
-
- let mut html = String::new();
- if should_highlight {
- let parser = CodeHighlightingParser::new(Parser::new(content), highlight_theme);
- cmark::html::push_html(&mut html, parser);
- } else {
- let parser = Parser::new(content);
- cmark::html::push_html(&mut html, parser);
- };
- html
+ match error {
+ Some(e) => Err(e),
+ None => Ok(html),
+ }
}
#[cfg(test)]
mod tests {
- use super::{markdown_to_html};
+ use std::collections::HashMap;
+
+ use tera::Tera;
+
+ use config::Config;
+ use super::{markdown_to_html, parse_shortcode};
+
+ fn create_test_tera() -> Tera {
+ let mut tera = Tera::default();
+ tera.add_raw_template("shortcodes/youtube.html", "Youtube video: {{id}}").unwrap();
+ tera.add_raw_template("shortcodes/quote.html", "Quote: {{body}} - {{author}}").unwrap();
+ tera
+ }
+
+ #[test]
+ fn test_parse_simple_shortcode_one_arg() {
+ let (name, args) = parse_shortcode(r#"{{ youtube(id="w7Ft2ymGmfc") }}"#);
+ assert_eq!(name, "youtube");
+ assert_eq!(args["id"], "w7Ft2ymGmfc");
+ }
+
+ #[test]
+ fn test_parse_simple_shortcode_several_arg() {
+ let (name, args) = parse_shortcode(r#"{{ youtube(id="w7Ft2ymGmfc", autoplay=true) }}"#);
+ assert_eq!(name, "youtube");
+ assert_eq!(args["id"], "w7Ft2ymGmfc");
+ assert_eq!(args["autoplay"], "true");
+ }
+
+ #[test]
+ fn test_parse_block_shortcode_several_arg() {
+ let (name, args) = parse_shortcode(r#"{% youtube(id="w7Ft2ymGmfc", autoplay=true) %}"#);
+ assert_eq!(name, "youtube");
+ assert_eq!(args["id"], "w7Ft2ymGmfc");
+ assert_eq!(args["autoplay"], "true");
+ }
#[test]
fn test_markdown_to_html_simple() {
- let res = markdown_to_html("# hello", true, "base16-ocean-dark");
+ let res = markdown_to_html("# hello", &HashMap::new(), &Tera::default(), &Config::default()).unwrap();
assert_eq!(res, "$ gutenberg server\n
\n"
@@ -139,7 +299,7 @@ mod tests {
#[test]
fn test_markdown_to_html_code_block_no_lang() {
- let res = markdown_to_html("```\n$ gutenberg server\n$ ping\n```", true, "base16-ocean-dark");
+ let res = markdown_to_html("```\n$ gutenberg server\n$ ping\n```", &HashMap::new(), &Tera::default(), &Config::default()).unwrap();
assert_eq!(
res,
"\n$ gutenberg server\n$ ping\n" @@ -148,19 +308,46 @@ mod tests { #[test] fn test_markdown_to_html_code_block_with_lang() { - let res = markdown_to_html("```python\nlist.append(1)\n```", true, "base16-ocean-dark"); + let res = markdown_to_html("```python\nlist.append(1)\n```", &HashMap::new(), &Tera::default(), &Config::default()).unwrap(); assert_eq!( res, "
\nlist.append(1)\n" ); } + #[test] fn test_markdown_to_html_code_block_with_unknown_lang() { - let res = markdown_to_html("```yolo\nlist.append(1)\n```", true, "base16-ocean-dark"); + let res = markdown_to_html("```yolo\nlist.append(1)\n```", &HashMap::new(), &Tera::default(), &Config::default()).unwrap(); // defaults to plain text assert_eq!( res, "
\nlist.append(1)\n
"
);
}
+
+ #[test]
+ fn test_markdown_to_html_simple_shortcode() {
+ let res = markdown_to_html(r#"
+Hello
+{{ youtube(id="w7Ft2ymGmfc") }}
+ "#, &HashMap::new(), &create_test_tera(), &Config::default()).unwrap();
+ assert_eq!(res, "Hello\n
Youtube video: w7Ft2ymGmfc"); + } + + #[test] + fn test_markdown_to_html_shortcode_in_code_block() { + let res = markdown_to_html(r#"```{{ youtube(id="w7Ft2ymGmfc") }}```"#, &HashMap::new(), &create_test_tera(), &Config::default()).unwrap(); + assert_eq!(res, "{{ youtube(id="w7Ft2ymGmfc") }}
Hello\n
Quote: A quote - Keats"); + } } diff --git a/src/page.rs b/src/page.rs index c80ff8e..761f8d6 100644 --- a/src/page.rs +++ b/src/page.rs @@ -1,5 +1,6 @@ /// A page, can be a blog post or a basic page use std::cmp::Ordering; +use std::collections::HashMap; use std::fs::{read_dir}; use std::path::{Path, PathBuf}; use std::result::Result as StdResult; @@ -43,6 +44,8 @@ fn find_related_assets(path: &Path) -> VecHello world
\n"); } @@ -232,7 +247,8 @@ Hey there config.highlight_code = Some(true); let res = Page::parse(Path::new("hello.md"), &content, &config); assert!(res.is_ok()); - let page = res.unwrap(); + let mut page = res.unwrap(); + page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap(); assert!(page.content.starts_with("