2017-03-14 12:25:45 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::result::Result as StdResult;
|
|
|
|
|
|
|
|
use tera::{Tera, Context};
|
|
|
|
use serde::ser::{SerializeStruct, self};
|
|
|
|
|
|
|
|
use config::Config;
|
|
|
|
use front_matter::{FrontMatter, split_content};
|
|
|
|
use errors::{Result, ResultExt};
|
|
|
|
use utils::{read_file, find_content_components};
|
2017-05-03 08:52:49 +00:00
|
|
|
use page::{Page, sort_pages};
|
2017-03-14 12:25:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub struct Section {
|
|
|
|
/// The _index.md full 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
|
|
|
/// Path of the directory containing the _index.md file
|
|
|
|
pub parent_path: PathBuf,
|
|
|
|
/// The folder names from `content` to this section file
|
|
|
|
pub components: Vec<String>,
|
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,
|
|
|
|
/// The front matter meta-data
|
|
|
|
pub meta: FrontMatter,
|
|
|
|
/// All direct pages of that section
|
|
|
|
pub pages: Vec<Page>,
|
|
|
|
/// All direct subsections
|
|
|
|
pub subsections: Vec<Section>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Section {
|
2017-05-03 08:52:49 +00:00
|
|
|
pub fn new<P: AsRef<Path>>(file_path: P, meta: FrontMatter) -> Section {
|
|
|
|
let file_path = file_path.as_ref();
|
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
Section {
|
|
|
|
file_path: file_path.to_path_buf(),
|
2017-03-27 14:17:33 +00:00
|
|
|
relative_path: "".to_string(),
|
2017-03-14 12:25:45 +00:00
|
|
|
parent_path: file_path.parent().unwrap().to_path_buf(),
|
|
|
|
components: vec![],
|
2017-03-30 08:17:12 +00:00
|
|
|
path: "".to_string(),
|
2017-03-14 12:25:45 +00:00
|
|
|
permalink: "".to_string(),
|
|
|
|
meta: meta,
|
|
|
|
pages: vec![],
|
|
|
|
subsections: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse(file_path: &Path, content: &str, config: &Config) -> Result<Section> {
|
|
|
|
let (meta, _) = split_content(file_path, content)?;
|
|
|
|
let mut section = Section::new(file_path, meta);
|
|
|
|
section.components = find_content_components(§ion.file_path);
|
2017-03-30 08:17:12 +00:00
|
|
|
section.path = section.components.join("/");
|
|
|
|
section.permalink = config.make_permalink(§ion.path);
|
2017-05-08 08:39:31 +00:00
|
|
|
if section.components.is_empty() {
|
2017-05-03 08:52:49 +00:00
|
|
|
section.relative_path = "_index.md".to_string();
|
|
|
|
} else {
|
|
|
|
section.relative_path = format!("{}/_index.md", section.components.join("/"));
|
|
|
|
}
|
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-03-14 12:25:45 +00:00
|
|
|
/// 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();
|
2017-03-14 12:25:45 +00:00
|
|
|
|
|
|
|
let mut context = Context::new();
|
|
|
|
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-03-14 12:25:45 +00:00
|
|
|
|
|
|
|
tera.render(&tpl_name, &context)
|
|
|
|
.chain_err(|| format!("Failed to render section '{}'", self.file_path.display()))
|
|
|
|
}
|
2017-05-03 08:52:49 +00:00
|
|
|
|
|
|
|
pub fn is_index(&self) -> bool {
|
2017-05-08 08:39:31 +00:00
|
|
|
self.components.is_empty()
|
2017-05-03 08:52:49 +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 {
|
|
|
|
let mut state = serializer.serialize_struct("section", 6)?;
|
|
|
|
state.serialize_field("title", &self.meta.title)?;
|
|
|
|
state.serialize_field("description", &self.meta.description)?;
|
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-03 08:52:49 +00:00
|
|
|
let (sorted_pages, _) = sort_pages(self.pages.clone(), Some(self));
|
|
|
|
state.serialize_field("pages", &sorted_pages)?;
|
2017-03-14 12:25:45 +00:00
|
|
|
state.serialize_field("subsections", &self.subsections)?;
|
|
|
|
state.end()
|
|
|
|
}
|
|
|
|
}
|