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};
|
|
|
|
use std::result::Result as StdResult;
|
|
|
|
|
2017-05-22 11:28:43 +00:00
|
|
|
use tera::{Tera, Context as TeraContext};
|
2017-03-14 12:25:45 +00:00
|
|
|
use serde::ser::{SerializeStruct, self};
|
|
|
|
|
|
|
|
use config::Config;
|
2017-05-13 04:01:38 +00:00
|
|
|
use front_matter::{SectionFrontMatter, split_section_content};
|
2017-03-14 12:25:45 +00:00
|
|
|
use errors::{Result, ResultExt};
|
2017-05-16 04:37:00 +00:00
|
|
|
use fs::{read_file};
|
2017-05-17 12:53:26 +00:00
|
|
|
use rendering::markdown::markdown_to_html;
|
2017-05-22 11:28:43 +00:00
|
|
|
use rendering::context::Context;
|
2017-05-14 05:14:58 +00:00
|
|
|
use content::Page;
|
2017-05-15 10:53:39 +00:00
|
|
|
use content::file_info::FileInfo;
|
2017-06-16 04:00:48 +00:00
|
|
|
use content::Header;
|
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-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,
|
2017-03-14 12:25:45 +00:00
|
|
|
/// 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>,
|
2017-03-14 12:25:45 +00:00
|
|
|
/// 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>,
|
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-05-13 04:01:38 +00:00
|
|
|
meta: meta,
|
2017-03-30 08:17:12 +00:00
|
|
|
path: "".to_string(),
|
2017-03-14 12:25:45 +00:00
|
|
|
permalink: "".to_string(),
|
2017-05-12 09:05:00 +00:00
|
|
|
raw_content: "".to_string(),
|
|
|
|
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![],
|
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);
|
2017-05-12 09:05:00 +00:00
|
|
|
section.raw_content = content.clone();
|
2017-06-10 17:52:39 +00:00
|
|
|
section.path = format!("{}/", section.file.components.join("/"));
|
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)?;
|
|
|
|
|
|
|
|
Section::parse(path, &content, config)
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) -> Result<()> {
|
2017-06-16 04:00:48 +00:00
|
|
|
let context = Context::new(tera, config, &self.permalink, permalinks, self.meta.insert_anchor.unwrap());
|
|
|
|
let res = markdown_to_html(&self.raw_content, &context)?;
|
|
|
|
self.content = res.0;
|
|
|
|
self.toc = res.1;
|
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
|
2017-05-12 13:32:35 +00:00
|
|
|
pub fn render_html(&self, sections: HashMap<String, Section>, tera: &Tera, config: &Config) -> 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();
|
2017-03-14 12:25:45 +00:00
|
|
|
context.add("config", config);
|
|
|
|
context.add("section", self);
|
2017-03-30 08:17:12 +00:00
|
|
|
context.add("current_url", &self.permalink);
|
|
|
|
context.add("current_path", &self.path);
|
2017-05-08 10:29:37 +00:00
|
|
|
if self.is_index() {
|
2017-05-12 13:32:35 +00:00
|
|
|
context.add("sections", §ions);
|
2017-05-08 10:29:37 +00:00
|
|
|
}
|
2017-03-14 12:25:45 +00:00
|
|
|
|
|
|
|
tera.render(&tpl_name, &context)
|
2017-05-15 10:53:39 +00:00
|
|
|
.chain_err(|| format!("Failed to render section '{}'", self.file.path.display()))
|
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
|
|
|
|
2017-05-15 10:53:39 +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![];
|
2017-05-15 10:53:39 +00:00
|
|
|
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
|
|
|
|
}
|
2017-05-13 13:37:01 +00:00
|
|
|
|
|
|
|
/// Whether the page given belongs to that section
|
2017-05-22 11:28:43 +00:00
|
|
|
pub fn is_child_page(&self, path: &PathBuf) -> bool {
|
|
|
|
self.all_pages_path().contains(path)
|
2017-05-13 13:37:01 +00:00
|
|
|
}
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ser::Serialize for Section {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> where S: ser::Serializer {
|
2017-06-16 04:00:48 +00:00
|
|
|
let mut state = serializer.serialize_struct("section", 10)?;
|
2017-05-12 09:05:00 +00:00
|
|
|
state.serialize_field("content", &self.content)?;
|
2017-05-15 10:53:39 +00:00
|
|
|
state.serialize_field("permalink", &self.permalink)?;
|
2017-03-14 12:25:45 +00:00
|
|
|
state.serialize_field("title", &self.meta.title)?;
|
|
|
|
state.serialize_field("description", &self.meta.description)?;
|
2017-05-15 10:53:39 +00:00
|
|
|
state.serialize_field("extra", &self.meta.extra)?;
|
2017-03-30 08:17:12 +00:00
|
|
|
state.serialize_field("path", &format!("/{}", self.path))?;
|
2017-03-14 12:25:45 +00:00
|
|
|
state.serialize_field("permalink", &self.permalink)?;
|
2017-05-08 10:29:37 +00:00
|
|
|
state.serialize_field("pages", &self.pages)?;
|
2017-03-14 12:25:45 +00:00
|
|
|
state.serialize_field("subsections", &self.subsections)?;
|
2017-06-16 04:00:48 +00:00
|
|
|
state.serialize_field("toc", &self.toc)?;
|
2017-03-14 12:25:45 +00:00
|
|
|
state.end()
|
|
|
|
}
|
|
|
|
}
|
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(),
|
2017-05-12 07:30:01 +00:00
|
|
|
path: "".to_string(),
|
|
|
|
permalink: "".to_string(),
|
2017-05-12 09:05:00 +00:00
|
|
|
raw_content: "".to_string(),
|
|
|
|
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![],
|
2017-05-12 07:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|