2017-03-14 12:25:45 +00:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2017-03-07 06:01:20 +00:00
|
|
|
use std::iter::FromIterator;
|
2017-04-18 05:07:02 +00:00
|
|
|
use std::fs::{remove_dir_all, copy, create_dir_all};
|
2017-03-14 12:25:45 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-03-03 08:12:40 +00:00
|
|
|
|
|
|
|
use glob::glob;
|
|
|
|
use tera::{Tera, Context};
|
2017-03-07 06:01:20 +00:00
|
|
|
use slug::slugify;
|
2017-03-09 07:34:12 +00:00
|
|
|
use walkdir::WalkDir;
|
2017-03-03 08:12:40 +00:00
|
|
|
|
|
|
|
use errors::{Result, ResultExt};
|
|
|
|
use config::{Config, get_config};
|
2017-03-22 11:59:49 +00:00
|
|
|
use page::{Page, populate_previous_and_next_pages};
|
2017-03-10 11:39:58 +00:00
|
|
|
use utils::{create_file, create_directory};
|
2017-03-14 12:25:45 +00:00
|
|
|
use section::{Section};
|
2017-04-04 07:08:40 +00:00
|
|
|
use filters;
|
2017-03-03 08:12:40 +00:00
|
|
|
|
|
|
|
|
2017-03-09 07:46:38 +00:00
|
|
|
lazy_static! {
|
2017-03-29 14:32:40 +00:00
|
|
|
pub static ref GUTENBERG_TERA: Tera = {
|
2017-03-09 07:46:38 +00:00
|
|
|
let mut tera = Tera::default();
|
|
|
|
tera.add_raw_templates(vec![
|
|
|
|
("rss.xml", include_str!("templates/rss.xml")),
|
|
|
|
("sitemap.xml", include_str!("templates/sitemap.xml")),
|
2017-03-20 12:40:03 +00:00
|
|
|
("robots.txt", include_str!("templates/robots.txt")),
|
2017-04-06 08:07:53 +00:00
|
|
|
("anchor-link.html", include_str!("templates/anchor-link.html")),
|
2017-03-29 14:32:40 +00:00
|
|
|
|
|
|
|
("shortcodes/youtube.html", include_str!("templates/shortcodes/youtube.html")),
|
|
|
|
("shortcodes/vimeo.html", include_str!("templates/shortcodes/vimeo.html")),
|
|
|
|
("shortcodes/gist.html", include_str!("templates/shortcodes/gist.html")),
|
2017-03-09 07:46:38 +00:00
|
|
|
]).unwrap();
|
|
|
|
tera
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-07 06:01:20 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum RenderList {
|
|
|
|
Tags,
|
|
|
|
Categories,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A tag or category
|
|
|
|
#[derive(Debug, Serialize, PartialEq)]
|
|
|
|
struct ListItem {
|
|
|
|
name: String,
|
|
|
|
slug: String,
|
|
|
|
count: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ListItem {
|
|
|
|
pub fn new(name: &str, count: usize) -> ListItem {
|
|
|
|
ListItem {
|
|
|
|
name: name.to_string(),
|
|
|
|
slug: slugify(name),
|
|
|
|
count: count,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 08:12:40 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Site {
|
2017-03-14 12:25:45 +00:00
|
|
|
pub base_path: PathBuf,
|
|
|
|
pub config: Config,
|
|
|
|
pub pages: HashMap<PathBuf, Page>,
|
|
|
|
pub sections: BTreeMap<PathBuf, Section>,
|
2017-04-22 04:58:22 +00:00
|
|
|
pub index: Option<Section>,
|
2017-03-27 14:17:33 +00:00
|
|
|
pub tera: Tera,
|
2017-03-06 10:35:56 +00:00
|
|
|
live_reload: bool,
|
2017-03-14 12:25:45 +00:00
|
|
|
output_path: PathBuf,
|
2017-04-18 05:07:02 +00:00
|
|
|
static_path: PathBuf,
|
2017-03-20 03:42:43 +00:00
|
|
|
pub tags: HashMap<String, Vec<PathBuf>>,
|
|
|
|
pub categories: HashMap<String, Vec<PathBuf>>,
|
2017-04-21 07:21:44 +00:00
|
|
|
pub permalinks: HashMap<String, String>,
|
2017-03-03 08:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Site {
|
2017-03-14 12:25:45 +00:00
|
|
|
/// Parse a site at the given path. Defaults to the current dir
|
|
|
|
/// Passing in a path is only used in tests
|
2017-03-25 04:18:15 +00:00
|
|
|
pub fn new<P: AsRef<Path>>(path: P, config_file: &str) -> Result<Site> {
|
2017-03-14 12:25:45 +00:00
|
|
|
let path = path.as_ref();
|
|
|
|
|
2017-04-22 02:44:56 +00:00
|
|
|
let tpl_glob = format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "templates/**/*.html");
|
2017-03-14 12:25:45 +00:00
|
|
|
let mut tera = Tera::new(&tpl_glob).chain_err(|| "Error parsing templates")?;
|
2017-03-09 07:46:38 +00:00
|
|
|
tera.extend(&GUTENBERG_TERA)?;
|
2017-04-04 07:08:40 +00:00
|
|
|
tera.register_filter("markdown", filters::markdown);
|
|
|
|
tera.register_filter("base64_encode", filters::base64_encode);
|
|
|
|
tera.register_filter("base64_decode", filters::base64_decode);
|
2017-03-09 07:46:38 +00:00
|
|
|
|
2017-03-20 10:00:00 +00:00
|
|
|
let site = Site {
|
2017-03-14 12:25:45 +00:00
|
|
|
base_path: path.to_path_buf(),
|
2017-03-25 04:18:15 +00:00
|
|
|
config: get_config(path, config_file),
|
2017-03-03 08:12:40 +00:00
|
|
|
pages: HashMap::new(),
|
2017-03-14 12:25:45 +00:00
|
|
|
sections: BTreeMap::new(),
|
2017-04-22 04:58:22 +00:00
|
|
|
index: None,
|
2017-03-27 14:17:33 +00:00
|
|
|
tera: tera,
|
2017-03-14 12:25:45 +00:00
|
|
|
live_reload: false,
|
2017-04-18 05:07:02 +00:00
|
|
|
output_path: path.join("public"),
|
|
|
|
static_path: path.join("static"),
|
2017-03-20 03:42:43 +00:00
|
|
|
tags: HashMap::new(),
|
|
|
|
categories: HashMap::new(),
|
2017-04-21 07:21:44 +00:00
|
|
|
permalinks: HashMap::new(),
|
2017-03-03 08:12:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(site)
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
/// What the function name says
|
|
|
|
pub fn enable_live_reload(&mut self) {
|
|
|
|
self.live_reload = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Used by tests to change the output path to a tmp dir
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn set_output_path<P: AsRef<Path>>(&mut self, path: P) {
|
|
|
|
self.output_path = path.as_ref().to_path_buf();
|
|
|
|
}
|
|
|
|
|
2017-03-21 07:57:00 +00:00
|
|
|
/// Reads all .md files in the `content` directory and create pages/sections
|
2017-03-03 08:12:40 +00:00
|
|
|
/// out of them
|
2017-03-21 07:57:00 +00:00
|
|
|
pub fn load(&mut self) -> Result<()> {
|
2017-04-22 04:58:22 +00:00
|
|
|
let base_path = self.base_path.to_string_lossy().replace("\\", "/");
|
|
|
|
let content_glob = format!("{}/{}", base_path, "content/**/*.md");
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-03-21 07:57:00 +00:00
|
|
|
// TODO: make that parallel, that's the main bottleneck
|
|
|
|
// `add_section` and `add_page` can't be used in the parallel version afaik
|
2017-03-14 12:25:45 +00:00
|
|
|
for entry in glob(&content_glob).unwrap().filter_map(|e| e.ok()) {
|
|
|
|
let path = entry.as_path();
|
|
|
|
if path.file_name().unwrap() == "_index.md" {
|
2017-04-22 04:58:22 +00:00
|
|
|
// Index section
|
|
|
|
if path.parent().unwrap() == self.base_path.join("content") {
|
|
|
|
self.index = Some(Section::from_file(path, &self.config)?);
|
|
|
|
} else {
|
|
|
|
// all the other sections
|
|
|
|
self.add_section(path)?;
|
|
|
|
}
|
2017-03-14 12:25:45 +00:00
|
|
|
} else {
|
2017-03-22 11:59:49 +00:00
|
|
|
self.add_page(path)?;
|
2017-03-21 07:57:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
// A map of all .md files (section and pages) and their permalink
|
|
|
|
// We need that if there are relative links in the content that need to be resolved
|
|
|
|
let mut permalinks = HashMap::new();
|
|
|
|
|
|
|
|
for page in self.pages.values() {
|
|
|
|
permalinks.insert(page.relative_path.clone(), page.permalink.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
for section in self.sections.values() {
|
|
|
|
permalinks.insert(section.relative_path.clone(), section.permalink.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
for page in self.pages.values_mut() {
|
|
|
|
page.render_markdown(&permalinks, &self.tera, &self.config)?;
|
|
|
|
}
|
|
|
|
|
2017-04-21 07:21:44 +00:00
|
|
|
self.permalinks = permalinks;
|
2017-03-21 07:57:00 +00:00
|
|
|
self.populate_sections();
|
|
|
|
self.populate_tags_and_categories();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Simple wrapper fn to avoid repeating that code in several places
|
|
|
|
fn add_page(&mut self, path: &Path) -> Result<()> {
|
|
|
|
let page = Page::from_file(&path, &self.config)?;
|
|
|
|
self.pages.insert(page.file_path.clone(), page);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Simple wrapper fn to avoid repeating that code in several places
|
|
|
|
fn add_section(&mut self, path: &Path) -> Result<()> {
|
|
|
|
let section = Section::from_file(path, &self.config)?;
|
|
|
|
self.sections.insert(section.parent_path.clone(), section);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-04-21 07:21:44 +00:00
|
|
|
/// Called in serve, add a page again updating permalinks and its content
|
|
|
|
fn add_page_and_render(&mut self, path: &Path) -> Result<()> {
|
|
|
|
self.add_page(path)?;
|
|
|
|
let mut page = self.pages.get_mut(path).unwrap();
|
|
|
|
self.permalinks.insert(page.relative_path.clone(), page.permalink.clone());
|
|
|
|
page.render_markdown(&self.permalinks, &self.tera, &self.config)
|
|
|
|
}
|
|
|
|
|
2017-03-21 07:57:00 +00:00
|
|
|
/// Find out the direct subsections of each subsection if there are some
|
|
|
|
/// as well as the pages for each section
|
|
|
|
fn populate_sections(&mut self) {
|
|
|
|
for page in self.pages.values() {
|
|
|
|
if self.sections.contains_key(&page.parent_path) {
|
|
|
|
self.sections.get_mut(&page.parent_path).unwrap().pages.push(page.clone());
|
2017-03-03 08:12:40 +00:00
|
|
|
}
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
2017-03-21 07:57:00 +00:00
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
let mut grandparent_paths = HashMap::new();
|
2017-03-21 07:57:00 +00:00
|
|
|
for section in self.sections.values() {
|
2017-03-14 12:25:45 +00:00
|
|
|
let grand_parent = section.parent_path.parent().unwrap().to_path_buf();
|
|
|
|
grandparent_paths.entry(grand_parent).or_insert_with(|| vec![]).push(section.clone());
|
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2017-03-21 07:57:00 +00:00
|
|
|
for (parent_path, section) in &mut self.sections {
|
2017-03-19 11:34:02 +00:00
|
|
|
section.pages.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
2017-03-22 11:59:49 +00:00
|
|
|
section.pages = populate_previous_and_next_pages(section.pages.as_slice(), true);
|
2017-03-19 11:34:02 +00:00
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
match grandparent_paths.get(parent_path) {
|
|
|
|
Some(paths) => section.subsections.extend(paths.clone()),
|
|
|
|
None => continue,
|
|
|
|
};
|
2017-03-03 08:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 03:42:43 +00:00
|
|
|
/// Separated from `parse` for easier testing
|
2017-03-21 07:57:00 +00:00
|
|
|
pub fn populate_tags_and_categories(&mut self) {
|
2017-03-20 03:42:43 +00:00
|
|
|
for page in self.pages.values() {
|
|
|
|
if let Some(ref category) = page.meta.category {
|
|
|
|
self.categories
|
|
|
|
.entry(category.to_string())
|
|
|
|
.or_insert_with(|| vec![])
|
|
|
|
.push(page.file_path.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref tags) = page.meta.tags {
|
|
|
|
for tag in tags {
|
|
|
|
self.tags
|
|
|
|
.entry(tag.to_string())
|
|
|
|
.or_insert_with(|| vec![])
|
|
|
|
.push(page.file_path.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
/// Inject live reload script tag if in live reload mode
|
2017-03-06 10:35:56 +00:00
|
|
|
fn inject_livereload(&self, html: String) -> String {
|
|
|
|
if self.live_reload {
|
|
|
|
return html.replace(
|
|
|
|
"</body>",
|
|
|
|
r#"<script src="/livereload.js?port=1112&mindelay=10"></script></body>"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
html
|
|
|
|
}
|
|
|
|
|
2017-04-18 05:07:02 +00:00
|
|
|
/// Copy static file to public directory.
|
|
|
|
pub fn copy_static_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
|
|
|
|
let relative_path = path.as_ref().strip_prefix(&self.static_path).unwrap();
|
|
|
|
let target_path = self.output_path.join(relative_path);
|
|
|
|
if let Some(parent_directory) = target_path.parent() {
|
|
|
|
create_dir_all(parent_directory)?;
|
|
|
|
}
|
|
|
|
copy(path.as_ref(), &target_path)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-03-08 04:21:45 +00:00
|
|
|
/// Copy the content of the `static` folder into the `public` folder
|
2017-03-09 07:34:12 +00:00
|
|
|
pub fn copy_static_directory(&self) -> Result<()> {
|
2017-04-18 05:07:02 +00:00
|
|
|
for entry in WalkDir::new(&self.static_path).into_iter().filter_map(|e| e.ok()) {
|
|
|
|
let relative_path = entry.path().strip_prefix(&self.static_path).unwrap();
|
|
|
|
let target_path = self.output_path.join(relative_path);
|
2017-03-09 07:34:12 +00:00
|
|
|
|
|
|
|
if entry.path().is_dir() {
|
|
|
|
if !target_path.exists() {
|
2017-03-10 11:39:58 +00:00
|
|
|
create_directory(&target_path)?;
|
2017-03-09 07:34:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-18 05:07:02 +00:00
|
|
|
let entry_fullpath = self.base_path.join(entry.path());
|
|
|
|
self.copy_static_file(entry_fullpath)?;
|
2017-03-09 07:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-08 04:21:45 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-03-10 11:39:58 +00:00
|
|
|
/// Deletes the `public` directory if it exists
|
|
|
|
pub fn clean(&self) -> Result<()> {
|
2017-04-18 05:07:02 +00:00
|
|
|
if self.output_path.exists() {
|
2017-03-10 11:39:58 +00:00
|
|
|
// Delete current `public` directory so we can start fresh
|
2017-04-18 05:07:02 +00:00
|
|
|
remove_dir_all(&self.output_path).chain_err(|| "Couldn't delete `public` directory")?;
|
2017-03-10 11:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-03-21 07:57:00 +00:00
|
|
|
pub fn rebuild_after_content_change(&mut self, path: &Path) -> Result<()> {
|
|
|
|
if path.exists() {
|
|
|
|
// file exists, either a new one or updating content
|
|
|
|
if self.pages.contains_key(path) {
|
|
|
|
if path.ends_with("_index.md") {
|
|
|
|
self.add_section(path)?;
|
|
|
|
} else {
|
|
|
|
// probably just an update so just re-parse that page
|
2017-04-21 07:21:44 +00:00
|
|
|
self.add_page_and_render(path)?;
|
2017-03-21 07:57:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// new file?
|
2017-04-21 07:21:44 +00:00
|
|
|
self.add_page_and_render(path)?;
|
2017-03-21 07:57:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-21 07:21:44 +00:00
|
|
|
// File doesn't exist -> a deletion so we remove it from everything
|
|
|
|
let relative_path = self.pages[path].relative_path.clone();
|
2017-03-21 07:57:00 +00:00
|
|
|
self.pages.remove(path);
|
2017-04-21 07:21:44 +00:00
|
|
|
self.permalinks.remove(&relative_path);
|
2017-03-21 07:57:00 +00:00
|
|
|
}
|
|
|
|
self.populate_sections();
|
|
|
|
self.populate_tags_and_categories();
|
2017-03-06 10:35:56 +00:00
|
|
|
self.build()
|
|
|
|
}
|
|
|
|
|
2017-03-10 11:39:58 +00:00
|
|
|
pub fn rebuild_after_template_change(&mut self) -> Result<()> {
|
2017-03-27 14:17:33 +00:00
|
|
|
self.tera.full_reload()?;
|
2017-03-10 11:39:58 +00:00
|
|
|
self.build_pages()
|
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2017-03-10 11:39:58 +00:00
|
|
|
pub fn build_pages(&self) -> Result<()> {
|
2017-03-14 12:25:45 +00:00
|
|
|
let public = self.output_path.clone();
|
2017-03-10 11:39:58 +00:00
|
|
|
if !public.exists() {
|
|
|
|
create_directory(&public)?;
|
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
|
|
|
|
let mut pages = vec![];
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-03-03 08:12:40 +00:00
|
|
|
// First we render the pages themselves
|
|
|
|
for page in self.pages.values() {
|
|
|
|
// Copy the nesting of the content directory if we have sections for that page
|
|
|
|
let mut current_path = public.to_path_buf();
|
|
|
|
|
2017-03-30 08:17:12 +00:00
|
|
|
for component in page.path.split('/') {
|
2017-03-14 12:25:45 +00:00
|
|
|
current_path.push(component);
|
2017-03-03 08:12:40 +00:00
|
|
|
|
|
|
|
if !current_path.exists() {
|
2017-03-10 11:39:58 +00:00
|
|
|
create_directory(¤t_path)?;
|
2017-03-03 08:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the folder exists
|
2017-03-10 11:39:58 +00:00
|
|
|
create_directory(¤t_path)?;
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-03-03 08:12:40 +00:00
|
|
|
// Finally, create a index.html file there with the page rendered
|
2017-03-27 14:17:33 +00:00
|
|
|
let output = page.render_html(&self.tera, &self.config)?;
|
2017-03-06 10:35:56 +00:00
|
|
|
create_file(current_path.join("index.html"), &self.inject_livereload(output))?;
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-03-12 03:54:57 +00:00
|
|
|
// Copy any asset we found previously into the same directory as the index.html
|
|
|
|
for asset in &page.assets {
|
|
|
|
let asset_path = asset.as_path();
|
|
|
|
copy(&asset_path, ¤t_path.join(asset_path.file_name().unwrap()))?;
|
|
|
|
}
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-03-22 11:59:49 +00:00
|
|
|
pages.push(page.clone());
|
2017-03-03 08:12:40 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 06:01:20 +00:00
|
|
|
// Outputting categories and pages
|
2017-03-20 09:36:24 +00:00
|
|
|
if self.config.generate_categories_pages.unwrap() {
|
|
|
|
self.render_categories_and_tags(RenderList::Categories)?;
|
|
|
|
}
|
|
|
|
if self.config.generate_tags_pages.unwrap() {
|
|
|
|
self.render_categories_and_tags(RenderList::Tags)?;
|
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
|
|
|
|
// And finally the index page
|
|
|
|
let mut context = Context::new();
|
2017-03-06 11:58:31 +00:00
|
|
|
pages.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
2017-03-22 11:59:49 +00:00
|
|
|
|
|
|
|
context.add("pages", &populate_previous_and_next_pages(&pages, false));
|
2017-03-27 02:48:01 +00:00
|
|
|
context.add("sections", &self.sections.values().collect::<Vec<&Section>>());
|
2017-03-03 08:12:40 +00:00
|
|
|
context.add("config", &self.config);
|
2017-03-30 08:17:12 +00:00
|
|
|
context.add("current_url", &self.config.base_url);
|
|
|
|
context.add("current_path", &"");
|
2017-03-27 14:17:33 +00:00
|
|
|
let index = self.tera.render("index.html", &context)?;
|
2017-03-06 10:35:56 +00:00
|
|
|
create_file(public.join("index.html"), &self.inject_livereload(index))?;
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2017-03-07 06:01:20 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-03-10 11:39:58 +00:00
|
|
|
|
|
|
|
/// Builds the site to the `public` directory after deleting it
|
|
|
|
pub fn build(&self) -> Result<()> {
|
|
|
|
self.clean()?;
|
|
|
|
self.build_pages()?;
|
|
|
|
self.render_sitemap()?;
|
2017-03-20 03:42:43 +00:00
|
|
|
|
2017-03-12 03:59:28 +00:00
|
|
|
if self.config.generate_rss.unwrap() {
|
|
|
|
self.render_rss_feed()?;
|
|
|
|
}
|
2017-03-20 03:42:43 +00:00
|
|
|
|
2017-03-20 12:40:03 +00:00
|
|
|
self.render_robots()?;
|
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
self.render_sections()?;
|
2017-03-10 11:39:58 +00:00
|
|
|
self.copy_static_directory()
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:40:03 +00:00
|
|
|
fn render_robots(&self) -> Result<()> {
|
|
|
|
create_file(
|
|
|
|
self.output_path.join("robots.txt"),
|
2017-03-27 14:17:33 +00:00
|
|
|
&self.tera.render("robots.txt", &Context::new())?
|
2017-03-20 12:40:03 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-03-07 06:01:20 +00:00
|
|
|
/// Render the /{categories, list} pages and each individual category/tag page
|
2017-03-20 03:42:43 +00:00
|
|
|
/// They are the same thing fundamentally, a list of pages with something in common
|
|
|
|
fn render_categories_and_tags(&self, kind: RenderList) -> Result<()> {
|
|
|
|
let items = match kind {
|
|
|
|
RenderList::Categories => &self.categories,
|
|
|
|
RenderList::Tags => &self.tags,
|
|
|
|
};
|
|
|
|
|
|
|
|
if items.is_empty() {
|
2017-03-07 06:01:20 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2017-03-20 03:42:43 +00:00
|
|
|
let (list_tpl_name, single_tpl_name, name, var_name) = if kind == RenderList::Categories {
|
|
|
|
("categories.html", "category.html", "categories", "category")
|
2017-03-07 06:01:20 +00:00
|
|
|
} else {
|
2017-03-20 03:42:43 +00:00
|
|
|
("tags.html", "tag.html", "tags", "tag")
|
2017-03-07 06:01:20 +00:00
|
|
|
};
|
|
|
|
|
2017-03-20 03:42:43 +00:00
|
|
|
// Create the categories/tags directory first
|
2017-03-14 12:25:45 +00:00
|
|
|
let public = self.output_path.clone();
|
2017-03-07 06:01:20 +00:00
|
|
|
let mut output_path = public.to_path_buf();
|
|
|
|
output_path.push(name);
|
2017-03-10 11:39:58 +00:00
|
|
|
create_directory(&output_path)?;
|
2017-03-07 06:01:20 +00:00
|
|
|
|
2017-03-20 03:42:43 +00:00
|
|
|
// Then render the index page for that kind.
|
|
|
|
// We sort by number of page in that category/tag
|
|
|
|
let mut sorted_items = vec![];
|
|
|
|
for (item, count) in Vec::from_iter(items).into_iter().map(|(a, b)| (a, b.len())) {
|
2017-03-22 11:59:49 +00:00
|
|
|
sorted_items.push(ListItem::new(item, count));
|
2017-03-07 06:01:20 +00:00
|
|
|
}
|
2017-03-20 03:42:43 +00:00
|
|
|
sorted_items.sort_by(|a, b| b.count.cmp(&a.count));
|
2017-03-07 06:01:20 +00:00
|
|
|
let mut context = Context::new();
|
2017-03-20 03:42:43 +00:00
|
|
|
context.add(name, &sorted_items);
|
2017-03-07 06:01:20 +00:00
|
|
|
context.add("config", &self.config);
|
2017-03-30 08:17:12 +00:00
|
|
|
context.add("current_url", &self.config.make_permalink(name));
|
|
|
|
context.add("current_path", &format!("/{}", name));
|
2017-03-20 03:42:43 +00:00
|
|
|
// And render it immediately
|
2017-03-27 14:17:33 +00:00
|
|
|
let list_output = self.tera.render(list_tpl_name, &context)?;
|
2017-03-07 06:01:20 +00:00
|
|
|
create_file(output_path.join("index.html"), &self.inject_livereload(list_output))?;
|
|
|
|
|
2017-03-20 03:42:43 +00:00
|
|
|
// Now, each individual item
|
|
|
|
for (item_name, pages_paths) in items.iter() {
|
|
|
|
let mut pages: Vec<&Page> = self.pages
|
|
|
|
.iter()
|
2017-03-22 11:59:49 +00:00
|
|
|
.filter(|&(path, _)| pages_paths.contains(path))
|
2017-03-20 03:42:43 +00:00
|
|
|
.map(|(_, page)| page)
|
|
|
|
.collect();
|
2017-03-07 06:01:20 +00:00
|
|
|
pages.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
2017-03-20 03:42:43 +00:00
|
|
|
|
|
|
|
let mut context = Context::new();
|
2017-03-07 06:01:20 +00:00
|
|
|
let slug = slugify(&item_name);
|
|
|
|
context.add(var_name, &item_name);
|
|
|
|
context.add(&format!("{}_slug", var_name), &slug);
|
|
|
|
context.add("pages", &pages);
|
|
|
|
context.add("config", &self.config);
|
2017-03-30 08:17:12 +00:00
|
|
|
context.add("current_url", &self.config.make_permalink(&format!("{}/{}", name, slug)));
|
|
|
|
context.add("current_path", &format!("/{}/{}", name, slug));
|
2017-03-27 14:17:33 +00:00
|
|
|
let single_output = self.tera.render(single_tpl_name, &context)?;
|
2017-03-07 06:01:20 +00:00
|
|
|
|
2017-03-10 11:39:58 +00:00
|
|
|
create_directory(&output_path.join(&slug))?;
|
2017-03-07 06:01:20 +00:00
|
|
|
create_file(
|
|
|
|
output_path.join(&slug).join("index.html"),
|
|
|
|
&self.inject_livereload(single_output)
|
|
|
|
)?;
|
|
|
|
}
|
2017-03-06 14:45:57 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-03-07 07:43:27 +00:00
|
|
|
fn render_sitemap(&self) -> Result<()> {
|
2017-03-06 14:45:57 +00:00
|
|
|
let mut context = Context::new();
|
|
|
|
context.add("pages", &self.pages.values().collect::<Vec<&Page>>());
|
2017-03-19 10:40:31 +00:00
|
|
|
context.add("sections", &self.sections.values().collect::<Vec<&Section>>());
|
2017-03-20 03:42:43 +00:00
|
|
|
|
|
|
|
let mut categories = vec![];
|
2017-03-22 11:59:49 +00:00
|
|
|
if self.config.generate_categories_pages.unwrap() && !self.categories.is_empty() {
|
|
|
|
categories.push(self.config.make_permalink("categories"));
|
|
|
|
for category in self.categories.keys() {
|
|
|
|
categories.push(
|
|
|
|
self.config.make_permalink(&format!("categories/{}", slugify(category)))
|
|
|
|
);
|
2017-03-20 03:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
context.add("categories", &categories);
|
|
|
|
|
|
|
|
let mut tags = vec![];
|
2017-03-22 11:59:49 +00:00
|
|
|
if self.config.generate_tags_pages.unwrap() && !self.tags.is_empty() {
|
|
|
|
tags.push(self.config.make_permalink("tags"));
|
|
|
|
for tag in self.tags.keys() {
|
|
|
|
tags.push(
|
|
|
|
self.config.make_permalink(&format!("tags/{}", slugify(tag)))
|
|
|
|
);
|
2017-03-20 03:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
context.add("tags", &tags);
|
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
let sitemap = self.tera.render("sitemap.xml", &context)?;
|
2017-03-06 14:45:57 +00:00
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
create_file(self.output_path.join("sitemap.xml"), &sitemap)?;
|
2017-03-06 14:45:57 +00:00
|
|
|
|
2017-03-03 08:12:40 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-03-07 07:43:27 +00:00
|
|
|
|
|
|
|
fn render_rss_feed(&self) -> Result<()> {
|
|
|
|
let mut context = Context::new();
|
|
|
|
let mut pages = self.pages.values()
|
|
|
|
.filter(|p| p.meta.date.is_some())
|
|
|
|
.take(15) // limit to the last 15 elements
|
|
|
|
.collect::<Vec<&Page>>();
|
|
|
|
|
|
|
|
// Don't generate a RSS feed if none of the pages has a date
|
|
|
|
if pages.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-03-07 07:43:27 +00:00
|
|
|
pages.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
|
|
|
context.add("pages", &pages);
|
|
|
|
context.add("last_build_date", &pages[0].meta.date);
|
|
|
|
context.add("config", &self.config);
|
2017-03-10 11:39:58 +00:00
|
|
|
|
2017-03-10 13:19:36 +00:00
|
|
|
let rss_feed_url = if self.config.base_url.ends_with('/') {
|
2017-04-06 02:21:37 +00:00
|
|
|
format!("{}{}", self.config.base_url, "rss.xml")
|
2017-03-10 11:39:58 +00:00
|
|
|
} else {
|
2017-04-06 02:21:37 +00:00
|
|
|
format!("{}/{}", self.config.base_url, "rss.xml")
|
2017-03-10 11:39:58 +00:00
|
|
|
};
|
|
|
|
context.add("feed_url", &rss_feed_url);
|
2017-03-07 07:43:27 +00:00
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
let sitemap = self.tera.render("rss.xml", &context)?;
|
2017-03-07 07:43:27 +00:00
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
create_file(self.output_path.join("rss.xml"), &sitemap)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_sections(&self) -> Result<()> {
|
|
|
|
let public = self.output_path.clone();
|
|
|
|
|
|
|
|
for section in self.sections.values() {
|
|
|
|
let mut output_path = public.to_path_buf();
|
|
|
|
for component in §ion.components {
|
|
|
|
output_path.push(component);
|
|
|
|
|
|
|
|
if !output_path.exists() {
|
|
|
|
create_directory(&output_path)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
let output = section.render_html(&self.tera, &self.config)?;
|
2017-03-14 12:25:45 +00:00
|
|
|
create_file(output_path.join("index.html"), &self.inject_livereload(output))?;
|
|
|
|
}
|
2017-03-07 07:43:27 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
}
|