zola/src/content/page.rs

327 lines
11 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-27 14:17:33 +00:00
use std::collections::HashMap;
2017-03-12 03:54:57 +00:00
use std::path::{Path, PathBuf};
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
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-05-15 03:23:19 +00:00
use front_matter::{PageFrontMatter, split_page_content};
2017-03-07 12:34:31 +00:00
use markdown::markdown_to_html;
2017-05-16 04:37:00 +00:00
use fs::{read_file};
use content::utils::{find_related_assets, get_reading_analytics};
use content::file_info::FileInfo;
2016-12-06 08:27:03 +00:00
2016-12-06 11:53:14 +00:00
#[derive(Clone, Debug, PartialEq)]
pub struct Page {
/// All info about the actual file
pub file: FileInfo,
/// The front matter meta-data
pub meta: PageFrontMatter,
2017-02-23 08:34:57 +00:00
/// The actual content of the page, in markdown
2016-12-13 09:05:59 +00:00
pub raw_content: String,
2017-03-12 03:54:57 +00:00
/// All the non-md files we found next to the .md file
pub assets: Vec<PathBuf>,
2017-02-23 08:34:57 +00:00
/// The HTML rendered of the page
pub content: String,
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 URL path of the page
pub path: String,
2017-03-06 14:45:57 +00:00
/// The full URL for that page
pub permalink: String,
/// The summary for the article, defaults to None
2017-03-07 03:42:14 +00:00
/// When <!-- more --> is found in the text, will take the content up to that part
/// as summary
pub summary: Option<String>,
/// The previous page, by whatever sorting is used for the index/section
2017-02-23 08:34:57 +00:00
pub previous: Option<Box<Page>>,
/// The next page, by whatever sorting is used for the index/section
2017-02-23 08:34:57 +00:00
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<P: AsRef<Path>>(file_path: P, meta: PageFrontMatter) -> Page {
let file_path = file_path.as_ref();
2016-12-06 11:53:14 +00:00
Page {
file: FileInfo::new_page(file_path),
meta: meta,
2016-12-13 06:22:24 +00:00
raw_content: "".to_string(),
2017-03-12 03:54:57 +00:00
assets: vec![],
2016-12-06 11:53:14 +00:00
content: "".to_string(),
2017-03-06 14:45:57 +00:00
slug: "".to_string(),
path: "".to_string(),
2017-03-06 14:45:57 +00:00
permalink: "".to_string(),
summary: None,
2017-02-23 08:34:57 +00:00
previous: None,
next: None,
2016-12-06 11:53:14 +00:00
}
}
2017-03-12 03:54:57 +00:00
/// Parse a page given the content of the .md file
/// Files without front matter or with invalid front matter are considered
/// erroneous
pub fn parse(file_path: &Path, content: &str, config: &Config) -> Result<Page> {
let (meta, content) = split_page_content(file_path, content)?;
let mut page = Page::new(file_path, meta);
page.raw_content = content;
2017-03-06 14:45:57 +00:00
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.file.name.clone())
2017-03-06 14:45:57 +00:00
}
};
if let Some(ref u) = page.meta.url {
page.path = u.trim().to_string();
} else {
page.path = if page.file.components.is_empty() {
page.slug.clone()
} else {
format!("{}/{}", page.file.components.join("/"), page.slug)
};
2016-12-13 10:14:49 +00:00
}
page.permalink = config.make_permalink(&page.path);
2016-12-13 10:14:49 +00:00
Ok(page)
}
2016-12-06 11:53:14 +00:00
2017-03-12 03:54:57 +00:00
/// Read and parse a .md file into a Page struct
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 content = read_file(path)?;
let mut page = Page::parse(path, &content, config)?;
2017-03-19 10:29:43 +00:00
page.assets = find_related_assets(path.parent().unwrap());
2016-12-13 06:22:24 +00:00
if !page.assets.is_empty() && page.file.name != "index" {
bail!("Page `{}` has assets ({:?}) but is not named index.md", path.display(), page.assets);
}
2016-12-06 08:27:03 +00:00
2017-03-12 03:54:57 +00:00
Ok(page)
}
2016-12-06 08:27:03 +00:00
2017-03-27 14:17:33 +00:00
/// We need access to all pages url to render links relative to content
/// so that can't happen at the same time as parsing
pub fn render_markdown(&mut self, permalinks: &HashMap<String, String>, tera: &Tera, config: &Config) -> Result<()> {
self.content = markdown_to_html(&self.raw_content, permalinks, tera, config)?;
if self.raw_content.contains("<!-- more -->") {
self.summary = Some({
2017-03-27 14:17:33 +00:00
let summary = self.raw_content.splitn(2, "<!-- more -->").collect::<Vec<&str>>()[0];
markdown_to_html(summary, permalinks, tera, config)?
})
2017-03-27 14:17:33 +00:00
}
Ok(())
}
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()
};
let mut context = Context::new();
context.add("config", config);
context.add("page", self);
context.add("current_url", &self.permalink);
context.add("current_path", &self.path);
2016-12-13 06:22:24 +00:00
tera.render(&tpl_name, &context)
.chain_err(|| format!("Failed to render page '{}'", self.file.path.display()))
}
2016-12-06 08:27:03 +00:00
}
impl Default for Page {
fn default() -> Page {
Page {
file: FileInfo::default(),
meta: PageFrontMatter::default(),
raw_content: "".to_string(),
assets: vec![],
content: "".to_string(),
slug: "".to_string(),
path: "".to_string(),
permalink: "".to_string(),
summary: None,
previous: None,
next: None,
}
}
}
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-05-16 04:37:00 +00:00
let mut state = serializer.serialize_struct("page", 15)?;
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("path", &format!("/{}", self.path))?;
2017-03-06 14:45:57 +00:00
state.serialize_field("permalink", &self.permalink)?;
state.serialize_field("summary", &self.summary)?;
2017-02-23 08:34:57 +00:00
state.serialize_field("tags", &self.meta.tags)?;
state.serialize_field("category", &self.meta.category)?;
state.serialize_field("extra", &self.meta.extra)?;
2017-05-15 03:23:19 +00:00
let (word_count, reading_time) = get_reading_analytics(&self.raw_content);
2017-03-06 13:45:33 +00:00
state.serialize_field("word_count", &word_count)?;
state.serialize_field("reading_time", &reading_time)?;
state.serialize_field("previous", &self.previous)?;
state.serialize_field("next", &self.next)?;
2017-02-23 08:34:57 +00:00
state.end()
}
}
2017-05-15 07:56:16 +00:00
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::fs::{File, create_dir};
use std::path::Path;
use tera::Tera;
use tempdir::TempDir;
use config::Config;
use super::Page;
#[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());
let mut page = res.unwrap();
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
assert_eq!(page.meta.title.unwrap(), "Hello".to_string());
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_make_url_from_sections_and_slug() {
let content = r#"
+++
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());
let mut page = res.unwrap();
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
assert_eq!(page.path, "posts/intro/hello-world");
assert_eq!(page.permalink, "http://hello.com/posts/intro/hello-world");
}
#[test]
fn can_make_url_from_slug_only() {
let content = r#"
+++
slug = "hello-world"
+++
Hello world"#;
let config = Config::default();
let res = Page::parse(Path::new("start.md"), content, &config);
assert!(res.is_ok());
let mut page = res.unwrap();
page.render_markdown(&HashMap::default(), &Tera::default(), &config).unwrap();
assert_eq!(page.path, "hello-world");
assert_eq!(page.permalink, config.make_permalink("hello-world"));
}
#[test]
fn errors_on_invalid_front_matter_format() {
// missing starting +++
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 can_make_slug_from_non_slug_filename() {
let config = Config::default();
let res = Page::parse(Path::new(" file with space.md"), "+++\n+++", &config);
assert!(res.is_ok());
let mut page = res.unwrap();
page.render_markdown(&HashMap::default(), &Tera::default(), &config).unwrap();
assert_eq!(page.slug, "file-with-space");
assert_eq!(page.permalink, config.make_permalink(&page.slug));
}
#[test]
fn can_specify_summary() {
let config = Config::default();
let content = r#"
+++
+++
Hello world
<!-- more -->"#.to_string();
let res = Page::parse(Path::new("hello.md"), &content, &config);
assert!(res.is_ok());
let mut page = res.unwrap();
page.render_markdown(&HashMap::default(), &Tera::default(), &config).unwrap();
assert_eq!(page.summary, Some("<p>Hello world</p>\n".to_string()));
}
#[test]
fn 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(
nested_path.join("index.md").as_path(),
"+++\nurl=\"hey\"+++\n",
&Config::default()
);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.file.parent, path.join("content").join("posts"));
2017-05-15 07:56:16 +00:00
}
#[test]
fn errors_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());
}
}