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};
|
2016-12-06 08:27:03 +00:00
|
|
|
|
2018-10-25 14:22:00 +00:00
|
|
|
use regex::Regex;
|
2019-09-16 09:44:39 +00:00
|
|
|
use slotmap::DefaultKey;
|
2018-10-31 07:18:57 +00:00
|
|
|
use slug::slugify;
|
|
|
|
use tera::{Context as TeraContext, Tera};
|
2016-12-06 08:27:03 +00:00
|
|
|
|
2016-12-06 12:48:23 +00:00
|
|
|
use config::Config;
|
2019-02-09 18:54:46 +00:00
|
|
|
use errors::{Error, Result};
|
2018-10-31 07:18:57 +00:00
|
|
|
use front_matter::{split_page_content, InsertAnchor, PageFrontMatter};
|
|
|
|
use library::Library;
|
2019-09-06 21:36:30 +00:00
|
|
|
use rendering::{render_content, Heading, RenderContext};
|
2018-10-31 07:18:57 +00:00
|
|
|
use utils::fs::{find_related_assets, read_file};
|
2017-07-01 07:47:41 +00:00
|
|
|
use utils::site::get_reading_analytics;
|
2017-08-23 10:17:24 +00:00
|
|
|
use utils::templates::render_template;
|
2017-07-01 07:47:41 +00:00
|
|
|
|
2018-10-02 14:42:34 +00:00
|
|
|
use content::file_info::FileInfo;
|
2019-06-06 17:49:31 +00:00
|
|
|
use content::has_anchor;
|
2018-10-24 09:40:57 +00:00
|
|
|
use content::ser::SerializingPage;
|
2016-12-06 08:27:03 +00:00
|
|
|
|
2018-10-25 14:22:00 +00:00
|
|
|
lazy_static! {
|
2018-11-30 21:20:58 +00:00
|
|
|
// Based on https://regex101.com/r/H2n38Z/1/tests
|
|
|
|
// A regex parsing RFC3339 date followed by {_,-}, some characters and ended by .md
|
|
|
|
static ref RFC3339_DATE: Regex = Regex::new(
|
|
|
|
r"^(?P<datetime>(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])(T([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)([01][0-9]|2[0-3]):([0-5][0-9])))?)(_|-)(?P<slug>.+$)"
|
|
|
|
).unwrap();
|
2018-10-25 14:22:00 +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,
|
2018-10-18 13:54:51 +00:00
|
|
|
/// The list of parent sections
|
2019-09-16 09:44:39 +00:00
|
|
|
pub ancestors: Vec<DefaultKey>,
|
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>,
|
2018-10-24 09:49:09 +00:00
|
|
|
/// All the non-md files we found next to the .md file as string for use in templates
|
|
|
|
pub serialized_assets: Vec<String>,
|
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>,
|
2018-07-28 02:20:20 +00:00
|
|
|
/// The earlier page, for pages sorted by date
|
2019-09-16 09:44:39 +00:00
|
|
|
pub earlier: Option<DefaultKey>,
|
2018-07-28 02:20:20 +00:00
|
|
|
/// The later page, for pages sorted by date
|
2019-09-16 09:44:39 +00:00
|
|
|
pub later: Option<DefaultKey>,
|
2018-07-28 02:20:20 +00:00
|
|
|
/// The lighter page, for pages sorted by weight
|
2019-09-16 09:44:39 +00:00
|
|
|
pub lighter: Option<DefaultKey>,
|
2018-07-28 02:20:20 +00:00
|
|
|
/// The heavier page, for pages sorted by weight
|
2019-09-16 09:44:39 +00:00
|
|
|
pub heavier: Option<DefaultKey>,
|
2019-09-06 21:36:30 +00:00
|
|
|
/// Toc made from the headings of the markdown file
|
|
|
|
pub toc: Vec<Heading>,
|
2018-09-20 16:27:56 +00:00
|
|
|
/// How many words in the raw content
|
|
|
|
pub word_count: Option<usize>,
|
|
|
|
/// How long would it take to read the raw content.
|
|
|
|
/// See `get_reading_analytics` on how it is calculated
|
|
|
|
pub reading_time: Option<usize>,
|
2019-01-29 18:20:03 +00:00
|
|
|
/// The language of that page. Equal to the default lang if the user doesn't setup `languages` in config.
|
2018-12-27 12:14:54 +00:00
|
|
|
/// Corresponds to the lang in the {slug}.{lang}.md file scheme
|
2019-01-29 18:20:03 +00:00
|
|
|
pub lang: String,
|
2018-12-29 10:17:43 +00:00
|
|
|
/// Contains all the translated version of that page
|
2019-09-16 09:44:39 +00:00
|
|
|
pub translations: Vec<DefaultKey>,
|
2019-06-06 17:49:31 +00:00
|
|
|
/// Contains the internal links that have an anchor: we can only check the anchor
|
|
|
|
/// after all pages have been built and their ToC compiled. The page itself should exist otherwise
|
|
|
|
/// it would have errored before getting there
|
|
|
|
/// (path to markdown, anchor value)
|
|
|
|
pub internal_links_with_anchors: Vec<(String, String)>,
|
2019-05-27 12:05:07 +00:00
|
|
|
/// Contains the external links that need to be checked
|
|
|
|
pub external_links: Vec<String>,
|
2016-12-06 08:27:03 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 08:34:57 +00:00
|
|
|
impl Page {
|
2019-03-08 22:26:57 +00:00
|
|
|
pub fn new<P: AsRef<Path>>(file_path: P, meta: PageFrontMatter, base_path: &PathBuf) -> Page {
|
2017-05-15 10:53:39 +00:00
|
|
|
let file_path = file_path.as_ref();
|
|
|
|
|
2016-12-06 11:53:14 +00:00
|
|
|
Page {
|
2019-03-08 22:26:57 +00:00
|
|
|
file: FileInfo::new_page(file_path, base_path),
|
2018-01-29 17:40:12 +00:00
|
|
|
meta,
|
2018-10-18 13:54:51 +00:00
|
|
|
ancestors: vec![],
|
2016-12-13 06:22:24 +00:00
|
|
|
raw_content: "".to_string(),
|
2017-03-12 03:54:57 +00:00
|
|
|
assets: vec![],
|
2018-10-24 09:49:09 +00:00
|
|
|
serialized_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,
|
2018-07-28 02:20:20 +00:00
|
|
|
earlier: None,
|
|
|
|
later: None,
|
|
|
|
lighter: None,
|
|
|
|
heavier: None,
|
2017-06-16 04:00:48 +00:00
|
|
|
toc: vec![],
|
2018-09-20 16:27:56 +00:00
|
|
|
word_count: None,
|
|
|
|
reading_time: None,
|
2019-01-29 18:20:03 +00:00
|
|
|
lang: String::new(),
|
2018-12-29 10:17:43 +00:00
|
|
|
translations: Vec::new(),
|
2019-06-06 17:49:31 +00:00
|
|
|
internal_links_with_anchors: Vec::new(),
|
2019-05-27 12:05:07 +00:00
|
|
|
external_links: Vec::new(),
|
2016-12-06 11:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 09:55:43 +00:00
|
|
|
pub fn is_draft(&self) -> bool {
|
2018-03-21 15:18:24 +00:00
|
|
|
self.meta.draft
|
2017-09-25 09:55:43 +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
|
2019-03-08 22:26:57 +00:00
|
|
|
pub fn parse(
|
|
|
|
file_path: &Path,
|
|
|
|
content: &str,
|
|
|
|
config: &Config,
|
|
|
|
base_path: &PathBuf,
|
|
|
|
) -> Result<Page> {
|
2017-05-13 04:01:38 +00:00
|
|
|
let (meta, content) = split_page_content(file_path, content)?;
|
2019-03-08 22:26:57 +00:00
|
|
|
let mut page = Page::new(file_path, meta, base_path);
|
2018-10-25 14:22:00 +00:00
|
|
|
|
2018-12-27 12:14:54 +00:00
|
|
|
page.lang = page.file.find_language(config)?;
|
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
page.raw_content = content;
|
2018-09-20 16:27:56 +00:00
|
|
|
let (word_count, reading_time) = get_reading_analytics(&page.raw_content);
|
|
|
|
page.word_count = Some(word_count);
|
|
|
|
page.reading_time = Some(reading_time);
|
2018-10-25 14:22:00 +00:00
|
|
|
|
2018-11-30 21:20:58 +00:00
|
|
|
let mut slug_from_dated_filename = None;
|
2019-02-09 18:54:46 +00:00
|
|
|
let file_path = if page.file.name == "index" {
|
|
|
|
if let Some(parent) = page.file.path.parent() {
|
|
|
|
parent.file_name().unwrap().to_str().unwrap().to_string()
|
|
|
|
} else {
|
|
|
|
page.file.name.replace(".md", "")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
page.file.name.replace(".md", "")
|
|
|
|
};
|
|
|
|
if let Some(ref caps) = RFC3339_DATE.captures(&file_path) {
|
2018-11-30 21:20:58 +00:00
|
|
|
slug_from_dated_filename = Some(caps.name("slug").unwrap().as_str().to_string());
|
2018-10-25 14:22:00 +00:00
|
|
|
if page.meta.date.is_none() {
|
2018-11-30 21:20:58 +00:00
|
|
|
page.meta.date = Some(caps.name("datetime").unwrap().as_str().to_string());
|
2018-11-12 23:08:46 +00:00
|
|
|
page.meta.date_to_datetime();
|
2018-10-25 14:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 14:45:57 +00:00
|
|
|
page.slug = {
|
|
|
|
if let Some(ref slug) = page.meta.slug {
|
2019-08-04 14:13:07 +00:00
|
|
|
slugify(&slug.trim())
|
2018-09-30 19:15:09 +00:00
|
|
|
} else if page.file.name == "index" {
|
|
|
|
if let Some(parent) = page.file.path.parent() {
|
2019-02-09 18:54:46 +00:00
|
|
|
if let Some(slug) = slug_from_dated_filename {
|
|
|
|
slugify(&slug)
|
|
|
|
} else {
|
|
|
|
slugify(parent.file_name().unwrap().to_str().unwrap())
|
|
|
|
}
|
2017-10-04 00:23:25 +00:00
|
|
|
} else {
|
2018-10-25 14:22:00 +00:00
|
|
|
slugify(&page.file.name)
|
2017-10-04 00:23:25 +00:00
|
|
|
}
|
Fix clippy warnings (#744)
Clippy is returning some warnings. Let's fix or explicitly ignore
them. In particular:
- In `components/imageproc/src/lib.rs`, we implement `Hash` explicitly
but derive `PartialEq`. We need to maintain the property that two
keys being equal implies the hashes of those two keys are equal.
Our `Hash` implementations preserve this, so we'll explicitly ignore
the warnings.
- In `components/site/src/lib.rs`, we were calling `.into()` on some
values that are already of the correct type.
- In `components/site/src/lib.rs`, we were using `.map(|x| *x)` in
iterator chains to remove a level of indirection; we can instead say
`.copied()` (introduced in Rust v1.36) or `.cloned()`. Using
`.copied` here is better from a type-checking point of view, but
we'll use `.cloned` for now as Rust v1.36 was only recently
released.
- In `components/templates/src/filters.rs` and
`components/utils/src/site.rs`, we were taking `HashMap`s as
function arguments but not generically accepting alternate `Hasher`
implementations.
- In `src/cmd/check.rs`, we use `env::current_dir()` as a default
value, but our use of `unwrap_or` meant that we would always
retrieve the current directory even when not needed.
- In `components/errors/src/lib.rs`, we can use `if let` rather than
`match`.
- In `components/library/src/content/page.rs`, we can collapse a
nested conditional into `else if let ...`.
- In `components/library/src/sorting.rs`, a function takes `&&Page`
arguments. Clippy warns about this for efficiency reasons, but
we're doing it here to match a particular sorting API, so we'll
explicitly ignore the warning.
2019-07-12 08:29:44 +00:00
|
|
|
} else if let Some(slug) = slug_from_dated_filename {
|
|
|
|
slugify(&slug)
|
2018-09-30 19:15:09 +00:00
|
|
|
} else {
|
Fix clippy warnings (#744)
Clippy is returning some warnings. Let's fix or explicitly ignore
them. In particular:
- In `components/imageproc/src/lib.rs`, we implement `Hash` explicitly
but derive `PartialEq`. We need to maintain the property that two
keys being equal implies the hashes of those two keys are equal.
Our `Hash` implementations preserve this, so we'll explicitly ignore
the warnings.
- In `components/site/src/lib.rs`, we were calling `.into()` on some
values that are already of the correct type.
- In `components/site/src/lib.rs`, we were using `.map(|x| *x)` in
iterator chains to remove a level of indirection; we can instead say
`.copied()` (introduced in Rust v1.36) or `.cloned()`. Using
`.copied` here is better from a type-checking point of view, but
we'll use `.cloned` for now as Rust v1.36 was only recently
released.
- In `components/templates/src/filters.rs` and
`components/utils/src/site.rs`, we were taking `HashMap`s as
function arguments but not generically accepting alternate `Hasher`
implementations.
- In `src/cmd/check.rs`, we use `env::current_dir()` as a default
value, but our use of `unwrap_or` meant that we would always
retrieve the current directory even when not needed.
- In `components/errors/src/lib.rs`, we can use `if let` rather than
`match`.
- In `components/library/src/content/page.rs`, we can collapse a
nested conditional into `else if let ...`.
- In `components/library/src/sorting.rs`, a function takes `&&Page`
arguments. Clippy warns about this for efficiency reasons, but
we're doing it here to match a particular sorting API, so we'll
explicitly ignore the warning.
2019-07-12 08:29:44 +00:00
|
|
|
slugify(&page.file.name)
|
2017-03-06 14:45:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-04 00:35:37 +00:00
|
|
|
if let Some(ref p) = page.meta.path {
|
2019-01-29 08:30:54 +00:00
|
|
|
page.path = p.trim().trim_start_matches('/').to_string();
|
2017-05-15 10:53:39 +00:00
|
|
|
} else {
|
2018-12-28 09:42:26 +00:00
|
|
|
let mut path = if page.file.components.is_empty() {
|
2017-05-15 10:53:39 +00:00
|
|
|
page.slug.clone()
|
|
|
|
} else {
|
|
|
|
format!("{}/{}", page.file.components.join("/"), page.slug)
|
|
|
|
};
|
2018-12-28 09:42:26 +00:00
|
|
|
|
2019-01-29 18:20:03 +00:00
|
|
|
if page.lang != config.default_language {
|
|
|
|
path = format!("{}/{}", page.lang, path);
|
2018-12-28 09:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
page.path = path;
|
2016-12-13 10:14:49 +00:00
|
|
|
}
|
2019-05-16 04:40:58 +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
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
page.components = page
|
|
|
|
.path
|
|
|
|
.split('/')
|
2017-10-31 15:41:31 +00:00
|
|
|
.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
|
2019-03-08 22:26:57 +00:00
|
|
|
pub fn from_file<P: AsRef<Path>>(
|
|
|
|
path: P,
|
|
|
|
config: &Config,
|
|
|
|
base_path: &PathBuf,
|
|
|
|
) -> 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)?;
|
2019-03-08 22:26:57 +00:00
|
|
|
let mut page = Page::parse(path, &content, config, base_path)?;
|
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
|
|
|
let parent_dir = path.parent().unwrap();
|
2018-03-12 19:11:03 +00:00
|
|
|
let assets = find_related_assets(parent_dir);
|
|
|
|
|
|
|
|
if let Some(ref globset) = config.ignored_content_globset {
|
|
|
|
// `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).
|
2018-10-31 07:18:57 +00:00
|
|
|
page.assets = assets
|
|
|
|
.into_iter()
|
|
|
|
.filter(|path| match path.file_name() {
|
|
|
|
None => true,
|
|
|
|
Some(file) => !globset.is_match(file),
|
|
|
|
})
|
|
|
|
.collect();
|
2018-03-12 19:11:03 +00:00
|
|
|
} else {
|
|
|
|
page.assets = assets;
|
|
|
|
}
|
2018-10-24 09:49:09 +00:00
|
|
|
|
2019-05-16 04:40:58 +00:00
|
|
|
page.serialized_assets = page.serialize_assets(&base_path);
|
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
|
|
|
} 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
|
2018-08-15 13:42:43 +00:00
|
|
|
pub fn render_markdown(
|
|
|
|
&mut self,
|
|
|
|
permalinks: &HashMap<String, String>,
|
|
|
|
tera: &Tera,
|
|
|
|
config: &Config,
|
|
|
|
anchor_insert: InsertAnchor,
|
|
|
|
) -> Result<()> {
|
2018-10-31 07:18:57 +00:00
|
|
|
let mut context =
|
|
|
|
RenderContext::new(tera, config, &self.permalink, permalinks, anchor_insert);
|
2018-02-02 20:35:04 +00:00
|
|
|
|
2018-10-18 13:54:51 +00:00
|
|
|
context.tera_context.insert("page", &SerializingPage::from_page_basic(self, None));
|
2018-02-02 20:35:04 +00:00
|
|
|
|
2019-02-09 18:54:46 +00:00
|
|
|
let res = render_content(&self.raw_content, &context).map_err(|e| {
|
|
|
|
Error::chain(format!("Failed to render content of {}", self.file.path.display()), e)
|
|
|
|
})?;
|
2018-08-22 16:34:32 +00:00
|
|
|
|
|
|
|
self.summary = res.summary_len.map(|l| res.body[0..l].to_owned());
|
|
|
|
self.content = res.body;
|
|
|
|
self.toc = res.toc;
|
2019-05-27 12:05:07 +00:00
|
|
|
self.external_links = res.external_links;
|
2019-06-06 17:49:31 +00:00
|
|
|
self.internal_links_with_anchors = res.internal_links_with_anchors;
|
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
|
2018-10-02 14:42:34 +00:00
|
|
|
pub fn render_html(&self, tera: &Tera, config: &Config, library: &Library) -> Result<String> {
|
2017-03-10 12:36:43 +00:00
|
|
|
let tpl_name = match self.meta.template {
|
2018-11-07 19:37:25 +00:00
|
|
|
Some(ref l) => l,
|
|
|
|
None => "page.html",
|
2017-03-10 12:36:43 +00:00
|
|
|
};
|
2017-03-30 08:17:12 +00:00
|
|
|
|
2017-05-22 11:28:43 +00:00
|
|
|
let mut context = TeraContext::new();
|
2018-09-09 17:43:14 +00:00
|
|
|
context.insert("config", config);
|
|
|
|
context.insert("current_url", &self.permalink);
|
|
|
|
context.insert("current_path", &self.path);
|
2018-10-15 20:28:25 +00:00
|
|
|
context.insert("page", &self.to_serialized(library));
|
2018-12-28 12:24:49 +00:00
|
|
|
context.insert("lang", &self.lang);
|
2016-12-13 06:22:24 +00:00
|
|
|
|
2019-02-09 18:54:46 +00:00
|
|
|
render_template(&tpl_name, tera, context, &config.theme).map_err(|e| {
|
|
|
|
Error::chain(format!("Failed to render page '{}'", self.file.path.display()), e)
|
|
|
|
})
|
2016-12-11 06:05:03 +00:00
|
|
|
}
|
2018-02-02 20:35:04 +00:00
|
|
|
|
2018-06-23 14:38:53 +00:00
|
|
|
/// Creates a vectors of asset URLs.
|
2019-05-16 04:40:58 +00:00
|
|
|
fn serialize_assets(&self, base_path: &PathBuf) -> Vec<String> {
|
2018-10-31 07:18:57 +00:00
|
|
|
self.assets
|
|
|
|
.iter()
|
2018-06-23 14:38:53 +00:00
|
|
|
.filter_map(|asset| asset.file_name())
|
|
|
|
.filter_map(|filename| filename.to_str())
|
2019-05-16 04:40:58 +00:00
|
|
|
.map(|filename| {
|
|
|
|
let mut path = self.file.path.clone();
|
|
|
|
// Popping the index.md from the path since file.parent would be one level too high
|
|
|
|
// for our need here
|
|
|
|
path.pop();
|
|
|
|
path.push(filename);
|
2019-06-02 18:21:06 +00:00
|
|
|
path = path
|
|
|
|
.strip_prefix(&base_path.join("content"))
|
|
|
|
.expect("Should be able to stripe prefix")
|
|
|
|
.to_path_buf();
|
2019-05-16 04:40:58 +00:00
|
|
|
path
|
|
|
|
})
|
|
|
|
.map(|path| path.to_string_lossy().to_string())
|
2018-06-23 14:38:53 +00:00
|
|
|
.collect()
|
2018-02-02 20:35:04 +00:00
|
|
|
}
|
2018-10-02 14:42:34 +00:00
|
|
|
|
2019-06-06 17:49:31 +00:00
|
|
|
pub fn has_anchor(&self, anchor: &str) -> bool {
|
|
|
|
has_anchor(&self.toc, anchor)
|
|
|
|
}
|
|
|
|
|
2018-10-15 20:28:25 +00:00
|
|
|
pub fn to_serialized<'a>(&'a self, library: &'a Library) -> SerializingPage<'a> {
|
|
|
|
SerializingPage::from_page(self, library)
|
2018-10-02 14:42:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 13:54:51 +00:00
|
|
|
pub fn to_serialized_basic<'a>(&'a self, library: &'a Library) -> SerializingPage<'a> {
|
|
|
|
SerializingPage::from_page_basic(self, Some(library))
|
2018-10-02 14:42:34 +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(),
|
2018-10-18 13:54:51 +00:00
|
|
|
ancestors: vec![],
|
2017-05-13 04:01:38 +00:00
|
|
|
raw_content: "".to_string(),
|
|
|
|
assets: vec![],
|
2018-10-24 09:49:09 +00:00
|
|
|
serialized_assets: vec![],
|
2017-05-13 04:01:38 +00:00
|
|
|
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,
|
2018-07-28 02:20:20 +00:00
|
|
|
earlier: None,
|
|
|
|
later: None,
|
|
|
|
lighter: None,
|
|
|
|
heavier: None,
|
2017-06-16 04:00:48 +00:00
|
|
|
toc: vec![],
|
2018-09-20 16:27:56 +00:00
|
|
|
word_count: None,
|
|
|
|
reading_time: None,
|
2019-01-29 18:20:03 +00:00
|
|
|
lang: String::new(),
|
2018-12-29 10:17:43 +00:00
|
|
|
translations: Vec::new(),
|
2019-06-06 17:49:31 +00:00
|
|
|
internal_links_with_anchors: Vec::new(),
|
2019-05-27 12:05:07 +00:00
|
|
|
external_links: Vec::new(),
|
2017-05-13 04:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 07:56:16 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::collections::HashMap;
|
2018-10-31 07:18:57 +00:00
|
|
|
use std::fs::{create_dir, File};
|
2017-10-04 00:23:25 +00:00
|
|
|
use std::io::Write;
|
2019-03-08 22:26:57 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
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
|
|
|
use globset::{Glob, GlobSetBuilder};
|
2018-10-31 07:18:57 +00:00
|
|
|
use tempfile::tempdir;
|
|
|
|
use tera::Tera;
|
2017-05-15 07:56:16 +00:00
|
|
|
|
|
|
|
use super::Page;
|
2018-12-27 12:14:54 +00:00
|
|
|
use config::{Config, Language};
|
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"#;
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::parse(Path::new("post.md"), content, &Config::default(), &PathBuf::new());
|
2017-05-15 07:56:16 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let mut page = res.unwrap();
|
2018-08-15 13:42:43 +00:00
|
|
|
page.render_markdown(
|
|
|
|
&HashMap::default(),
|
|
|
|
&Tera::default(),
|
|
|
|
&Config::default(),
|
|
|
|
InsertAnchor::None,
|
2018-10-31 07:18:57 +00:00
|
|
|
)
|
|
|
|
.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();
|
2019-03-08 22:26:57 +00:00
|
|
|
let res =
|
|
|
|
Page::parse(Path::new("content/posts/intro/start.md"), content, &conf, &PathBuf::new());
|
2017-05-15 07:56:16 +00:00
|
|
|
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();
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::parse(Path::new("start.md"), content, &config, &PathBuf::new());
|
2017-05-15 07:56:16 +00:00
|
|
|
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"));
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:13:07 +00:00
|
|
|
#[test]
|
|
|
|
fn can_make_url_from_slug_only_with_no_special_chars() {
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
slug = "hello-&-world"
|
|
|
|
+++
|
|
|
|
Hello world"#;
|
|
|
|
let config = Config::default();
|
|
|
|
let res = Page::parse(Path::new("start.md"), content, &config, &PathBuf::new());
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
assert_eq!(page.path, "hello-world/");
|
|
|
|
assert_eq!(page.components, vec!["hello-world"]);
|
|
|
|
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();
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::parse(
|
|
|
|
Path::new("content/posts/intro/start.md"),
|
|
|
|
content,
|
|
|
|
&config,
|
|
|
|
&PathBuf::new(),
|
|
|
|
);
|
2017-10-04 00:35:37 +00:00
|
|
|
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();
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::parse(
|
|
|
|
Path::new("content/posts/intro/start.md"),
|
|
|
|
content,
|
|
|
|
&config,
|
|
|
|
&PathBuf::new(),
|
|
|
|
);
|
2017-10-04 00:35:37 +00:00
|
|
|
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"#;
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::parse(Path::new("start.md"), content, &Config::default(), &PathBuf::new());
|
2017-05-15 07:56:16 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_make_slug_from_non_slug_filename() {
|
|
|
|
let config = Config::default();
|
2019-03-08 22:26:57 +00:00
|
|
|
let res =
|
|
|
|
Page::parse(Path::new(" file with space.md"), "+++\n+++", &config, &PathBuf::new());
|
2017-05-15 07:56:16 +00:00
|
|
|
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
|
2018-10-31 07:18:57 +00:00
|
|
|
<!-- more -->"#
|
|
|
|
.to_string();
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::parse(Path::new("hello.md"), &content, &config, &PathBuf::new());
|
2017-05-15 07:56:16 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let mut page = res.unwrap();
|
2018-10-31 07:18:57 +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() {
|
2018-04-25 08:28:23 +00:00
|
|
|
let tmp_dir = tempdir().expect("create temp dir");
|
2017-05-15 07:56:16 +00:00
|
|
|
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();
|
|
|
|
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::from_file(
|
|
|
|
nested_path.join("index.md").as_path(),
|
|
|
|
&Config::default(),
|
2019-05-16 04:40:58 +00:00
|
|
|
&path.to_path_buf(),
|
2019-03-08 22:26:57 +00:00
|
|
|
);
|
2017-05-15 07:56:16 +00:00
|
|
|
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() {
|
2018-04-25 08:28:23 +00:00
|
|
|
let tmp_dir = tempdir().expect("create temp dir");
|
2017-10-04 00:23:25 +00:00
|
|
|
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();
|
|
|
|
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::from_file(
|
|
|
|
nested_path.join("index.md").as_path(),
|
|
|
|
&Config::default(),
|
2019-05-16 04:40:58 +00:00
|
|
|
&path.to_path_buf(),
|
2019-03-08 22:26:57 +00:00
|
|
|
);
|
2017-10-04 00:23:25 +00:00
|
|
|
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
|
|
|
|
2019-05-16 04:40:58 +00:00
|
|
|
// https://github.com/getzola/zola/issues/674
|
|
|
|
#[test]
|
|
|
|
fn page_with_assets_uses_filepath_for_assets() {
|
|
|
|
let tmp_dir = tempdir().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"+++\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(),
|
|
|
|
&path.to_path_buf(),
|
|
|
|
);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
assert_eq!(page.file.parent, path.join("content").join("posts"));
|
|
|
|
assert_eq!(page.assets.len(), 3);
|
|
|
|
assert_eq!(page.serialized_assets.len(), 3);
|
2019-05-17 14:35:18 +00:00
|
|
|
// We should not get with-assets since that's the slugified version
|
|
|
|
assert!(page.serialized_assets[0].contains("with_assets"));
|
2019-05-16 04:40:58 +00:00
|
|
|
assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/");
|
|
|
|
}
|
|
|
|
|
2019-02-09 18:54:46 +00:00
|
|
|
// https://github.com/getzola/zola/issues/607
|
|
|
|
#[test]
|
|
|
|
fn page_with_assets_and_date_in_folder_name() {
|
|
|
|
let tmp_dir = tempdir().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("2013-06-02_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"+++\n\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();
|
|
|
|
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::from_file(
|
|
|
|
nested_path.join("index.md").as_path(),
|
|
|
|
&Config::default(),
|
2019-05-16 04:40:58 +00:00
|
|
|
&path.to_path_buf(),
|
2019-03-08 22:26:57 +00:00
|
|
|
);
|
2019-02-09 18:54:46 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
assert_eq!(page.file.parent, path.join("content").join("posts"));
|
|
|
|
assert_eq!(page.slug, "with-assets");
|
|
|
|
assert_eq!(page.meta.date, Some("2013-06-02".to_string()));
|
|
|
|
assert_eq!(page.assets.len(), 3);
|
|
|
|
assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/");
|
|
|
|
}
|
|
|
|
|
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() {
|
2018-04-25 08:28:23 +00:00
|
|
|
let tmp_dir = tempdir().expect("create temp dir");
|
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
|
|
|
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();
|
2018-03-12 19:11:03 +00:00
|
|
|
config.ignored_content_globset = Some(gsb.build().unwrap());
|
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
|
|
|
|
2019-06-02 18:21:06 +00:00
|
|
|
let res =
|
|
|
|
Page::from_file(nested_path.join("index.md").as_path(), &config, &path.to_path_buf());
|
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
|
|
|
|
|
|
|
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"));
|
|
|
|
}
|
2018-10-25 14:22:00 +00:00
|
|
|
|
|
|
|
#[test]
|
2018-11-30 21:20:58 +00:00
|
|
|
fn can_get_date_from_short_date_in_filename() {
|
2018-10-25 14:22:00 +00:00
|
|
|
let config = Config::default();
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
+++
|
|
|
|
Hello world
|
2018-10-31 07:18:57 +00:00
|
|
|
<!-- more -->"#
|
|
|
|
.to_string();
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::parse(Path::new("2018-10-08_hello.md"), &content, &config, &PathBuf::new());
|
2018-10-25 14:22:00 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(page.meta.date, Some("2018-10-08".to_string()));
|
|
|
|
assert_eq!(page.slug, "hello");
|
|
|
|
}
|
|
|
|
|
2018-11-30 21:20:58 +00:00
|
|
|
#[test]
|
|
|
|
fn can_get_date_from_full_rfc3339_date_in_filename() {
|
|
|
|
let config = Config::default();
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
+++
|
|
|
|
Hello world
|
|
|
|
<!-- more -->"#
|
|
|
|
.to_string();
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::parse(
|
|
|
|
Path::new("2018-10-02T15:00:00Z-hello.md"),
|
|
|
|
&content,
|
|
|
|
&config,
|
|
|
|
&PathBuf::new(),
|
|
|
|
);
|
2018-11-30 21:20:58 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(page.meta.date, Some("2018-10-02T15:00:00Z".to_string()));
|
|
|
|
assert_eq!(page.slug, "hello");
|
|
|
|
}
|
|
|
|
|
2018-10-25 14:22:00 +00:00
|
|
|
#[test]
|
|
|
|
fn frontmatter_date_override_filename_date() {
|
|
|
|
let config = Config::default();
|
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
date = 2018-09-09
|
|
|
|
+++
|
|
|
|
Hello world
|
2018-10-31 07:18:57 +00:00
|
|
|
<!-- more -->"#
|
|
|
|
.to_string();
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::parse(Path::new("2018-10-08_hello.md"), &content, &config, &PathBuf::new());
|
2018-10-25 14:22:00 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(page.meta.date, Some("2018-09-09".to_string()));
|
|
|
|
assert_eq!(page.slug, "hello");
|
|
|
|
}
|
2018-12-27 12:14:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_specify_language_in_filename() {
|
|
|
|
let mut config = Config::default();
|
2019-09-03 14:50:23 +00:00
|
|
|
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
|
2018-12-27 12:14:54 +00:00
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
+++
|
|
|
|
Bonjour le monde"#
|
|
|
|
.to_string();
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::parse(Path::new("hello.fr.md"), &content, &config, &PathBuf::new());
|
2018-12-27 12:14:54 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
2019-01-29 18:20:03 +00:00
|
|
|
assert_eq!(page.lang, "fr".to_string());
|
2018-12-28 09:42:26 +00:00
|
|
|
assert_eq!(page.slug, "hello");
|
|
|
|
assert_eq!(page.permalink, "http://a-website.com/fr/hello/");
|
2018-12-27 12:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_specify_language_in_filename_with_date() {
|
|
|
|
let mut config = Config::default();
|
2019-09-03 14:50:23 +00:00
|
|
|
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
|
2018-12-27 12:14:54 +00:00
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
+++
|
|
|
|
Bonjour le monde"#
|
|
|
|
.to_string();
|
2019-03-08 22:26:57 +00:00
|
|
|
let res =
|
|
|
|
Page::parse(Path::new("2018-10-08_hello.fr.md"), &content, &config, &PathBuf::new());
|
2018-12-27 12:14:54 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
|
|
|
assert_eq!(page.meta.date, Some("2018-10-08".to_string()));
|
2019-01-29 18:20:03 +00:00
|
|
|
assert_eq!(page.lang, "fr".to_string());
|
2018-12-28 09:42:26 +00:00
|
|
|
assert_eq!(page.slug, "hello");
|
|
|
|
assert_eq!(page.permalink, "http://a-website.com/fr/hello/");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn i18n_frontmatter_path_overrides_default_permalink() {
|
|
|
|
let mut config = Config::default();
|
2019-09-03 14:50:23 +00:00
|
|
|
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
|
2018-12-28 09:42:26 +00:00
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
path = "bonjour"
|
|
|
|
+++
|
|
|
|
Bonjour le monde"#
|
|
|
|
.to_string();
|
2019-03-08 22:26:57 +00:00
|
|
|
let res = Page::parse(Path::new("hello.fr.md"), &content, &config, &PathBuf::new());
|
2018-12-28 09:42:26 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let page = res.unwrap();
|
2019-01-29 18:20:03 +00:00
|
|
|
assert_eq!(page.lang, "fr".to_string());
|
2018-12-28 09:42:26 +00:00
|
|
|
assert_eq!(page.slug, "hello");
|
|
|
|
assert_eq!(page.permalink, "http://a-website.com/bonjour/");
|
2018-12-27 12:14:54 +00:00
|
|
|
}
|
2017-05-15 07:56:16 +00:00
|
|
|
}
|