zola/src/page.rs

482 lines
15 KiB
Rust
Raw Normal View History

2016-12-06 08:27:03 +00:00
/// A page, can be a blog post or a basic page
2017-03-06 11:58:31 +00:00
use std::cmp::Ordering;
use std::fs::File;
use std::io::prelude::*;
2016-12-13 06:22:24 +00:00
use std::path::Path;
2017-02-23 08:34:57 +00:00
use std::result::Result as StdResult;
2016-12-06 08:27:03 +00:00
2016-12-13 06:22:24 +00:00
2016-12-06 08:27:03 +00:00
use regex::Regex;
2017-02-23 08:34:57 +00:00
use tera::{Tera, Context};
use serde::ser::{SerializeStruct, self};
use slug::slugify;
2016-12-06 08:27:03 +00:00
use errors::{Result, ResultExt};
2016-12-06 12:48:23 +00:00
use config::Config;
2017-02-23 08:34:57 +00:00
use front_matter::{FrontMatter};
2017-03-07 12:34:31 +00:00
use markdown::markdown_to_html;
2016-12-06 08:27:03 +00:00
lazy_static! {
2017-02-23 08:34:57 +00:00
static ref PAGE_RE: Regex = Regex::new(r"^\n?\+\+\+\n((?s).*(?-s))\+\+\+\n((?s).*(?-s))$").unwrap();
2017-03-07 03:42:14 +00:00
}
2016-12-06 11:53:14 +00:00
2017-02-23 08:34:57 +00:00
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct Page {
2017-02-23 08:34:57 +00:00
/// .md filepath, excluding the content/ bit
2016-12-13 09:05:59 +00:00
#[serde(skip_serializing)]
pub filepath: String,
2017-02-23 08:34:57 +00:00
/// The name of the .md file
2016-12-13 09:05:59 +00:00
#[serde(skip_serializing)]
2016-12-13 06:22:24 +00:00
pub filename: String,
2017-02-23 08:34:57 +00:00
/// The directories above our .md file are called sections
/// for example a file at content/kb/solutions/blabla.md will have 2 sections:
/// `kb` and `solutions`
2016-12-13 09:05:59 +00:00
#[serde(skip_serializing)]
2016-12-13 06:22:24 +00:00
pub sections: Vec<String>,
2017-02-23 08:34:57 +00:00
/// The actual content of the page, in markdown
2016-12-13 09:05:59 +00:00
#[serde(skip_serializing)]
pub raw_content: String,
2017-02-23 08:34:57 +00:00
/// The HTML rendered of the page
pub content: String,
2017-02-23 08:34:57 +00:00
/// The front matter meta-data
pub meta: FrontMatter,
2017-03-06 14:45:57 +00:00
/// The slug of that page.
/// First tries to find the slug in the meta and defaults to filename otherwise
pub slug: String,
/// The relative URL of the page
pub url: String,
/// The full URL for that page
pub permalink: String,
2017-03-07 03:42:14 +00:00
/// The summary for the article, defaults to empty string
/// When <!-- more --> is found in the text, will take the content up to that part
/// as summary
pub summary: String,
2017-03-06 14:45:57 +00:00
2017-02-23 08:34:57 +00:00
/// The previous page, by date
pub previous: Option<Box<Page>>,
/// The next page, by date
pub next: Option<Box<Page>>,
2016-12-06 08:27:03 +00:00
}
2017-02-23 08:34:57 +00:00
impl Page {
pub fn new(meta: FrontMatter) -> Page {
2016-12-06 11:53:14 +00:00
Page {
filepath: "".to_string(),
2016-12-13 06:22:24 +00:00
filename: "".to_string(),
sections: vec![],
raw_content: "".to_string(),
2016-12-06 11:53:14 +00:00
content: "".to_string(),
2017-03-06 14:45:57 +00:00
slug: "".to_string(),
url: "".to_string(),
permalink: "".to_string(),
2017-03-07 03:42:14 +00:00
summary: "".to_string(),
2017-02-23 08:34:57 +00:00
meta: meta,
previous: None,
next: None,
2016-12-06 11:53:14 +00:00
}
}
2017-03-06 13:45:33 +00:00
// Get word count and estimated reading time
pub fn get_reading_analytics(&self) -> (usize, usize) {
// Only works for latin language but good enough for a start
let word_count: usize = self.raw_content.split_whitespace().count();
// https://help.medium.com/hc/en-us/articles/214991667-Read-time
// 275 seems a bit too high though
(word_count, (word_count / 200))
}
2016-12-06 08:27:03 +00:00
// Parse a page given the content of the .md file
// Files without front matter or with invalid front matter are considered
// erroneous
2017-03-06 14:45:57 +00:00
pub fn parse(filepath: &str, content: &str, config: &Config) -> Result<Page> {
2016-12-06 08:27:03 +00:00
// 1. separate front matter from content
2017-02-23 08:34:57 +00:00
if !PAGE_RE.is_match(content) {
bail!("Couldn't find front matter in `{}`. Did you forget to add `+++`?", filepath);
2016-12-06 08:27:03 +00:00
}
// 2. extract the front matter and the content
2017-02-23 08:34:57 +00:00
let caps = PAGE_RE.captures(content).unwrap();
// caps[0] is the full match
let front_matter = &caps[1];
let content = &caps[2];
2016-12-06 08:27:03 +00:00
2017-02-23 08:34:57 +00:00
// 3. create our page, parse front matter and assign all of that
2017-03-10 13:19:36 +00:00
let meta = FrontMatter::parse(front_matter)
.chain_err(|| format!("Error when parsing front matter of file `{}`", filepath))?;
2016-12-06 11:53:14 +00:00
2017-02-23 08:34:57 +00:00
let mut page = Page::new(meta);
page.filepath = filepath.to_string();
page.raw_content = content.to_string();
2017-03-07 03:42:14 +00:00
// 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
2017-03-10 13:19:36 +00:00
let should_highlight = if config.highlight_code.unwrap() {
page.raw_content.contains("```")
} else {
false
};
page.content = markdown_to_html(&page.raw_content, should_highlight);
2017-03-07 03:42:14 +00:00
if page.raw_content.contains("<!-- more -->") {
page.summary = {
2017-03-10 13:19:36 +00:00
let summary = page.raw_content.splitn(2, "<!-- more -->").collect::<Vec<&str>>()[0];
markdown_to_html(summary, should_highlight)
2017-03-07 03:42:14 +00:00
}
}
2017-03-06 14:45:57 +00:00
let path = Path::new(filepath);
page.filename = path.file_stem().expect("Couldn't get filename").to_string_lossy().to_string();
page.slug = {
if let Some(ref slug) = page.meta.slug {
2017-03-07 07:43:27 +00:00
slug.trim().to_string()
2017-03-06 14:45:57 +00:00
} else {
slugify(page.filename.clone())
}
};
2016-12-13 06:22:24 +00:00
2017-02-23 08:34:57 +00:00
// 4. Find sections
2016-12-13 10:14:49 +00:00
// Pages with custom urls exists outside of sections
2017-03-06 14:45:57 +00:00
if let Some(ref u) = page.meta.url {
2017-03-07 07:43:27 +00:00
page.url = u.trim().to_string();
2017-03-06 14:45:57 +00:00
} else {
2016-12-13 10:14:49 +00:00
// find out if we have sections
for section in path.parent().unwrap().components() {
page.sections.push(section.as_ref().to_string_lossy().to_string());
}
2017-03-06 14:45:57 +00:00
if !page.sections.is_empty() {
page.url = format!("{}/{}", page.sections.join("/"), page.slug);
} else {
2017-03-10 13:19:36 +00:00
page.url = page.slug.clone();
2017-03-06 14:45:57 +00:00
}
2016-12-13 10:14:49 +00:00
}
2017-03-10 13:19:36 +00:00
page.permalink = if config.base_url.ends_with('/') {
2017-03-06 14:45:57 +00:00
format!("{}{}", config.base_url, page.url)
} else {
format!("{}/{}", config.base_url, page.url)
};
2016-12-13 10:14:49 +00:00
Ok(page)
}
2016-12-06 11:53:14 +00:00
2017-03-06 14:45:57 +00:00
pub fn from_file<P: AsRef<Path>>(path: P, config: &Config) -> Result<Page> {
2016-12-13 06:22:24 +00:00
let path = path.as_ref();
let mut content = String::new();
File::open(path)
2016-12-13 06:22:24 +00:00
.chain_err(|| format!("Failed to open '{:?}'", path.display()))?
.read_to_string(&mut content)?;
2016-12-06 08:27:03 +00:00
2016-12-13 06:22:24 +00:00
// Remove the content string from name
// Maybe get a path as an arg instead and use strip_prefix?
2017-03-06 14:45:57 +00:00
Page::parse(&path.strip_prefix("content").unwrap().to_string_lossy(), &content, config)
}
2016-12-06 08:27:03 +00:00
2017-03-06 13:45:33 +00:00
/// Renders the page using the default layout, unless specified in front-matter
pub fn render_html(&self, tera: &Tera, config: &Config) -> Result<String> {
let tpl_name = match self.meta.template {
Some(ref l) => l.to_string(),
None => "page.html".to_string()
};
// TODO: create a helper to create context to ensure all contexts
// have the same names
let mut context = Context::new();
context.add("config", config);
context.add("page", self);
2016-12-13 06:22:24 +00:00
tera.render(&tpl_name, &context)
.chain_err(|| format!("Failed to render page '{}'", self.filename))
}
2016-12-06 08:27:03 +00:00
}
2017-02-23 08:34:57 +00:00
impl ser::Serialize for Page {
fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> where S: ser::Serializer {
2017-03-06 14:45:57 +00:00
let mut state = serializer.serialize_struct("page", 13)?;
2017-02-23 08:34:57 +00:00
state.serialize_field("content", &self.content)?;
state.serialize_field("title", &self.meta.title)?;
state.serialize_field("description", &self.meta.description)?;
state.serialize_field("date", &self.meta.date)?;
2017-03-06 14:45:57 +00:00
state.serialize_field("slug", &self.slug)?;
state.serialize_field("url", &format!("/{}", self.url))?;
state.serialize_field("permalink", &self.permalink)?;
2017-02-23 08:34:57 +00:00
state.serialize_field("tags", &self.meta.tags)?;
state.serialize_field("draft", &self.meta.draft)?;
state.serialize_field("category", &self.meta.category)?;
state.serialize_field("extra", &self.meta.extra)?;
2017-03-06 13:45:33 +00:00
let (word_count, reading_time) = self.get_reading_analytics();
state.serialize_field("word_count", &word_count)?;
state.serialize_field("reading_time", &reading_time)?;
2017-02-23 08:34:57 +00:00
state.end()
}
}
2016-12-06 08:27:03 +00:00
2017-03-06 11:58:31 +00:00
impl PartialOrd for Page {
fn partial_cmp(&self, other: &Page) -> Option<Ordering> {
if self.meta.date.is_none() {
return Some(Ordering::Less);
}
if other.meta.date.is_none() {
return Some(Ordering::Greater);
}
let this_date = self.meta.parse_date().unwrap();
let other_date = other.meta.parse_date().unwrap();
if this_date > other_date {
return Some(Ordering::Less);
}
if this_date < other_date {
return Some(Ordering::Greater);
}
Some(Ordering::Equal)
}
2016-12-13 09:05:59 +00:00
}
2016-12-06 08:27:03 +00:00
#[cfg(test)]
mod tests {
2016-12-06 11:53:14 +00:00
use super::{Page};
2017-03-06 14:45:57 +00:00
use config::Config;
2016-12-06 11:53:14 +00:00
#[test]
fn test_can_parse_a_valid_page() {
let content = r#"
2017-02-23 08:34:57 +00:00
+++
2016-12-06 11:53:14 +00:00
title = "Hello"
2017-02-23 08:34:57 +00:00
description = "hey there"
slug = "hello-world"
2016-12-06 11:53:14 +00:00
+++
Hello world"#;
2017-03-06 14:45:57 +00:00
let res = Page::parse("post.md", content, &Config::default());
2016-12-06 11:53:14 +00:00
assert!(res.is_ok());
let page = res.unwrap();
2016-12-06 08:27:03 +00:00
2017-02-23 08:34:57 +00:00
assert_eq!(page.meta.title, "Hello".to_string());
assert_eq!(page.meta.slug.unwrap(), "hello-world".to_string());
2016-12-13 06:22:24 +00:00
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#"
2017-02-23 08:34:57 +00:00
+++
2016-12-13 06:22:24 +00:00
title = "Hello"
2017-02-23 08:34:57 +00:00
description = "hey there"
2016-12-13 06:22:24 +00:00
slug = "hello-world"
+++
Hello world"#;
2017-03-06 14:45:57 +00:00
let res = Page::parse("posts/intro.md", content, &Config::default());
2016-12-13 06:22:24 +00:00
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.sections, vec!["posts".to_string()]);
}
#[test]
2016-12-13 10:14:49 +00:00
fn test_can_find_multiple_parent_directories() {
2016-12-13 06:22:24 +00:00
let content = r#"
2017-02-23 08:34:57 +00:00
+++
2016-12-13 06:22:24 +00:00
title = "Hello"
2017-02-23 08:34:57 +00:00
description = "hey there"
2016-12-13 06:22:24 +00:00
slug = "hello-world"
+++
Hello world"#;
2017-03-06 14:45:57 +00:00
let res = Page::parse("posts/intro/start.md", content, &Config::default());
2016-12-13 06:22:24 +00:00
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.sections, vec!["posts".to_string(), "intro".to_string()]);
2016-12-06 11:53:14 +00:00
}
2016-12-13 10:14:49 +00:00
#[test]
fn test_can_make_url_from_sections_and_slug() {
let content = r#"
2017-02-23 08:34:57 +00:00
+++
2016-12-13 10:14:49 +00:00
title = "Hello"
2017-02-23 08:34:57 +00:00
description = "hey there"
2016-12-13 10:14:49 +00:00
slug = "hello-world"
+++
Hello world"#;
2017-03-06 14:45:57 +00:00
let mut conf = Config::default();
conf.base_url = "http://hello.com/".to_string();
let res = Page::parse("posts/intro/start.md", content, &conf);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.url, "posts/intro/hello-world");
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("posts/intro/start.md", content, &conf);
2016-12-13 10:14:49 +00:00
assert!(res.is_ok());
let page = res.unwrap();
2017-03-06 14:45:57 +00:00
assert_eq!(page.url, "posts/intro/hello-world");
assert_eq!(page.permalink, format!("{}{}", conf.base_url, "/posts/intro/hello-world"));
2016-12-13 10:14:49 +00:00
}
#[test]
2017-03-06 14:45:57 +00:00
fn test_can_make_url_from_slug_only() {
2016-12-13 10:14:49 +00:00
let content = r#"
2017-02-23 08:34:57 +00:00
+++
2016-12-13 10:14:49 +00:00
title = "Hello"
2017-02-23 08:34:57 +00:00
description = "hey there"
2016-12-13 10:14:49 +00:00
slug = "hello-world"
+++
Hello world"#;
2017-03-06 14:45:57 +00:00
let res = Page::parse("start.md", content, &Config::default());
2016-12-13 10:14:49 +00:00
assert!(res.is_ok());
let page = res.unwrap();
2017-03-06 14:45:57 +00:00
assert_eq!(page.url, "hello-world");
assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "hello-world"));
2017-02-23 08:34:57 +00:00
}
#[test]
fn test_errors_on_invalid_front_matter_format() {
let content = r#"
title = "Hello"
description = "hey there"
slug = "hello-world"
+++
Hello world"#;
2017-03-06 14:45:57 +00:00
let res = Page::parse("start.md", content, &Config::default());
2017-02-23 08:34:57 +00:00
assert!(res.is_err());
2016-12-13 10:14:49 +00:00
}
#[test]
fn test_can_make_slug_from_non_slug_filename() {
let content = r#"
+++
title = "Hello"
description = "hey there"
+++
Hello world"#;
2017-03-06 14:45:57 +00:00
let res = Page::parse("file with space.md", content, &Config::default());
assert!(res.is_ok());
let page = res.unwrap();
2017-03-06 14:45:57 +00:00
assert_eq!(page.slug, "file-with-space");
assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "file-with-space"));
}
2017-03-06 13:45:33 +00:00
2017-03-07 07:43:27 +00:00
#[test]
fn test_trim_slug_if_needed() {
let content = r#"
+++
title = "Hello"
description = "hey there"
+++
Hello world"#;
let res = Page::parse(" file with space.md", content, &Config::default());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.slug, "file-with-space");
assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "file-with-space"));
}
2017-03-06 13:45:33 +00:00
#[test]
fn test_reading_analytics_short() {
let content = r#"
+++
title = "Hello"
description = "hey there"
+++
Hello world"#;
2017-03-06 14:45:57 +00:00
let res = Page::parse("file with space.md", content, &Config::default());
2017-03-06 13:45:33 +00:00
assert!(res.is_ok());
let page = res.unwrap();
let (word_count, reading_time) = page.get_reading_analytics();
assert_eq!(word_count, 2);
assert_eq!(reading_time, 0);
}
#[test]
fn test_reading_analytics_long() {
let mut content = r#"
+++
title = "Hello"
description = "hey there"
+++
Hello world"#.to_string();
for _ in 0..1000 {
content.push_str(" Hello world");
}
2017-03-06 14:45:57 +00:00
let res = Page::parse("hello.md", &content, &Config::default());
2017-03-06 13:45:33 +00:00
assert!(res.is_ok());
let page = res.unwrap();
let (word_count, reading_time) = page.get_reading_analytics();
assert_eq!(word_count, 2002);
assert_eq!(reading_time, 10);
}
2017-03-07 03:42:14 +00:00
#[test]
fn test_automatic_summary_is_empty_string() {
let content = r#"
+++
title = "Hello"
description = "hey there"
+++
Hello world"#.to_string();
let res = Page::parse("hello.md", &content, &Config::default());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.summary, "");
}
#[test]
fn test_can_specify_summary() {
let content = r#"
+++
title = "Hello"
description = "hey there"
+++
Hello world
<!-- more -->
"#.to_string();
let res = Page::parse("hello.md", &content, &Config::default());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.summary, "<p>Hello world</p>\n");
}
#[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("hello.md", &content, &config);
assert!(res.is_ok());
let page = res.unwrap();
assert!(page.content.starts_with("<pre"));
}
2016-12-06 08:27:03 +00:00
}