2017-03-14 12:25:45 +00:00
|
|
|
extern crate gutenberg;
|
2017-03-27 14:17:33 +00:00
|
|
|
extern crate tera;
|
2017-03-14 12:25:45 +00:00
|
|
|
extern crate tempdir;
|
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
use std::collections::HashMap;
|
2017-05-09 12:47:02 +00:00
|
|
|
use std::fs::{File, create_dir};
|
2017-03-14 12:25:45 +00:00
|
|
|
use std::path::Path;
|
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
use tempdir::TempDir;
|
|
|
|
use tera::Tera;
|
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
use gutenberg::{Page, Config};
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_parse_a_valid_page() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
slug = "hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let res = Page::parse(Path::new("post.md"), content, &Config::default());
|
|
|
|
assert!(res.is_ok());
|
2017-03-27 14:17:33 +00:00
|
|
|
let mut page = res.unwrap();
|
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-04-28 07:31:11 +00:00
|
|
|
assert_eq!(page.meta.title.unwrap(), "Hello".to_string());
|
2017-03-14 12:25:45 +00:00
|
|
|
assert_eq!(page.meta.slug.unwrap(), "hello-world".to_string());
|
|
|
|
assert_eq!(page.raw_content, "Hello world".to_string());
|
|
|
|
assert_eq!(page.content, "<p>Hello world</p>\n".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_find_one_parent_directory() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
slug = "hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let res = Page::parse(Path::new("content/posts/intro.md"), content, &Config::default());
|
|
|
|
assert!(res.is_ok());
|
2017-03-27 14:17:33 +00:00
|
|
|
let mut page = res.unwrap();
|
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
|
2017-03-14 12:25:45 +00:00
|
|
|
assert_eq!(page.components, vec!["posts".to_string()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_find_multiple_parent_directories() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
slug = "hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &Config::default());
|
|
|
|
assert!(res.is_ok());
|
2017-03-27 14:17:33 +00:00
|
|
|
let mut page = res.unwrap();
|
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
|
2017-03-14 12:25:45 +00:00
|
|
|
assert_eq!(page.components, vec!["posts".to_string(), "intro".to_string()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_make_url_from_sections_and_slug() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
slug = "hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let mut conf = Config::default();
|
|
|
|
conf.base_url = "http://hello.com/".to_string();
|
|
|
|
let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &conf);
|
|
|
|
assert!(res.is_ok());
|
2017-03-27 14:17:33 +00:00
|
|
|
let mut page = res.unwrap();
|
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
|
2017-03-30 08:17:12 +00:00
|
|
|
assert_eq!(page.path, "posts/intro/hello-world");
|
2017-03-14 12:25:45 +00:00
|
|
|
assert_eq!(page.permalink, "http://hello.com/posts/intro/hello-world");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_make_permalink_with_non_trailing_slash_base_url() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
slug = "hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let mut conf = Config::default();
|
|
|
|
conf.base_url = "http://hello.com".to_string();
|
|
|
|
let res = Page::parse(Path::new("content/posts/intro/hello-world.md"), content, &conf);
|
|
|
|
assert!(res.is_ok());
|
2017-03-27 14:17:33 +00:00
|
|
|
let mut page = res.unwrap();
|
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
|
2017-03-30 08:17:12 +00:00
|
|
|
assert_eq!(page.path, "posts/intro/hello-world");
|
2017-03-14 12:25:45 +00:00
|
|
|
assert_eq!(page.permalink, format!("{}{}", conf.base_url, "/posts/intro/hello-world"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_make_url_from_slug_only() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
slug = "hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let res = Page::parse(Path::new("start.md"), content, &Config::default());
|
|
|
|
assert!(res.is_ok());
|
2017-03-27 14:17:33 +00:00
|
|
|
let mut page = res.unwrap();
|
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
|
2017-03-30 08:17:12 +00:00
|
|
|
assert_eq!(page.path, "hello-world");
|
2017-03-14 12:25:45 +00:00
|
|
|
assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "hello-world"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_errors_on_invalid_front_matter_format() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
slug = "hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let res = Page::parse(Path::new("start.md"), content, &Config::default());
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_make_slug_from_non_slug_filename() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let res = Page::parse(Path::new("file with space.md"), content, &Config::default());
|
|
|
|
assert!(res.is_ok());
|
2017-03-27 14:17:33 +00:00
|
|
|
let mut page = res.unwrap();
|
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
|
2017-03-14 12:25:45 +00:00
|
|
|
assert_eq!(page.slug, "file-with-space");
|
|
|
|
assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "file-with-space"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_trim_slug_if_needed() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let res = Page::parse(Path::new(" file with space.md"), content, &Config::default());
|
|
|
|
assert!(res.is_ok());
|
2017-03-27 14:17:33 +00:00
|
|
|
let mut page = res.unwrap();
|
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
|
2017-03-14 12:25:45 +00:00
|
|
|
assert_eq!(page.slug, "file-with-space");
|
|
|
|
assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "file-with-space"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_automatic_summary_is_empty_string() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
+++
|
|
|
|
Hello world"#.to_string();
|
|
|
|
let res = Page::parse(Path::new("hello.md"), &content, &Config::default());
|
|
|
|
assert!(res.is_ok());
|
2017-03-27 14:17:33 +00:00
|
|
|
let mut page = res.unwrap();
|
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
|
2017-04-20 02:48:14 +00:00
|
|
|
assert_eq!(page.summary, None);
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_specify_summary() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
+++
|
|
|
|
Hello world
|
|
|
|
<!-- more -->
|
|
|
|
"#.to_string();
|
|
|
|
let res = Page::parse(Path::new("hello.md"), &content, &Config::default());
|
|
|
|
assert!(res.is_ok());
|
2017-03-27 14:17:33 +00:00
|
|
|
let mut page = res.unwrap();
|
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
|
2017-04-20 02:48:14 +00:00
|
|
|
assert_eq!(page.summary, Some("<p>Hello world</p>\n".to_string()));
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_auto_detect_when_highlighting_needed() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
title = "Hello"
|
|
|
|
description = "hey there"
|
|
|
|
+++
|
|
|
|
```
|
|
|
|
Hey there
|
|
|
|
```
|
|
|
|
"#.to_string();
|
|
|
|
let mut config = Config::default();
|
|
|
|
config.highlight_code = Some(true);
|
|
|
|
let res = Page::parse(Path::new("hello.md"), &content, &config);
|
|
|
|
assert!(res.is_ok());
|
2017-03-27 14:17:33 +00:00
|
|
|
let mut page = res.unwrap();
|
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
|
2017-03-14 12:25:45 +00:00
|
|
|
assert!(page.content.starts_with("<pre"));
|
|
|
|
}
|
|
|
|
|
2017-05-09 12:47:02 +00:00
|
|
|
#[test]
|
|
|
|
fn test_page_with_assets_gets_right_parent_path() {
|
|
|
|
let tmp_dir = TempDir::new("example").expect("create temp dir");
|
|
|
|
let path = tmp_dir.path();
|
|
|
|
create_dir(&path.join("content")).expect("create content temp dir");
|
|
|
|
create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
|
|
|
|
let nested_path = path.join("content").join("posts").join("assets");
|
|
|
|
create_dir(&nested_path).expect("create nested temp dir");
|
|
|
|
File::create(nested_path.join("index.md")).unwrap();
|
|
|
|
File::create(nested_path.join("example.js")).unwrap();
|
|
|
|
File::create(nested_path.join("graph.jpg")).unwrap();
|
|
|
|
File::create(nested_path.join("fail.png")).unwrap();
|
|
|
|
|
|
|
|
let res = Page::parse(
|
2017-05-11 06:45:19 +00:00
|
|
|
nested_path.join("index.md").as_path(),
|
2017-05-09 12:47:02 +00:00
|
|
|
"+++\nurl=\"hey\"+++\n",
|
|
|
|
&Config::default()
|
|
|
|
);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
assert_eq!(page.parent_path, path.join("content").join("posts"));
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
#[test]
|
|
|
|
fn test_file_not_named_index_with_assets() {
|
|
|
|
let tmp_dir = TempDir::new("example").expect("create temp dir");
|
|
|
|
File::create(tmp_dir.path().join("something.md")).unwrap();
|
|
|
|
File::create(tmp_dir.path().join("example.js")).unwrap();
|
|
|
|
File::create(tmp_dir.path().join("graph.jpg")).unwrap();
|
|
|
|
File::create(tmp_dir.path().join("fail.png")).unwrap();
|
|
|
|
|
|
|
|
let page = Page::from_file(tmp_dir.path().join("something.md"), &Config::default());
|
|
|
|
assert!(page.is_err());
|
|
|
|
}
|