2017-05-09 11:24:44 +00:00
|
|
|
use std::collections::HashMap;
|
2017-03-14 12:25:45 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2018-10-24 09:40:57 +00:00
|
|
|
use slotmap::Key;
|
2018-10-31 07:18:57 +00:00
|
|
|
use tera::{Context as TeraContext, Tera};
|
2017-03-14 12:25:45 +00:00
|
|
|
|
|
|
|
use config::Config;
|
2019-01-11 19:29:46 +00:00
|
|
|
use errors::{Result, Error};
|
2018-10-31 07:18:57 +00:00
|
|
|
use front_matter::{split_section_content, SectionFrontMatter};
|
|
|
|
use rendering::{render_content, Header, RenderContext};
|
|
|
|
use utils::fs::{find_related_assets, read_file};
|
2017-09-27 14:37:17 +00:00
|
|
|
use utils::site::get_reading_analytics;
|
2018-10-31 07:18:57 +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;
|
2018-10-24 09:40:57 +00:00
|
|
|
use content::ser::SerializingSection;
|
2018-10-02 14:42:34 +00:00
|
|
|
use library::Library;
|
2017-03-14 12:25:45 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub struct Section {
|
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: SectionFrontMatter,
|
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 for the path of that page
|
|
|
|
pub components: Vec<String>,
|
2017-03-14 12:25:45 +00:00
|
|
|
/// 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>,
|
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-03-14 12:25:45 +00:00
|
|
|
/// All direct pages of that section
|
2018-10-02 14:42:34 +00:00
|
|
|
pub pages: Vec<Key>,
|
2017-05-08 10:29:37 +00:00
|
|
|
/// All pages that cannot be sorted in this section
|
2018-10-02 14:42:34 +00:00
|
|
|
pub ignored_pages: Vec<Key>,
|
2018-10-18 13:54:51 +00:00
|
|
|
/// The list of parent sections
|
|
|
|
pub ancestors: Vec<Key>,
|
2017-03-14 12:25:45 +00:00
|
|
|
/// All direct subsections
|
2018-10-02 14:42:34 +00:00
|
|
|
pub subsections: Vec<Key>,
|
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>,
|
2018-12-27 12:14:54 +00:00
|
|
|
/// The language of that section. `None` if the user doesn't setup `languages` in config.
|
|
|
|
/// Corresponds to the lang in the _index.{lang}.md file scheme
|
|
|
|
pub lang: Option<String>,
|
2018-12-29 10:17:43 +00:00
|
|
|
/// Contains all the translated version of that section
|
|
|
|
pub translations: Vec<Key>,
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Section {
|
2017-05-13 04:01:38 +00:00
|
|
|
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();
|
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
Section {
|
2017-05-15 10:53:39 +00:00
|
|
|
file: FileInfo::new_section(file_path),
|
2017-10-31 15:41:31 +00:00
|
|
|
meta,
|
2018-10-18 13:54:51 +00:00
|
|
|
ancestors: vec![],
|
2017-03-30 08:17:12 +00:00
|
|
|
path: "".to_string(),
|
2017-10-31 15:41:31 +00:00
|
|
|
components: vec![],
|
2017-03-14 12:25:45 +00:00
|
|
|
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![],
|
2018-10-24 09:49:09 +00:00
|
|
|
serialized_assets: vec![],
|
2017-05-12 09:05:00 +00:00
|
|
|
content: "".to_string(),
|
2017-03-14 12:25:45 +00:00
|
|
|
pages: vec![],
|
2017-05-08 10:29:37 +00:00
|
|
|
ignored_pages: vec![],
|
2017-03-14 12:25:45 +00:00
|
|
|
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,
|
2018-12-27 12:14:54 +00:00
|
|
|
lang: None,
|
2018-12-29 10:17:43 +00:00
|
|
|
translations: Vec::new(),
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse(file_path: &Path, content: &str, config: &Config) -> Result<Section> {
|
2017-05-13 04:01:38 +00:00
|
|
|
let (meta, content) = split_section_content(file_path, content)?;
|
2017-03-14 12:25:45 +00:00
|
|
|
let mut section = Section::new(file_path, meta);
|
2018-12-27 12:14:54 +00:00
|
|
|
section.lang = section.file.find_language(config)?;
|
2018-11-19 14:04:22 +00:00
|
|
|
section.raw_content = content;
|
2018-09-20 16:27:56 +00:00
|
|
|
let (word_count, reading_time) = get_reading_analytics(§ion.raw_content);
|
|
|
|
section.word_count = Some(word_count);
|
|
|
|
section.reading_time = Some(reading_time);
|
2018-12-28 09:42:26 +00:00
|
|
|
let path = format!("{}/", section.file.components.join("/"));
|
|
|
|
if let Some(ref lang) = section.lang {
|
|
|
|
section.path = format!("{}/{}", lang, path);
|
|
|
|
} else {
|
|
|
|
section.path = path;
|
|
|
|
}
|
2018-10-31 07:18:57 +00:00
|
|
|
section.components = section
|
|
|
|
.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
|
|
|
section.permalink = config.make_permalink(§ion.path);
|
2017-03-14 12:25:45 +00:00
|
|
|
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)?;
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2018-08-09 09:58:09 +00:00
|
|
|
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).
|
2018-10-31 07:18:57 +00:00
|
|
|
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 {
|
2018-08-09 09:58:09 +00:00
|
|
|
section.assets = assets;
|
2018-08-07 10:12:12 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 09:49:09 +00:00
|
|
|
section.serialized_assets = section.serialize_assets();
|
|
|
|
|
2018-08-07 10:12:12 +00:00
|
|
|
Ok(section)
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 19:26:33 +00:00
|
|
|
pub fn get_template_name(&self) -> &str {
|
2017-05-03 08:52:49 +00:00
|
|
|
match self.meta.template {
|
2018-11-07 19:26:33 +00:00
|
|
|
Some(ref l) => l,
|
2017-05-03 08:52:49 +00:00
|
|
|
None => {
|
|
|
|
if self.is_index() {
|
2018-11-14 16:34:21 +00:00
|
|
|
return "index.html";
|
2017-05-03 08:52:49 +00:00
|
|
|
}
|
2018-11-07 19:26:33 +00:00
|
|
|
"section.html"
|
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
|
2018-10-02 14:42:34 +00:00
|
|
|
pub fn render_markdown(
|
|
|
|
&mut self,
|
|
|
|
permalinks: &HashMap<String, String>,
|
|
|
|
tera: &Tera,
|
|
|
|
config: &Config,
|
|
|
|
) -> 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,
|
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-10-18 13:54:51 +00:00
|
|
|
context.tera_context.insert("section", &SerializingSection::from_section_basic(self, None));
|
2018-02-02 20:35:04 +00:00
|
|
|
|
2018-05-17 16:32:31 +00:00
|
|
|
let res = render_content(&self.raw_content, &context)
|
2019-01-11 19:29:46 +00:00
|
|
|
.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.content = res.body;
|
|
|
|
self.toc = res.toc;
|
2017-05-12 09:05:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:25:45 +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-05-03 08:52:49 +00:00
|
|
|
let tpl_name = self.get_template_name();
|
2017-03-14 12:25:45 +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-05 17:34:27 +00:00
|
|
|
context.insert("section", &self.to_serialized(library));
|
2018-12-28 12:24:49 +00:00
|
|
|
context.insert("lang", &self.lang);
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2018-11-07 19:26:33 +00:00
|
|
|
render_template(tpl_name, tera, &context, &config.theme)
|
2019-01-11 19:29:46 +00:00
|
|
|
.map_err(|e| Error::chain(format!("Failed to render section '{}'", self.file.path.display()), e))
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
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 {
|
2017-05-15 10:53:39 +00:00
|
|
|
self.file.components.is_empty()
|
2017-05-03 08:52:49 +00:00
|
|
|
}
|
2017-05-08 10:29:37 +00:00
|
|
|
|
2018-08-07 10:12:12 +00:00
|
|
|
/// Creates a vectors of asset URLs.
|
2018-10-24 09:49:09 +00:00
|
|
|
fn serialize_assets(&self) -> Vec<String> {
|
2018-10-31 07:18:57 +00:00
|
|
|
self.assets
|
|
|
|
.iter()
|
2018-08-07 10:12:12 +00:00
|
|
|
.filter_map(|asset| asset.file_name())
|
|
|
|
.filter_map(|filename| filename.to_str())
|
|
|
|
.map(|filename| self.path.clone() + filename)
|
|
|
|
.collect()
|
|
|
|
}
|
2018-09-19 07:24:35 +00:00
|
|
|
|
2018-10-02 14:42:34 +00:00
|
|
|
pub fn to_serialized<'a>(&'a self, library: &'a Library) -> SerializingSection<'a> {
|
|
|
|
SerializingSection::from_section(self, library)
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
2018-10-18 14:49:33 +00:00
|
|
|
|
|
|
|
pub fn to_serialized_basic<'a>(&'a self, library: &'a Library) -> SerializingSection<'a> {
|
|
|
|
SerializingSection::from_section_basic(self, Some(library))
|
|
|
|
}
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
2017-05-12 07:30:01 +00:00
|
|
|
|
2017-05-15 10:53:39 +00:00
|
|
|
/// Used to create a default index section if there is no _index.md in the root content directory
|
2017-05-12 07:30:01 +00:00
|
|
|
impl Default for Section {
|
|
|
|
fn default() -> Section {
|
|
|
|
Section {
|
2017-05-15 10:53:39 +00:00
|
|
|
file: FileInfo::default(),
|
2017-05-13 04:01:38 +00:00
|
|
|
meta: SectionFrontMatter::default(),
|
2018-10-18 13:54:51 +00:00
|
|
|
ancestors: vec![],
|
2017-05-12 07:30:01 +00:00
|
|
|
path: "".to_string(),
|
2017-10-31 15:41:31 +00:00
|
|
|
components: vec![],
|
2017-05-12 07:30:01 +00:00
|
|
|
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![],
|
2018-10-24 09:49:09 +00:00
|
|
|
serialized_assets: vec![],
|
2017-05-12 09:05:00 +00:00
|
|
|
content: "".to_string(),
|
2017-05-12 07:30:01 +00:00
|
|
|
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-12-27 12:14:54 +00:00
|
|
|
lang: None,
|
2018-12-29 10:17:43 +00:00
|
|
|
translations: Vec::new(),
|
2017-05-12 07:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-07 10:12:12 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-10-31 07:18:57 +00:00
|
|
|
use std::fs::{create_dir, File};
|
2018-08-07 10:12:12 +00:00
|
|
|
use std::io::Write;
|
2018-12-27 12:14:54 +00:00
|
|
|
use std::path::Path;
|
2018-08-07 10:12:12 +00:00
|
|
|
|
|
|
|
use globset::{Glob, GlobSetBuilder};
|
2018-10-31 07:18:57 +00:00
|
|
|
use tempfile::tempdir;
|
2018-08-07 10:12:12 +00:00
|
|
|
|
|
|
|
use super::Section;
|
2018-12-27 12:14:54 +00:00
|
|
|
use config::{Config, Language};
|
2018-08-07 10:12:12 +00:00
|
|
|
|
|
|
|
#[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();
|
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
let res = Section::from_file(nested_path.join("_index.md").as_path(), &Config::default());
|
2018-08-07 10:12:12 +00:00
|
|
|
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());
|
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
let res = Section::from_file(nested_path.join("_index.md").as_path(), &config);
|
2018-08-07 10:12:12 +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-12-27 12:14:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_specify_language_in_filename() {
|
|
|
|
let mut config = Config::default();
|
2018-12-29 10:17:43 +00:00
|
|
|
config.languages.push(Language { code: String::from("fr"), rss: false });
|
2018-12-27 12:14:54 +00:00
|
|
|
let content = r#"
|
|
|
|
+++
|
|
|
|
+++
|
|
|
|
Bonjour le monde"#
|
|
|
|
.to_string();
|
2018-12-28 09:42:26 +00:00
|
|
|
let res = Section::parse(Path::new("content/hello/nested/_index.fr.md"), &content, &config);
|
2018-12-27 12:14:54 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let section = res.unwrap();
|
|
|
|
assert_eq!(section.lang, Some("fr".to_string()));
|
2018-12-28 09:42:26 +00:00
|
|
|
assert_eq!(section.permalink, "http://a-website.com/fr/hello/nested/");
|
2018-12-27 12:14:54 +00:00
|
|
|
}
|
2018-08-07 10:12:12 +00:00
|
|
|
}
|