zola/components/content/src/section.rs

330 lines
12 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::result::Result as StdResult;
use tera::{Tera, Context as TeraContext};
use serde::ser::{SerializeStruct, self};
use config::Config;
use front_matter::{SectionFrontMatter, split_section_content};
use errors::{Result, ResultExt};
2018-08-07 10:12:12 +00:00
use utils::fs::{read_file, find_related_assets};
2017-08-23 10:17:24 +00:00
use utils::templates::render_template;
use utils::site::get_reading_analytics;
2018-05-06 20:58:39 +00:00
use rendering::{RenderContext, Header, render_content};
2017-07-01 07:47:41 +00:00
use page::Page;
use file_info::FileInfo;
#[derive(Clone, Debug, PartialEq)]
pub struct Section {
/// All info about the actual file
pub file: FileInfo,
/// The front matter meta-data
pub meta: SectionFrontMatter,
/// The URL path of the page
pub path: String,
2017-10-31 15:41:31 +00:00
/// The components for the path of that page
pub components: Vec<String>,
/// The full URL for that page
pub permalink: String,
2017-05-12 09:05:00 +00:00
/// The actual content of the page, in markdown
pub raw_content: String,
/// The HTML rendered of the page
pub content: String,
2018-08-07 10:12:12 +00:00
/// All the non-md files we found next to the .md file
pub assets: Vec<PathBuf>,
/// All direct pages of that section
pub pages: Vec<Page>,
2017-05-08 10:29:37 +00:00
/// All pages that cannot be sorted in this section
pub ignored_pages: Vec<Page>,
/// All direct subsections
pub subsections: Vec<Section>,
2017-06-16 04:00:48 +00:00
/// Toc made from the headers of the markdown file
pub toc: Vec<Header>,
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>,
}
impl Section {
pub fn new<P: AsRef<Path>>(file_path: P, meta: SectionFrontMatter) -> Section {
2017-05-03 08:52:49 +00:00
let file_path = file_path.as_ref();
Section {
file: FileInfo::new_section(file_path),
2017-10-31 15:41:31 +00:00
meta,
path: "".to_string(),
2017-10-31 15:41:31 +00:00
components: vec![],
permalink: "".to_string(),
2017-05-12 09:05:00 +00:00
raw_content: "".to_string(),
2018-08-07 10:12:12 +00:00
assets: vec![],
2017-05-12 09:05:00 +00:00
content: "".to_string(),
pages: vec![],
2017-05-08 10:29:37 +00:00
ignored_pages: vec![],
subsections: vec![],
2017-06-16 04:00:48 +00:00
toc: vec![],
2018-09-20 16:27:56 +00:00
word_count: None,
reading_time: None,
}
}
pub fn parse(file_path: &Path, content: &str, config: &Config) -> Result<Section> {
let (meta, content) = split_section_content(file_path, content)?;
let mut section = Section::new(file_path, meta);
2017-05-12 09:05:00 +00:00
section.raw_content = content.clone();
2018-09-20 16:27:56 +00:00
let (word_count, reading_time) = get_reading_analytics(&section.raw_content);
section.word_count = Some(word_count);
section.reading_time = Some(reading_time);
section.path = format!("{}/", section.file.components.join("/"));
2017-10-31 15:41:31 +00:00
section.components = section.path.split('/')
.map(|p| p.to_string())
.filter(|p| !p.is_empty())
.collect::<Vec<_>>();
section.permalink = config.make_permalink(&section.path);
Ok(section)
}
/// Read and parse a .md file into a Page struct
pub fn from_file<P: AsRef<Path>>(path: P, config: &Config) -> Result<Section> {
let path = path.as_ref();
let content = read_file(path)?;
2018-08-07 10:12:12 +00:00
let mut section = Section::parse(path, &content, config)?;
let parent_dir = path.parent().unwrap();
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).
section.assets = assets.into_iter()
.filter(|path|
match path.file_name() {
None => true,
Some(file) => !globset.is_match(file)
}
).collect();
2018-08-07 10:12:12 +00:00
} else {
section.assets = assets;
2018-08-07 10:12:12 +00:00
}
Ok(section)
}
2017-05-03 08:52:49 +00:00
pub fn get_template_name(&self) -> String {
match self.meta.template {
Some(ref l) => l.to_string(),
None => {
if self.is_index() {
return "index.html".to_string();
}
"section.html".to_string()
2018-07-31 13:17:31 +00:00
}
2017-05-03 08:52:49 +00:00
}
}
2017-05-12 09:05:00 +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, base_path: &Path) -> Result<()> {
2018-02-02 20:35:04 +00:00
let mut context = RenderContext::new(
2017-07-01 07:47:41 +00:00
tera,
2018-05-06 20:58:39 +00:00
config,
2017-07-01 07:47:41 +00:00
&self.permalink,
permalinks,
base_path,
2018-03-14 17:22:24 +00:00
self.meta.insert_anchor_links,
2017-07-01 07:47:41 +00:00
);
2018-02-02 20:35:04 +00:00
2018-09-09 17:43:14 +00:00
context.tera_context.insert("section", self);
2018-02-02 20:35:04 +00:00
let res = render_content(&self.raw_content, &context)
.chain_err(|| format!("Failed to render content of {}", self.file.path.display()))?;
self.content = res.body;
self.toc = res.toc;
2017-05-12 09:05:00 +00:00
Ok(())
}
/// Renders the page using the default layout, unless specified in front-matter
pub fn render_html(&self, tera: &Tera, config: &Config) -> Result<String> {
2017-05-03 08:52:49 +00:00
let tpl_name = self.get_template_name();
let mut context = TeraContext::new();
2018-09-09 17:43:14 +00:00
context.insert("config", config);
context.insert("section", self);
context.insert("current_url", &self.permalink);
context.insert("current_path", &self.path);
render_template(&tpl_name, tera, &context, &config.theme)
.chain_err(|| format!("Failed to render section '{}'", self.file.path.display()))
}
2017-05-03 08:52:49 +00:00
2017-05-08 10:29:37 +00:00
/// Is this the index section?
2017-05-03 08:52:49 +00:00
pub fn is_index(&self) -> bool {
self.file.components.is_empty()
2017-05-03 08:52:49 +00:00
}
2017-05-08 10:29:37 +00:00
/// Returns all the paths of the pages belonging to that section
2017-05-08 10:29:37 +00:00
pub fn all_pages_path(&self) -> Vec<PathBuf> {
let mut paths = vec![];
paths.extend(self.pages.iter().map(|p| p.file.path.clone()));
paths.extend(self.ignored_pages.iter().map(|p| p.file.path.clone()));
2017-05-08 10:29:37 +00:00
paths
}
/// Whether the page given belongs to that section
pub fn is_child_page(&self, path: &PathBuf) -> bool {
self.all_pages_path().contains(path)
}
2018-08-07 10:12:12 +00:00
/// Creates a vectors of asset URLs.
fn serialize_assets(&self) -> Vec<String> {
self.assets.iter()
.filter_map(|asset| asset.file_name())
.filter_map(|filename| filename.to_str())
.map(|filename| self.path.clone() + filename)
.collect()
}
pub fn clone_without_pages(&self) -> Section {
let mut subsections = vec![];
for subsection in &self.subsections {
subsections.push(subsection.clone_without_pages());
}
Section {
file: self.file.clone(),
meta: self.meta.clone(),
path: self.path.clone(),
components: self.components.clone(),
permalink: self.permalink.clone(),
raw_content: self.raw_content.clone(),
content: self.content.clone(),
assets: self.assets.clone(),
toc: self.toc.clone(),
subsections,
pages: vec![],
ignored_pages: vec![],
2018-09-30 19:15:09 +00:00
word_count: self.word_count,
reading_time: self.reading_time,
}
}
}
impl ser::Serialize for Section {
fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> where S: ser::Serializer {
2017-10-31 15:41:31 +00:00
let mut state = serializer.serialize_struct("section", 13)?;
2017-05-12 09:05:00 +00:00
state.serialize_field("content", &self.content)?;
state.serialize_field("permalink", &self.permalink)?;
state.serialize_field("title", &self.meta.title)?;
state.serialize_field("description", &self.meta.description)?;
state.serialize_field("extra", &self.meta.extra)?;
state.serialize_field("path", &self.path)?;
2017-10-31 15:41:31 +00:00
state.serialize_field("components", &self.components)?;
state.serialize_field("permalink", &self.permalink)?;
2017-05-08 10:29:37 +00:00
state.serialize_field("pages", &self.pages)?;
state.serialize_field("subsections", &self.subsections)?;
2018-09-20 16:27:56 +00:00
state.serialize_field("word_count", &self.word_count)?;
state.serialize_field("reading_time", &self.reading_time)?;
2017-06-16 04:00:48 +00:00
state.serialize_field("toc", &self.toc)?;
2018-08-07 10:12:12 +00:00
let assets = self.serialize_assets();
state.serialize_field("assets", &assets)?;
state.end()
}
}
/// Used to create a default index section if there is no _index.md in the root content directory
impl Default for Section {
fn default() -> Section {
Section {
file: FileInfo::default(),
meta: SectionFrontMatter::default(),
path: "".to_string(),
2017-10-31 15:41:31 +00:00
components: vec![],
permalink: "".to_string(),
2017-05-12 09:05:00 +00:00
raw_content: "".to_string(),
2018-08-07 10:12:12 +00:00
assets: vec![],
2017-05-12 09:05:00 +00:00
content: "".to_string(),
pages: vec![],
ignored_pages: vec![],
subsections: vec![],
2017-06-16 04:00:48 +00:00
toc: vec![],
2018-09-20 16:27:56 +00:00
reading_time: None,
word_count: None,
}
}
}
2018-08-07 10:12:12 +00:00
#[cfg(test)]
mod tests {
use std::io::Write;
use std::fs::{File, create_dir};
use tempfile::tempdir;
use globset::{Glob, GlobSetBuilder};
use config::Config;
use super::Section;
#[test]
fn section_with_assets_gets_right_info() {
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 = Section::from_file(
nested_path.join("_index.md").as_path(),
&Config::default(),
);
assert!(res.is_ok());
let section = res.unwrap();
assert_eq!(section.assets.len(), 3);
assert_eq!(section.permalink, "http://a-website.com/posts/with-assets/");
}
#[test]
fn section_with_ignored_assets_filters_out_correct_files() {
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"+++\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_globset = Some(gsb.build().unwrap());
let res = Section::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"));
}
}