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-05-22 11:28:43 +00:00
|
|
|
use tera::{Tera, Context as TeraContext};
|
2017-02-23 08:34:57 +00:00
|
|
|
use serde::ser::{SerializeStruct, self};
|
2017-03-03 08:12:40 +00:00
|
|
|
use slug::slugify;
|
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;
|
2017-07-01 07:47:41 +00:00
|
|
|
use utils::fs::{read_file, find_related_assets};
|
|
|
|
use utils::site::get_reading_analytics;
|
2017-08-23 10:17:24 +00:00
|
|
|
use utils::templates::render_template;
|
2017-05-22 11:28:43 +00:00
|
|
|
use front_matter::{PageFrontMatter, InsertAnchor, split_page_content};
|
2017-07-01 07:47:41 +00:00
|
|
|
use rendering::{Context, Header, markdown_to_html};
|
|
|
|
|
|
|
|
use file_info::FileInfo;
|
2016-12-06 08:27:03 +00:00
|
|
|
|
2016-12-06 11:53:14 +00:00
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2016-12-11 06:05:03 +00:00
|
|
|
pub struct Page {
|
2017-05-15 10:53:39 +00:00
|
|
|
/// All info about the actual file
|
|
|
|
pub file: FileInfo,
|
2017-05-13 04:01:38 +00:00
|
|
|
/// 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
|
2016-12-11 06:05:03 +00:00
|
|
|
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,
|
2017-03-30 08:17:12 +00:00
|
|
|
/// The URL path of the page
|
|
|
|
pub path: String,
|
2017-10-31 15:41:31 +00:00
|
|
|
/// The components of the path of the page
|
|
|
|
pub components: Vec<String>,
|
2017-03-06 14:45:57 +00:00
|
|
|
/// The full URL for that page
|
|
|
|
pub permalink: String,
|
2017-04-20 02:48:14 +00:00
|
|
|
/// 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
|
2017-04-20 02:48:14 +00:00
|
|
|
pub summary: Option<String>,
|
2017-04-24 09:11:51 +00:00
|
|
|
/// 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>>,
|
2017-04-24 09:11:51 +00:00
|
|
|
/// 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>>,
|
2017-06-16 04:00:48 +00:00
|
|
|
/// Toc made from the headers of the markdown file
|
|
|
|
pub toc: Vec<Header>,
|
2016-12-06 08:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-23 08:34:57 +00:00
|
|
|
impl Page {
|
2017-05-15 10:53:39 +00:00
|
|
|
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 {
|
2017-05-15 10:53:39 +00:00
|
|
|
file: FileInfo::new_page(file_path),
|
2018-01-29 17:40:12 +00:00
|
|
|
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(),
|
2017-03-30 08:17:12 +00:00
|
|
|
path: "".to_string(),
|
2017-10-31 15:41:31 +00:00
|
|
|
components: vec![],
|
2017-03-06 14:45:57 +00:00
|
|
|
permalink: "".to_string(),
|
2017-04-20 02:48:14 +00:00
|
|
|
summary: None,
|
2017-02-23 08:34:57 +00:00
|
|
|
previous: None,
|
|
|
|
next: None,
|
2017-06-16 04:00:48 +00:00
|
|
|
toc: vec![],
|
2016-12-06 11:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 09:55:43 +00:00
|
|
|
pub fn is_draft(&self) -> bool {
|
|
|
|
self.meta.draft.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
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
|
2017-03-14 12:25:45 +00:00
|
|
|
pub fn parse(file_path: &Path, content: &str, config: &Config) -> Result<Page> {
|
2017-05-13 04:01:38 +00:00
|
|
|
let (meta, content) = split_page_content(file_path, content)?;
|
2017-05-15 10:53:39 +00:00
|
|
|
let mut page = Page::new(file_path, meta);
|
2017-03-14 12:25:45 +00:00
|
|
|
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 {
|
2017-10-04 00:23:25 +00:00
|
|
|
if page.file.name == "index" {
|
|
|
|
if let Some(parent) = page.file.path.parent() {
|
|
|
|
slugify(parent.file_name().unwrap().to_str().unwrap())
|
|
|
|
} else {
|
|
|
|
slugify(page.file.name.clone())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
slugify(page.file.name.clone())
|
|
|
|
}
|
2017-03-06 14:45:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-04 00:35:37 +00:00
|
|
|
if let Some(ref p) = page.meta.path {
|
|
|
|
page.path = p.trim().trim_left_matches('/').to_string();
|
2017-05-15 10:53:39 +00:00
|
|
|
} 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
|
|
|
}
|
2017-06-10 17:52:39 +00:00
|
|
|
if !page.path.ends_with('/') {
|
|
|
|
page.path = format!("{}/", page.path);
|
|
|
|
}
|
2017-10-04 00:35:37 +00:00
|
|
|
|
2017-10-31 15:41:31 +00:00
|
|
|
page.components = page.path.split('/')
|
|
|
|
.map(|p| p.to_string())
|
|
|
|
.filter(|p| !p.is_empty())
|
|
|
|
.collect::<Vec<_>>();
|
2017-03-30 08:17:12 +00:00
|
|
|
page.permalink = config.make_permalink(&page.path);
|
2016-12-13 10:14:49 +00:00
|
|
|
|
2016-12-11 06:05:03 +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();
|
2017-03-14 12:25:45 +00:00
|
|
|
let content = read_file(path)?;
|
|
|
|
let mut page = Page::parse(path, &content, config)?;
|
2016-12-13 06:22:24 +00:00
|
|
|
|
2017-06-21 09:07:36 +00:00
|
|
|
if page.file.name == "index" {
|
Filter ignored content in page.rs.
* Add ignored_content to the Config structure.
* Use the GlobSet crate to parse the glob patterns into a matcher, which
is created once at program initialization. If there are no patterns in
ignored_content, an empty globber is created, which excludes no files.
This is consistent with the existing behaviour of Gutenberg, before
this feature was added.
* Bail if there are any errors in the glob patterns.
* Add a call to the globber in page.rs to actually do the filtering.
* Update documentation.
A note on the Config structure
------------------------------
* I had to remove the PartialEq derive from the Config structure as it
does not work for the GlobSet type. No harm is done, Config does not
need to be PartialEq anyway, since there is no need to sort Configs.
* The implementation follows the pattern of the existing config settings
in that it uses an Option<...>. This would appear unnecessary, in that
an empty vec could be used as the default, but it appears to be needed
by the TOML parsing. A better approach would be to use a separate
SerializableConfig and map to/from a Config struct. This would also
allow the elimination of most, if not all, of the other Options in
the Config structure, but that ought to be another PR.
2018-02-25 11:42:31 +00:00
|
|
|
// `find_related_assets` only scans the immediate directory (it is not recursive) so our
|
|
|
|
// filtering only needs to work against the file_name component, not the full suffix. If
|
|
|
|
// `find_related_assets` was changed to also return files in subdirectories, we could
|
|
|
|
// use `PathBuf.strip_prefix` to remove the parent directory and then glob-filter
|
|
|
|
// against the remaining path. Note that the current behaviour effectively means that
|
|
|
|
// the `ignored_content` setting in the config file is limited to single-file glob
|
|
|
|
// patterns (no "**" patterns).
|
|
|
|
let globber = config.ignored_content_globber.as_ref().unwrap();
|
|
|
|
let parent_dir = path.parent().unwrap();
|
|
|
|
page.assets = find_related_assets(parent_dir).into_iter()
|
|
|
|
.filter(|path|
|
|
|
|
match path.file_name() {
|
|
|
|
None => true,
|
|
|
|
Some(file) => !globber.is_match(file)
|
|
|
|
}
|
|
|
|
).collect();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
page.assets = vec![];
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
2016-12-06 08:27:03 +00:00
|
|
|
|
2017-03-12 03:54:57 +00:00
|
|
|
Ok(page)
|
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
}
|
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
|
2017-05-22 11:28:43 +00:00
|
|
|
pub fn render_markdown(&mut self, permalinks: &HashMap<String, String>, tera: &Tera, config: &Config, anchor_insert: InsertAnchor) -> Result<()> {
|
2017-07-01 07:47:41 +00:00
|
|
|
let context = Context::new(
|
|
|
|
tera,
|
|
|
|
config.highlight_code.unwrap(),
|
|
|
|
config.highlight_theme.clone().unwrap(),
|
|
|
|
&self.permalink,
|
|
|
|
permalinks,
|
|
|
|
anchor_insert
|
|
|
|
);
|
2017-06-16 04:00:48 +00:00
|
|
|
let res = markdown_to_html(&self.raw_content, &context)?;
|
|
|
|
self.content = res.0;
|
|
|
|
self.toc = res.1;
|
2017-03-27 14:17:33 +00:00
|
|
|
if self.raw_content.contains("<!-- more -->") {
|
2017-04-20 02:48:14 +00:00
|
|
|
self.summary = Some({
|
2017-03-27 14:17:33 +00:00
|
|
|
let summary = self.raw_content.splitn(2, "<!-- more -->").collect::<Vec<&str>>()[0];
|
2017-06-16 04:00:48 +00:00
|
|
|
markdown_to_html(summary, &context)?.0
|
2017-04-20 02:48:14 +00:00
|
|
|
})
|
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
|
2017-06-29 12:14:08 +00:00
|
|
|
pub fn render_html(&self, tera: &Tera, config: &Config) -> Result<String> {
|
2017-03-10 12:36:43 +00:00
|
|
|
let tpl_name = match self.meta.template {
|
|
|
|
Some(ref l) => l.to_string(),
|
|
|
|
None => "page.html".to_string()
|
|
|
|
};
|
2017-03-30 08:17:12 +00:00
|
|
|
|
2017-05-22 11:28:43 +00:00
|
|
|
let mut context = TeraContext::new();
|
2017-03-10 12:36:43 +00:00
|
|
|
context.add("config", config);
|
2016-12-11 06:05:03 +00:00
|
|
|
context.add("page", self);
|
2017-03-30 08:17:12 +00:00
|
|
|
context.add("current_url", &self.permalink);
|
|
|
|
context.add("current_path", &self.path);
|
2016-12-13 06:22:24 +00:00
|
|
|
|
2017-08-23 10:17:24 +00:00
|
|
|
render_template(&tpl_name, tera, &context, config.theme.clone())
|
2017-05-15 10:53:39 +00:00
|
|
|
.chain_err(|| format!("Failed to render page '{}'", self.file.path.display()))
|
2016-12-11 06:05:03 +00:00
|
|
|
}
|
2016-12-06 08:27:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 04:01:38 +00:00
|
|
|
impl Default for Page {
|
|
|
|
fn default() -> Page {
|
|
|
|
Page {
|
2017-05-15 10:53:39 +00:00
|
|
|
file: FileInfo::default(),
|
2017-05-13 04:01:38 +00:00
|
|
|
meta: PageFrontMatter::default(),
|
|
|
|
raw_content: "".to_string(),
|
|
|
|
assets: vec![],
|
|
|
|
content: "".to_string(),
|
|
|
|
slug: "".to_string(),
|
|
|
|
path: "".to_string(),
|
2017-10-31 15:41:31 +00:00
|
|
|
components: vec![],
|
2017-05-13 04:01:38 +00:00
|
|
|
permalink: "".to_string(),
|
|
|
|
summary: None,
|
|
|
|
previous: None,
|
|
|
|
next: None,
|
2017-06-16 04:00:48 +00:00
|
|
|
toc: vec![],
|
2017-05-13 04:01:38 +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-11-01 03:29:27 +00:00
|
|
|
let mut state = serializer.serialize_struct("page", 18)?;
|
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)?;
|
2018-02-25 18:25:15 +00:00
|
|
|
state.serialize_field("date", &self.meta.date)?;
|
2017-03-06 14:45:57 +00:00
|
|
|
state.serialize_field("slug", &self.slug)?;
|
2017-09-26 08:21:08 +00:00
|
|
|
state.serialize_field("path", &self.path)?;
|
2017-10-31 15:41:31 +00:00
|
|
|
state.serialize_field("components", &self.components)?;
|
2017-03-06 14:45:57 +00:00
|
|
|
state.serialize_field("permalink", &self.permalink)?;
|
2017-04-20 02:48:14 +00:00
|
|
|
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)?;
|
2017-04-20 02:48:14 +00:00
|
|
|
state.serialize_field("previous", &self.previous)?;
|
|
|
|
state.serialize_field("next", &self.next)?;
|
2017-06-16 04:00:48 +00:00
|
|
|
state.serialize_field("toc", &self.toc)?;
|
2017-11-01 03:29:27 +00:00
|
|
|
state.serialize_field("draft", &self.is_draft())?;
|
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;
|
2017-10-04 00:23:25 +00:00
|
|
|
use std::io::Write;
|
2017-05-15 07:56:16 +00:00
|
|
|
use std::fs::{File, create_dir};
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use tera::Tera;
|
|
|
|
use tempdir::TempDir;
|
Filter ignored content in page.rs.
* Add ignored_content to the Config structure.
* Use the GlobSet crate to parse the glob patterns into a matcher, which
is created once at program initialization. If there are no patterns in
ignored_content, an empty globber is created, which excludes no files.
This is consistent with the existing behaviour of Gutenberg, before
this feature was added.
* Bail if there are any errors in the glob patterns.
* Add a call to the globber in page.rs to actually do the filtering.
* Update documentation.
A note on the Config structure
------------------------------
* I had to remove the PartialEq derive from the Config structure as it
does not work for the GlobSet type. No harm is done, Config does not
need to be PartialEq anyway, since there is no need to sort Configs.
* The implementation follows the pattern of the existing config settings
in that it uses an Option<...>. This would appear unnecessary, in that
an empty vec could be used as the default, but it appears to be needed
by the TOML parsing. A better approach would be to use a separate
SerializableConfig and map to/from a Config struct. This would also
allow the elimination of most, if not all, of the other Options in
the Config structure, but that ought to be another PR.
2018-02-25 11:42:31 +00:00
|
|
|
use globset::{Glob, GlobSetBuilder};
|
2017-05-15 07:56:16 +00:00
|
|
|
|
|
|
|
use config::Config;
|
|
|
|
use super::Page;
|
2017-05-22 11:28:43 +00:00
|
|
|
use front_matter::InsertAnchor;
|
2017-05-15 07:56:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[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();
|
2017-05-22 11:28:43 +00:00
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default(), InsertAnchor::None).unwrap();
|
2017-05-15 07:56:16 +00:00
|
|
|
|
|
|
|
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());
|
2017-05-22 11:28:43 +00:00
|
|
|
let page = res.unwrap();
|
2017-06-10 17:52:39 +00:00
|
|
|
assert_eq!(page.path, "posts/intro/hello-world/");
|
2017-10-31 15:41:31 +00:00
|
|
|
assert_eq!(page.components, vec!["posts", "intro", "hello-world"]);
|
2017-06-10 17:52:39 +00:00
|
|
|
assert_eq!(page.permalink, "http://hello.com/posts/intro/hello-world/");
|
2017-05-15 07:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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());
|
2017-05-22 11:28:43 +00:00
|
|
|
let page = res.unwrap();
|
2017-06-10 17:52:39 +00:00
|
|
|
assert_eq!(page.path, "hello-world/");
|
2017-10-31 15:41:31 +00:00
|
|
|
assert_eq!(page.components, vec!["hello-world"]);
|
2017-05-15 07:56:16 +00:00
|
|
|
assert_eq!(page.permalink, config.make_permalink("hello-world"));
|
|
|
|
}
|
|
|
|
|
2017-10-04 00:35:37 +00:00
|
|
|
#[test]
|
|
|
|
fn can_make_url_from_path() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
path = "hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let config = Config::default();
|
|
|
|
let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &config);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
assert_eq!(page.path, "hello-world/");
|
2017-10-31 15:41:31 +00:00
|
|
|
assert_eq!(page.components, vec!["hello-world"]);
|
2017-10-04 00:35:37 +00:00
|
|
|
assert_eq!(page.permalink, config.make_permalink("hello-world"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_make_url_from_path_starting_slash() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
path = "/hello-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let config = Config::default();
|
|
|
|
let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &config);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
assert_eq!(page.path, "hello-world/");
|
|
|
|
assert_eq!(page.permalink, config.make_permalink("hello-world"));
|
|
|
|
}
|
|
|
|
|
2017-05-15 07:56:16 +00:00
|
|
|
#[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());
|
2017-05-22 11:28:43 +00:00
|
|
|
let page = res.unwrap();
|
2017-05-15 07:56:16 +00:00
|
|
|
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();
|
2017-05-22 11:28:43 +00:00
|
|
|
page.render_markdown(&HashMap::default(), &Tera::default(), &config, InsertAnchor::None).unwrap();
|
2017-05-15 07:56:16 +00:00
|
|
|
assert_eq!(page.summary, Some("<p>Hello world</p>\n".to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-10-04 00:23:25 +00:00
|
|
|
fn page_with_assets_gets_right_info() {
|
2017-05-15 07:56:16 +00:00
|
|
|
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");
|
2017-10-04 00:23:25 +00:00
|
|
|
let nested_path = path.join("content").join("posts").join("with-assets");
|
2017-05-15 07:56:16 +00:00
|
|
|
create_dir(&nested_path).expect("create nested temp dir");
|
2017-10-04 00:23:25 +00:00
|
|
|
let mut f = File::create(nested_path.join("index.md")).unwrap();
|
|
|
|
f.write_all(b"+++\n+++\n").unwrap();
|
2017-05-15 07:56:16 +00:00
|
|
|
File::create(nested_path.join("example.js")).unwrap();
|
|
|
|
File::create(nested_path.join("graph.jpg")).unwrap();
|
|
|
|
File::create(nested_path.join("fail.png")).unwrap();
|
|
|
|
|
2017-10-04 00:23:25 +00:00
|
|
|
let res = Page::from_file(
|
2017-05-15 07:56:16 +00:00
|
|
|
nested_path.join("index.md").as_path(),
|
|
|
|
&Config::default()
|
|
|
|
);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
2017-05-15 10:53:39 +00:00
|
|
|
assert_eq!(page.file.parent, path.join("content").join("posts"));
|
2017-10-04 00:23:25 +00:00
|
|
|
assert_eq!(page.slug, "with-assets");
|
|
|
|
assert_eq!(page.assets.len(), 3);
|
|
|
|
assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn page_with_assets_and_slug_overrides_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("with-assets");
|
|
|
|
create_dir(&nested_path).expect("create nested temp dir");
|
|
|
|
let mut f = File::create(nested_path.join("index.md")).unwrap();
|
|
|
|
f.write_all(b"+++\nslug=\"hey\"\n+++\n").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::from_file(
|
|
|
|
nested_path.join("index.md").as_path(),
|
|
|
|
&Config::default()
|
|
|
|
);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
assert_eq!(page.file.parent, path.join("content").join("posts"));
|
|
|
|
assert_eq!(page.slug, "hey");
|
|
|
|
assert_eq!(page.assets.len(), 3);
|
|
|
|
assert_eq!(page.permalink, "http://a-website.com/posts/hey/");
|
2017-05-15 07:56:16 +00:00
|
|
|
}
|
Filter ignored content in page.rs.
* Add ignored_content to the Config structure.
* Use the GlobSet crate to parse the glob patterns into a matcher, which
is created once at program initialization. If there are no patterns in
ignored_content, an empty globber is created, which excludes no files.
This is consistent with the existing behaviour of Gutenberg, before
this feature was added.
* Bail if there are any errors in the glob patterns.
* Add a call to the globber in page.rs to actually do the filtering.
* Update documentation.
A note on the Config structure
------------------------------
* I had to remove the PartialEq derive from the Config structure as it
does not work for the GlobSet type. No harm is done, Config does not
need to be PartialEq anyway, since there is no need to sort Configs.
* The implementation follows the pattern of the existing config settings
in that it uses an Option<...>. This would appear unnecessary, in that
an empty vec could be used as the default, but it appears to be needed
by the TOML parsing. A better approach would be to use a separate
SerializableConfig and map to/from a Config struct. This would also
allow the elimination of most, if not all, of the other Options in
the Config structure, but that ought to be another PR.
2018-02-25 11:42:31 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn page_with_ignored_assets_filters_out_correct_files() {
|
|
|
|
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("with-assets");
|
|
|
|
create_dir(&nested_path).expect("create nested temp dir");
|
|
|
|
let mut f = File::create(nested_path.join("index.md")).unwrap();
|
|
|
|
f.write_all(b"+++\nslug=\"hey\"\n+++\n").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 mut gsb = GlobSetBuilder::new();
|
|
|
|
gsb.add(Glob::new("*.{js,png}").unwrap());
|
|
|
|
let mut config = Config::default();
|
|
|
|
config.ignored_content_globber = Some(gsb.build().unwrap());
|
|
|
|
|
|
|
|
let res = Page::from_file(
|
|
|
|
nested_path.join("index.md").as_path(),
|
|
|
|
&config
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
assert_eq!(page.assets.len(), 1);
|
|
|
|
assert_eq!(page.assets[0].file_name().unwrap().to_str(), Some("graph.jpg"));
|
|
|
|
}
|
2017-05-15 07:56:16 +00:00
|
|
|
}
|