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-14 12:25:45 +00:00
|
|
|
use std::fs::{read_dir};
|
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};
|
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-05-13 04:01:38 +00:00
|
|
|
use front_matter::{PageFrontMatter, SortBy, split_page_content};
|
2017-03-07 12:34:31 +00:00
|
|
|
use markdown::markdown_to_html;
|
2017-03-14 12:25:45 +00:00
|
|
|
use utils::{read_file, find_content_components};
|
2016-12-06 08:27:03 +00:00
|
|
|
|
|
|
|
|
2017-03-12 03:54:57 +00:00
|
|
|
/// Looks into the current folder for the path and see if there's anything that is not a .md
|
|
|
|
/// file. Those will be copied next to the rendered .html file
|
|
|
|
fn find_related_assets(path: &Path) -> Vec<PathBuf> {
|
|
|
|
let mut assets = vec![];
|
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
for entry in read_dir(path).unwrap().filter_map(|e| e.ok()) {
|
2017-03-12 03:54:57 +00:00
|
|
|
let entry_path = entry.path();
|
|
|
|
if entry_path.is_file() {
|
|
|
|
match entry_path.extension() {
|
|
|
|
Some(e) => match e.to_str() {
|
|
|
|
Some("md") => continue,
|
|
|
|
_ => assets.push(entry_path.to_path_buf()),
|
|
|
|
},
|
|
|
|
None => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assets
|
|
|
|
}
|
|
|
|
|
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-13 04:01:38 +00:00
|
|
|
/// The front matter meta-data
|
|
|
|
pub meta: PageFrontMatter,
|
2017-03-14 12:25:45 +00:00
|
|
|
/// The .md path
|
|
|
|
pub file_path: PathBuf,
|
2017-03-27 14:17:33 +00:00
|
|
|
/// The .md path, starting from the content directory, with / slashes
|
|
|
|
pub relative_path: String,
|
2017-03-14 12:25:45 +00:00
|
|
|
/// The parent directory of the file. Is actually the grand parent directory
|
|
|
|
/// if it's an asset folder
|
|
|
|
pub parent_path: PathBuf,
|
2017-02-23 08:34:57 +00:00
|
|
|
/// The name of the .md file
|
2017-03-14 12:25:45 +00:00
|
|
|
pub file_name: String,
|
|
|
|
/// The directories above our .md file
|
|
|
|
/// for example a file at content/kb/solutions/blabla.md will have 2 components:
|
2017-02-23 08:34:57 +00:00
|
|
|
/// `kb` and `solutions`
|
2017-03-14 12:25:45 +00:00
|
|
|
pub components: 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
|
|
|
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-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-03-06 14:45:57 +00:00
|
|
|
|
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>>,
|
2016-12-06 08:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-23 08:34:57 +00:00
|
|
|
impl Page {
|
2017-05-13 04:01:38 +00:00
|
|
|
pub fn new(meta: PageFrontMatter) -> Page {
|
2016-12-06 11:53:14 +00:00
|
|
|
Page {
|
2017-05-13 04:01:38 +00:00
|
|
|
meta: meta,
|
2017-03-14 12:25:45 +00:00
|
|
|
file_path: PathBuf::new(),
|
2017-03-27 14:17:33 +00:00
|
|
|
relative_path: String::new(),
|
2017-03-14 12:25:45 +00:00
|
|
|
parent_path: PathBuf::new(),
|
|
|
|
file_name: "".to_string(),
|
|
|
|
components: vec![],
|
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-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,
|
2016-12-06 11:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:59:49 +00:00
|
|
|
pub fn has_date(&self) -> bool {
|
|
|
|
self.meta.date.is_some()
|
|
|
|
}
|
|
|
|
|
2017-03-12 03:54:57 +00:00
|
|
|
/// Get word count and estimated reading time
|
2017-03-06 13:45:33 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
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> {
|
2016-12-06 08:27:03 +00:00
|
|
|
// 1. separate front matter from content
|
2017-05-13 04:01:38 +00:00
|
|
|
let (meta, content) = split_page_content(file_path, content)?;
|
2017-02-23 08:34:57 +00:00
|
|
|
let mut page = Page::new(meta);
|
2017-03-14 12:25:45 +00:00
|
|
|
page.file_path = file_path.to_path_buf();
|
|
|
|
page.parent_path = page.file_path.parent().unwrap().to_path_buf();
|
|
|
|
page.raw_content = content;
|
2017-03-07 03:42:14 +00:00
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
let path = Path::new(file_path);
|
|
|
|
page.file_name = path.file_stem().unwrap().to_string_lossy().to_string();
|
|
|
|
|
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-03-14 12:25:45 +00:00
|
|
|
slugify(page.file_name.clone())
|
2017-03-06 14:45:57 +00:00
|
|
|
}
|
|
|
|
};
|
2017-03-27 14:17:33 +00:00
|
|
|
page.components = find_content_components(&page.file_path);
|
|
|
|
page.relative_path = format!("{}/{}.md", page.components.join("/"), page.file_name);
|
2017-03-06 14:45:57 +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-05-09 12:47:02 +00:00
|
|
|
let mut path_set = false;
|
2017-03-06 14:45:57 +00:00
|
|
|
if let Some(ref u) = page.meta.url {
|
2017-03-30 08:17:12 +00:00
|
|
|
page.path = u.trim().to_string();
|
2017-05-09 12:47:02 +00:00
|
|
|
path_set = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !page.components.is_empty() {
|
2017-04-22 03:35:11 +00:00
|
|
|
// If we have a folder with an asset, don't consider it as a component
|
|
|
|
if page.file_name == "index" {
|
|
|
|
page.components.pop();
|
|
|
|
// also set parent_path to grandparent instead
|
|
|
|
page.parent_path = page.parent_path.parent().unwrap().to_path_buf();
|
2017-03-06 14:45:57 +00:00
|
|
|
}
|
2017-05-09 12:47:02 +00:00
|
|
|
if !path_set {
|
|
|
|
// Don't add a trailing slash to sections
|
|
|
|
page.path = format!("{}/{}", page.components.join("/"), page.slug);
|
|
|
|
}
|
|
|
|
} else if !path_set {
|
2017-04-22 03:35:11 +00:00
|
|
|
page.path = page.slug.clone();
|
2016-12-13 10:14:49 +00:00
|
|
|
}
|
2017-03-14 12:25:45 +00:00
|
|
|
|
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)?;
|
2017-03-19 10:29:43 +00:00
|
|
|
page.assets = find_related_assets(path.parent().unwrap());
|
2016-12-13 06:22:24 +00:00
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
if !page.assets.is_empty() && page.file_name != "index" {
|
2017-04-25 03:00:42 +00:00
|
|
|
bail!("Page `{}` has assets ({:?}) but is not named index.md", path.display(), page.assets);
|
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
|
|
|
|
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 -->") {
|
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];
|
|
|
|
markdown_to_html(summary, permalinks, tera, config)?
|
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-03-03 08:12:40 +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
|
|
|
|
2016-12-11 06:05:03 +00:00
|
|
|
let mut context = Context::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-03-10 12:36:43 +00:00
|
|
|
tera.render(&tpl_name, &context)
|
2017-03-25 06:52:51 +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 {
|
|
|
|
meta: PageFrontMatter::default(),
|
|
|
|
file_path: PathBuf::new(),
|
|
|
|
relative_path: String::new(),
|
|
|
|
parent_path: PathBuf::new(),
|
|
|
|
file_name: "".to_string(),
|
|
|
|
components: vec![],
|
|
|
|
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-04-24 09:11:51 +00:00
|
|
|
let mut state = serializer.serialize_struct("page", 16)?;
|
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)?;
|
2017-03-30 08:17:12 +00:00
|
|
|
state.serialize_field("path", &format!("/{}", self.path))?;
|
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("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-04-20 02:48:14 +00:00
|
|
|
state.serialize_field("previous", &self.previous)?;
|
|
|
|
state.serialize_field("next", &self.next)?;
|
2017-02-23 08:34:57 +00:00
|
|
|
state.end()
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 08:27:03 +00:00
|
|
|
|
2017-05-01 06:35:49 +00:00
|
|
|
/// Sort pages using the method for the given section
|
|
|
|
///
|
|
|
|
/// Any pages that doesn't have a date when the sorting method is date or order
|
|
|
|
/// when the sorting method is order will be ignored.
|
2017-05-08 10:29:37 +00:00
|
|
|
pub fn sort_pages(pages: Vec<Page>, sort_by: SortBy) -> (Vec<Page>, Vec<Page>) {
|
2017-04-24 09:11:51 +00:00
|
|
|
match sort_by {
|
|
|
|
SortBy::Date => {
|
|
|
|
let mut can_be_sorted = vec![];
|
|
|
|
let mut cannot_be_sorted = vec![];
|
|
|
|
for page in pages {
|
|
|
|
if page.meta.date.is_some() {
|
|
|
|
can_be_sorted.push(page);
|
|
|
|
} else {
|
|
|
|
cannot_be_sorted.push(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
can_be_sorted.sort_by(|a, b| b.meta.date().unwrap().cmp(&a.meta.date().unwrap()));
|
|
|
|
|
2017-05-01 08:10:22 +00:00
|
|
|
(can_be_sorted, cannot_be_sorted)
|
2017-04-24 09:11:51 +00:00
|
|
|
},
|
|
|
|
SortBy::Order => {
|
|
|
|
let mut can_be_sorted = vec![];
|
|
|
|
let mut cannot_be_sorted = vec![];
|
|
|
|
for page in pages {
|
|
|
|
if page.meta.order.is_some() {
|
|
|
|
can_be_sorted.push(page);
|
|
|
|
} else {
|
|
|
|
cannot_be_sorted.push(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
can_be_sorted.sort_by(|a, b| b.meta.order().cmp(&a.meta.order()));
|
|
|
|
|
2017-05-01 08:10:22 +00:00
|
|
|
(can_be_sorted, cannot_be_sorted)
|
2017-04-24 09:11:51 +00:00
|
|
|
},
|
2017-05-13 13:37:01 +00:00
|
|
|
SortBy::None => (pages, vec![])
|
2017-04-24 09:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:59:49 +00:00
|
|
|
/// Horribly inefficient way to set previous and next on each pages
|
|
|
|
/// So many clones
|
2017-04-24 09:11:51 +00:00
|
|
|
pub fn populate_previous_and_next_pages(input: &[Page]) -> Vec<Page> {
|
2017-03-22 11:59:49 +00:00
|
|
|
let pages = input.to_vec();
|
|
|
|
let mut res = Vec::new();
|
|
|
|
|
2017-04-24 09:11:51 +00:00
|
|
|
// the input is already sorted
|
|
|
|
// We might put prev/next randomly if a page is missing date/order, probably fine
|
2017-03-22 11:59:49 +00:00
|
|
|
for (i, page) in input.iter().enumerate() {
|
|
|
|
let mut new_page = page.clone();
|
|
|
|
|
2017-04-24 09:11:51 +00:00
|
|
|
if i > 0 {
|
|
|
|
let next = &pages[i - 1];
|
|
|
|
new_page.next = Some(Box::new(next.clone()));
|
|
|
|
}
|
2017-03-22 11:59:49 +00:00
|
|
|
|
2017-04-24 09:11:51 +00:00
|
|
|
if i < input.len() - 1 {
|
|
|
|
let previous = &pages[i + 1];
|
|
|
|
new_page.previous = Some(Box::new(previous.clone()));
|
2017-03-22 11:59:49 +00:00
|
|
|
}
|
|
|
|
res.push(new_page);
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2016-12-06 08:27:03 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-03-14 12:25:45 +00:00
|
|
|
use std::fs::File;
|
2017-04-24 09:11:51 +00:00
|
|
|
|
2017-05-14 05:14:58 +00:00
|
|
|
use tempdir::TempDir;
|
|
|
|
|
2017-05-13 04:01:38 +00:00
|
|
|
use front_matter::{PageFrontMatter, SortBy};
|
2017-04-24 09:11:51 +00:00
|
|
|
use super::{Page, find_related_assets, sort_pages, populate_previous_and_next_pages};
|
2016-12-13 06:22:24 +00:00
|
|
|
|
2017-04-24 09:11:51 +00:00
|
|
|
fn create_page_with_date(date: &str) -> Page {
|
2017-05-13 04:01:38 +00:00
|
|
|
let mut front_matter = PageFrontMatter::default();
|
2017-04-24 09:11:51 +00:00
|
|
|
front_matter.date = Some(date.to_string());
|
|
|
|
Page::new(front_matter)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_page_with_order(order: usize) -> Page {
|
2017-05-13 04:01:38 +00:00
|
|
|
let mut front_matter = PageFrontMatter::default();
|
2017-04-24 09:11:51 +00:00
|
|
|
front_matter.order = Some(order);
|
|
|
|
Page::new(front_matter)
|
|
|
|
}
|
2016-12-06 11:53:14 +00:00
|
|
|
|
2016-12-13 10:14:49 +00:00
|
|
|
#[test]
|
2017-05-14 05:14:58 +00:00
|
|
|
fn can_find_related_assets() {
|
2017-03-14 12:25:45 +00:00
|
|
|
let tmp_dir = TempDir::new("example").expect("create temp dir");
|
|
|
|
File::create(tmp_dir.path().join("index.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 assets = find_related_assets(tmp_dir.path());
|
|
|
|
assert_eq!(assets.len(), 3);
|
|
|
|
assert_eq!(assets.iter().filter(|p| p.extension().unwrap() != "md").count(), 3);
|
|
|
|
assert_eq!(assets.iter().filter(|p| p.file_name().unwrap() == "example.js").count(), 1);
|
|
|
|
assert_eq!(assets.iter().filter(|p| p.file_name().unwrap() == "graph.jpg").count(), 1);
|
|
|
|
assert_eq!(assets.iter().filter(|p| p.file_name().unwrap() == "fail.png").count(), 1);
|
2017-03-10 08:28:17 +00:00
|
|
|
}
|
2017-04-24 09:11:51 +00:00
|
|
|
|
|
|
|
#[test]
|
2017-05-14 05:14:58 +00:00
|
|
|
fn can_sort_by_dates() {
|
2017-04-24 09:11:51 +00:00
|
|
|
let input = vec![
|
|
|
|
create_page_with_date("2018-01-01"),
|
|
|
|
create_page_with_date("2017-01-01"),
|
|
|
|
create_page_with_date("2019-01-01"),
|
|
|
|
];
|
2017-05-08 10:29:37 +00:00
|
|
|
let (pages, _) = sort_pages(input, SortBy::Date);
|
2017-04-24 09:11:51 +00:00
|
|
|
// Should be sorted by date
|
|
|
|
assert_eq!(pages[0].clone().meta.date.unwrap(), "2019-01-01");
|
|
|
|
assert_eq!(pages[1].clone().meta.date.unwrap(), "2018-01-01");
|
|
|
|
assert_eq!(pages[2].clone().meta.date.unwrap(), "2017-01-01");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-05-14 05:14:58 +00:00
|
|
|
fn can_sort_by_order() {
|
2017-04-24 09:11:51 +00:00
|
|
|
let input = vec![
|
|
|
|
create_page_with_order(2),
|
|
|
|
create_page_with_order(3),
|
|
|
|
create_page_with_order(1),
|
|
|
|
];
|
2017-05-08 10:29:37 +00:00
|
|
|
let (pages, _) = sort_pages(input, SortBy::Order);
|
2017-04-24 09:11:51 +00:00
|
|
|
// Should be sorted by date
|
|
|
|
assert_eq!(pages[0].clone().meta.order.unwrap(), 3);
|
|
|
|
assert_eq!(pages[1].clone().meta.order.unwrap(), 2);
|
|
|
|
assert_eq!(pages[2].clone().meta.order.unwrap(), 1);
|
|
|
|
}
|
|
|
|
|
2017-05-01 06:35:49 +00:00
|
|
|
#[test]
|
2017-05-14 05:14:58 +00:00
|
|
|
fn can_sort_by_none() {
|
2017-05-01 06:35:49 +00:00
|
|
|
let input = vec![
|
|
|
|
create_page_with_order(2),
|
|
|
|
create_page_with_order(3),
|
|
|
|
create_page_with_order(1),
|
|
|
|
];
|
2017-05-08 10:29:37 +00:00
|
|
|
let (pages, _) = sort_pages(input, SortBy::None);
|
2017-05-01 06:35:49 +00:00
|
|
|
// Should be sorted by date
|
|
|
|
assert_eq!(pages[0].clone().meta.order.unwrap(), 2);
|
|
|
|
assert_eq!(pages[1].clone().meta.order.unwrap(), 3);
|
|
|
|
assert_eq!(pages[2].clone().meta.order.unwrap(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-05-14 05:14:58 +00:00
|
|
|
fn ignore_page_with_missing_field() {
|
2017-05-01 06:35:49 +00:00
|
|
|
let input = vec![
|
|
|
|
create_page_with_order(2),
|
|
|
|
create_page_with_order(3),
|
|
|
|
create_page_with_date("2019-01-01"),
|
|
|
|
];
|
2017-05-08 10:29:37 +00:00
|
|
|
let (pages, unsorted) = sort_pages(input, SortBy::Order);
|
2017-05-01 06:35:49 +00:00
|
|
|
assert_eq!(pages.len(), 2);
|
2017-05-01 08:10:22 +00:00
|
|
|
assert_eq!(unsorted.len(), 1);
|
2017-05-01 06:35:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 09:11:51 +00:00
|
|
|
#[test]
|
2017-05-14 05:14:58 +00:00
|
|
|
fn can_populate_previous_and_next_pages() {
|
2017-04-24 09:11:51 +00:00
|
|
|
let input = vec![
|
|
|
|
create_page_with_order(3),
|
|
|
|
create_page_with_order(2),
|
|
|
|
create_page_with_order(1),
|
|
|
|
];
|
|
|
|
let pages = populate_previous_and_next_pages(input.as_slice());
|
|
|
|
|
|
|
|
assert!(pages[0].clone().next.is_none());
|
|
|
|
assert!(pages[0].clone().previous.is_some());
|
|
|
|
assert_eq!(pages[0].clone().previous.unwrap().meta.order.unwrap(), 2);
|
|
|
|
|
|
|
|
assert!(pages[1].clone().next.is_some());
|
|
|
|
assert!(pages[1].clone().previous.is_some());
|
|
|
|
assert_eq!(pages[1].clone().next.unwrap().meta.order.unwrap(), 3);
|
|
|
|
assert_eq!(pages[1].clone().previous.unwrap().meta.order.unwrap(), 1);
|
|
|
|
|
|
|
|
assert!(pages[2].clone().next.is_some());
|
|
|
|
assert!(pages[2].clone().previous.is_none());
|
|
|
|
assert_eq!(pages[2].clone().next.unwrap().meta.order.unwrap(), 2);
|
|
|
|
}
|
2016-12-06 08:27:03 +00:00
|
|
|
}
|