2016-12-06 08:27:03 +00:00
|
|
|
/// A page, can be a blog post or a basic page
|
2016-12-11 06:05:03 +00:00
|
|
|
use std::collections::HashMap;
|
2016-12-06 11:53:14 +00:00
|
|
|
use std::default::Default;
|
2016-12-11 06:05:03 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
2016-12-13 06:22:24 +00:00
|
|
|
use std::path::Path;
|
2016-12-06 08:27:03 +00:00
|
|
|
|
2016-12-13 06:22:24 +00:00
|
|
|
|
|
|
|
use pulldown_cmark as cmark;
|
2016-12-06 08:27:03 +00:00
|
|
|
use regex::Regex;
|
2016-12-11 06:05:03 +00:00
|
|
|
use tera::{Tera, Value, Context};
|
2016-12-06 08:27:03 +00:00
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
use errors::{Result, ResultExt};
|
2016-12-06 12:48:23 +00:00
|
|
|
use config::Config;
|
2016-12-11 06:05:03 +00:00
|
|
|
use front_matter::parse_front_matter;
|
2016-12-06 08:27:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref DELIM_RE: Regex = Regex::new(r"\+\+\+\s*\r?\n").unwrap();
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:53:14 +00:00
|
|
|
|
2016-12-13 09:05:59 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2016-12-11 06:05:03 +00:00
|
|
|
pub struct Page {
|
|
|
|
// .md filepath, excluding the content/ bit
|
2016-12-13 09:05:59 +00:00
|
|
|
#[serde(skip_serializing)]
|
2016-12-11 06:05:03 +00:00
|
|
|
pub filepath: String,
|
2016-12-13 06:22:24 +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,
|
|
|
|
// 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>,
|
2016-12-13 09:05:59 +00:00
|
|
|
// the actual content of the page, in markdown
|
|
|
|
#[serde(skip_serializing)]
|
|
|
|
pub raw_content: String,
|
2016-12-06 08:27:03 +00:00
|
|
|
|
|
|
|
// <title> of the page
|
2016-12-11 06:05:03 +00:00
|
|
|
pub title: String,
|
|
|
|
// The page slug
|
|
|
|
pub slug: String,
|
2016-12-13 10:14:49 +00:00
|
|
|
// the url the page appears at, overrides the slug if set in the frontmatter
|
|
|
|
// otherwise is set after parsing front matter and sections
|
|
|
|
pub url: String,
|
2016-12-13 06:22:24 +00:00
|
|
|
// the HTML rendered of the page
|
2016-12-11 06:05:03 +00:00
|
|
|
pub content: String,
|
2016-12-13 06:22:24 +00:00
|
|
|
|
2016-12-06 08:27:03 +00:00
|
|
|
// tags, not to be confused with categories
|
2016-12-11 06:05:03 +00:00
|
|
|
pub tags: Vec<String>,
|
2016-12-06 11:53:14 +00:00
|
|
|
// whether this page should be public or not
|
2016-12-11 06:05:03 +00:00
|
|
|
pub is_draft: bool,
|
2016-12-06 08:27:03 +00:00
|
|
|
// any extra parameter present in the front matter
|
|
|
|
// it will be passed to the template context
|
2016-12-11 06:05:03 +00:00
|
|
|
pub extra: HashMap<String, Value>,
|
2016-12-06 08:27:03 +00:00
|
|
|
|
|
|
|
// only one category allowed
|
2016-12-11 06:05:03 +00:00
|
|
|
pub category: Option<String>,
|
2016-12-06 11:53:14 +00:00
|
|
|
// optional date if we want to order pages (ie blog post)
|
2016-12-11 06:05:03 +00:00
|
|
|
pub date: Option<String>,
|
2016-12-13 06:22:24 +00:00
|
|
|
// optional layout, if we want to specify which tpl to render for that page
|
2016-12-13 09:05:59 +00:00
|
|
|
#[serde(skip_serializing)]
|
2016-12-11 06:05:03 +00:00
|
|
|
pub layout: Option<String>,
|
2016-12-06 08:27:03 +00:00
|
|
|
// description that appears when linked, e.g. on twitter
|
2016-12-11 06:05:03 +00:00
|
|
|
pub description: Option<String>,
|
2016-12-06 08:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-06 11:53:14 +00:00
|
|
|
impl Default for Page {
|
|
|
|
fn default() -> Page {
|
|
|
|
Page {
|
2016-12-11 06:05:03 +00:00
|
|
|
filepath: "".to_string(),
|
2016-12-13 06:22:24 +00:00
|
|
|
filename: "".to_string(),
|
|
|
|
sections: vec![],
|
2016-12-11 06:05:03 +00:00
|
|
|
|
2016-12-06 11:53:14 +00:00
|
|
|
title: "".to_string(),
|
2016-12-11 06:05:03 +00:00
|
|
|
slug: "".to_string(),
|
2016-12-13 10:14:49 +00:00
|
|
|
url: "".to_string(),
|
2016-12-13 06:22:24 +00:00
|
|
|
raw_content: "".to_string(),
|
2016-12-06 11:53:14 +00:00
|
|
|
content: "".to_string(),
|
|
|
|
tags: vec![],
|
|
|
|
is_draft: false,
|
|
|
|
extra: HashMap::new(),
|
|
|
|
|
|
|
|
category: None,
|
|
|
|
date: None,
|
|
|
|
layout: None,
|
|
|
|
description: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-06 08:27:03 +00:00
|
|
|
impl Page {
|
|
|
|
// Parse a page given the content of the .md file
|
|
|
|
// Files without front matter or with invalid front matter are considered
|
|
|
|
// erroneous
|
2016-12-11 06:05:03 +00:00
|
|
|
pub fn from_str(filepath: &str, content: &str) -> Result<Page> {
|
2016-12-06 08:27:03 +00:00
|
|
|
// 1. separate front matter from content
|
|
|
|
if !DELIM_RE.is_match(content) {
|
2016-12-11 06:05:03 +00:00
|
|
|
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
|
|
|
|
let splits: Vec<&str> = DELIM_RE.splitn(content, 2).collect();
|
|
|
|
let front_matter = splits[0];
|
|
|
|
let content = splits[1];
|
|
|
|
|
2016-12-06 11:53:14 +00:00
|
|
|
// 2. create our page, parse front matter and assign all of that
|
|
|
|
let mut page = Page::default();
|
2016-12-11 06:05:03 +00:00
|
|
|
page.filepath = filepath.to_string();
|
2016-12-13 06:22:24 +00:00
|
|
|
page.raw_content = content.to_string();
|
2016-12-11 06:05:03 +00:00
|
|
|
parse_front_matter(front_matter, &mut page)
|
|
|
|
.chain_err(|| format!("Error when parsing front matter of file `{}`", filepath))?;
|
2016-12-06 11:53:14 +00:00
|
|
|
|
2016-12-13 06:22:24 +00:00
|
|
|
page.content = {
|
|
|
|
let mut html = String::new();
|
|
|
|
let parser = cmark::Parser::new(&page.raw_content);
|
|
|
|
cmark::html::push_html(&mut html, parser);
|
|
|
|
html
|
|
|
|
};
|
|
|
|
|
2016-12-13 10:14:49 +00:00
|
|
|
// next find sections
|
|
|
|
// Pages with custom urls exists outside of sections
|
|
|
|
if page.url == "" {
|
|
|
|
let path = Path::new(filepath);
|
|
|
|
page.filename = path.file_stem().expect("Couldn't get file stem").to_string_lossy().to_string();
|
|
|
|
|
|
|
|
// find out if we have sections
|
|
|
|
for section in path.parent().unwrap().components() {
|
|
|
|
page.sections.push(section.as_ref().to_string_lossy().to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
// now the url
|
|
|
|
// it's either set in the front matter OR we get it from a combination of sections + slug
|
|
|
|
if page.sections.len() > 0 {
|
|
|
|
page.url = format!("/{}/{}", page.sections.join("/"), page.slug);
|
|
|
|
} else {
|
|
|
|
page.url = format!("/{}", page.slug);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
Ok(page)
|
|
|
|
}
|
2016-12-06 11:53:14 +00:00
|
|
|
|
2016-12-13 06:22:24 +00:00
|
|
|
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Page> {
|
|
|
|
let path = path.as_ref();
|
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
let mut content = String::new();
|
|
|
|
File::open(path)
|
2016-12-13 06:22:24 +00:00
|
|
|
.chain_err(|| format!("Failed to open '{:?}'", path.display()))?
|
2016-12-11 06:05:03 +00:00
|
|
|
.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?
|
|
|
|
Page::from_str(&path.strip_prefix("content").unwrap().to_string_lossy(), &content)
|
2016-12-11 06:05:03 +00:00
|
|
|
}
|
2016-12-06 08:27:03 +00:00
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
fn get_layout_name(&self) -> String {
|
|
|
|
match self.layout {
|
|
|
|
Some(ref l) => l.to_string(),
|
2016-12-13 06:22:24 +00:00
|
|
|
None => "single.html".to_string()
|
2016-12-06 11:53:14 +00:00
|
|
|
}
|
2016-12-06 08:27:03 +00:00
|
|
|
}
|
2016-12-06 12:48:23 +00:00
|
|
|
|
2016-12-13 06:22:24 +00:00
|
|
|
pub fn render_html(&mut self, tera: &Tera, config: &Config) -> Result<String> {
|
2016-12-11 06:05:03 +00:00
|
|
|
let tpl = self.get_layout_name();
|
|
|
|
let mut context = Context::new();
|
|
|
|
context.add("site", config);
|
|
|
|
context.add("page", self);
|
2016-12-13 06:22:24 +00:00
|
|
|
|
|
|
|
tera.render(&tpl, context)
|
|
|
|
.chain_err(|| "Error while rendering template")
|
2016-12-11 06:05:03 +00:00
|
|
|
}
|
2016-12-06 08:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-13 09:05:59 +00:00
|
|
|
// Order pages by date, no-op for now
|
|
|
|
pub fn order_pages(pages: Vec<Page>) -> Vec<Page> {
|
|
|
|
pages
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-06 08:27:03 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-12-06 11:53:14 +00:00
|
|
|
use super::{Page};
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_parse_a_valid_page() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
2016-12-11 06:05:03 +00:00
|
|
|
slug = "hello-world"
|
2016-12-06 11:53:14 +00:00
|
|
|
+++
|
|
|
|
Hello world"#;
|
2016-12-13 06:22:24 +00:00
|
|
|
let res = Page::from_str("post.md", content);
|
2016-12-06 11:53:14 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
2016-12-06 08:27:03 +00:00
|
|
|
|
2016-12-06 11:53:14 +00:00
|
|
|
assert_eq!(page.title, "Hello".to_string());
|
2016-12-11 06:05:03 +00:00
|
|
|
assert_eq!(page.slug, "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#"
|
|
|
|
title = "Hello"
|
|
|
|
slug = "hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let res = Page::from_str("posts/intro.md", content);
|
|
|
|
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#"
|
|
|
|
title = "Hello"
|
|
|
|
slug = "hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let res = Page::from_str("posts/intro/start.md", content);
|
|
|
|
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#"
|
|
|
|
title = "Hello"
|
|
|
|
slug = "hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let res = Page::from_str("posts/intro/start.md", content);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
assert_eq!(page.url, "/posts/intro/hello-world");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_make_url_from_sections_and_slug_root() {
|
|
|
|
let content = r#"
|
|
|
|
title = "Hello"
|
|
|
|
slug = "hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let res = Page::from_str("start.md", content);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
assert_eq!(page.url, "/hello-world");
|
|
|
|
}
|
2016-12-06 08:27:03 +00:00
|
|
|
}
|