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-06 08:27:03 +00:00
|
|
|
|
2016-12-06 11:53:14 +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-11 06:05:03 +00:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Page {
|
|
|
|
// .md filepath, excluding the content/ bit
|
|
|
|
pub filepath: 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-06 08:27:03 +00:00
|
|
|
// the actual content of the page
|
2016-12-11 06:05:03 +00:00
|
|
|
pub content: String,
|
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
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
// the url the page appears at, overrides the slug if set
|
|
|
|
pub url: Option<String>,
|
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-06 08:27:03 +00:00
|
|
|
// optional layout, if we want to specify which html to render for that page
|
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-06 11:53:14 +00:00
|
|
|
title: "".to_string(),
|
2016-12-11 06:05:03 +00:00
|
|
|
slug: "".to_string(),
|
2016-12-06 11:53:14 +00:00
|
|
|
content: "".to_string(),
|
|
|
|
tags: vec![],
|
|
|
|
is_draft: false,
|
|
|
|
extra: HashMap::new(),
|
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
url: None,
|
2016-12-06 11:53:14 +00:00
|
|
|
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-06 11:53:14 +00:00
|
|
|
page.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-11 06:05:03 +00:00
|
|
|
Ok(page)
|
|
|
|
}
|
2016-12-06 11:53:14 +00:00
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
pub fn from_file(path: &str) -> Result<Page> {
|
|
|
|
let mut content = String::new();
|
|
|
|
File::open(path)
|
|
|
|
.chain_err(|| format!("Failed to open '{:?}'", path))?
|
|
|
|
.read_to_string(&mut content)?;
|
2016-12-06 08:27:03 +00:00
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
Page::from_str(path, &content)
|
|
|
|
}
|
2016-12-06 08:27:03 +00:00
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
fn get_layout_name(&self) -> String {
|
|
|
|
// TODO: handle themes
|
|
|
|
match self.layout {
|
|
|
|
Some(ref l) => l.to_string(),
|
|
|
|
None => "_default/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-11 06:05:03 +00:00
|
|
|
pub fn render_html(&self, tera: &Tera, config: &Config) -> Result<String> {
|
|
|
|
let tpl = self.get_layout_name();
|
|
|
|
let mut context = Context::new();
|
|
|
|
context.add("site", config);
|
|
|
|
context.add("page", self);
|
|
|
|
// println!("{:?}", tera);
|
|
|
|
tera.render(&tpl, context).chain_err(|| "")
|
|
|
|
}
|
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"#;
|
|
|
|
let res = Page::from_str("", content);
|
|
|
|
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-06 11:53:14 +00:00
|
|
|
assert_eq!(page.content, "Hello world".to_string());
|
|
|
|
}
|
|
|
|
|
2016-12-06 08:27:03 +00:00
|
|
|
}
|