2017-07-01 07:47:41 +00:00
|
|
|
extern crate tera;
|
|
|
|
extern crate rayon;
|
|
|
|
extern crate glob;
|
|
|
|
extern crate walkdir;
|
2017-07-04 23:27:27 +00:00
|
|
|
extern crate serde;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
2017-07-06 13:19:15 +00:00
|
|
|
extern crate sass_rs;
|
|
|
|
|
|
|
|
#[macro_use]
|
2017-07-01 07:47:41 +00:00
|
|
|
extern crate errors;
|
|
|
|
extern crate config;
|
|
|
|
extern crate utils;
|
|
|
|
extern crate front_matter;
|
|
|
|
extern crate templates;
|
|
|
|
extern crate pagination;
|
|
|
|
extern crate taxonomies;
|
|
|
|
extern crate content;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
extern crate tempdir;
|
|
|
|
|
2017-05-16 04:37:00 +00:00
|
|
|
use std::collections::HashMap;
|
2017-04-18 05:07:02 +00:00
|
|
|
use std::fs::{remove_dir_all, copy, create_dir_all};
|
2017-07-01 10:13:21 +00:00
|
|
|
use std::mem;
|
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-09 07:34:12 +00:00
|
|
|
use walkdir::WalkDir;
|
2017-10-03 15:03:06 +00:00
|
|
|
use sass_rs::{Options, OutputStyle, compile_file};
|
2017-07-06 13:19:15 +00:00
|
|
|
|
2017-03-03 08:12:40 +00:00
|
|
|
use errors::{Result, ResultExt};
|
|
|
|
use config::{Config, get_config};
|
2017-07-09 02:37:05 +00:00
|
|
|
use utils::fs::{create_file, create_directory, ensure_directory_exists};
|
2017-08-24 23:38:03 +00:00
|
|
|
use utils::templates::{render_template, rewrite_theme_paths};
|
2017-07-01 07:47:41 +00:00
|
|
|
use content::{Page, Section, populate_previous_and_next_pages, sort_pages};
|
2017-05-13 04:05:38 +00:00
|
|
|
use templates::{GUTENBERG_TERA, global_fns, render_redirect_template};
|
2017-07-01 07:47:41 +00:00
|
|
|
use front_matter::{SortBy, InsertAnchor};
|
|
|
|
use taxonomies::Taxonomy;
|
|
|
|
use pagination::Paginator;
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2017-06-20 04:29:14 +00:00
|
|
|
use rayon::prelude::*;
|
2017-03-07 06:01:20 +00:00
|
|
|
|
2017-07-04 23:27:27 +00:00
|
|
|
|
2017-07-05 03:18:37 +00:00
|
|
|
/// The sitemap only needs links and potentially date so we trim down
|
|
|
|
/// all pages to only that
|
2017-07-04 23:27:27 +00:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
struct SitemapEntry {
|
|
|
|
permalink: String,
|
|
|
|
date: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SitemapEntry {
|
|
|
|
pub fn new(permalink: String, date: Option<String>) -> SitemapEntry {
|
|
|
|
SitemapEntry { permalink, date }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 08:12:40 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Site {
|
2017-05-16 04:37:00 +00:00
|
|
|
/// The base path of the gutenberg site
|
2017-03-14 12:25:45 +00:00
|
|
|
pub base_path: PathBuf,
|
2017-05-16 04:37:00 +00:00
|
|
|
/// The parsed config for the site
|
2017-03-14 12:25:45 +00:00
|
|
|
pub config: Config,
|
|
|
|
pub pages: HashMap<PathBuf, Page>,
|
2017-05-09 11:24:44 +00:00
|
|
|
pub sections: HashMap<PathBuf, 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-08-23 10:17:24 +00:00
|
|
|
pub static_path: PathBuf,
|
2017-05-16 04:37:00 +00:00
|
|
|
pub tags: Option<Taxonomy>,
|
|
|
|
pub categories: Option<Taxonomy>,
|
2017-05-13 13:37:01 +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
|
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-08-24 23:38:03 +00:00
|
|
|
let mut config = get_config(path, config_file);
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-04-28 07:18:18 +00:00
|
|
|
let tpl_glob = format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "templates/**/*.*ml");
|
2017-08-30 08:48:13 +00:00
|
|
|
// Only parsing as we might be extending templates from themes and that would error
|
|
|
|
// as we haven't loaded them yet
|
|
|
|
let mut tera = Tera::parse(&tpl_glob).chain_err(|| "Error parsing templates")?;
|
2017-03-09 07:46:38 +00:00
|
|
|
|
2017-08-24 23:38:03 +00:00
|
|
|
if let Some(theme) = config.theme.clone() {
|
|
|
|
// Grab data from the extra section of the theme
|
|
|
|
config.merge_with_theme(&path.join("themes").join(&theme).join("theme.toml"))?;
|
|
|
|
|
2017-08-23 10:17:24 +00:00
|
|
|
// Test that the {templates,static} folder exist for that theme
|
2017-08-24 23:38:03 +00:00
|
|
|
let theme_path = path.join("themes").join(&theme);
|
2017-08-23 10:17:24 +00:00
|
|
|
if !theme_path.join("templates").exists() {
|
|
|
|
bail!("Theme `{}` is missing a templates folder", theme);
|
|
|
|
}
|
|
|
|
if !theme_path.join("static").exists() {
|
|
|
|
bail!("Theme `{}` is missing a static folder", theme);
|
|
|
|
}
|
2017-08-30 08:48:13 +00:00
|
|
|
|
2017-08-23 10:17:24 +00:00
|
|
|
let theme_tpl_glob = format!("{}/{}", path.to_string_lossy().replace("\\", "/"), "themes/**/*.html");
|
2017-08-24 23:38:03 +00:00
|
|
|
let mut tera_theme = Tera::parse(&theme_tpl_glob).chain_err(|| "Error parsing templates from themes")?;
|
|
|
|
rewrite_theme_paths(&mut tera_theme, &theme);
|
2017-08-30 08:48:13 +00:00
|
|
|
tera_theme.build_inheritance_chains()?;
|
2017-08-24 23:38:03 +00:00
|
|
|
tera.extend(&tera_theme)?;
|
2017-08-23 10:17:24 +00:00
|
|
|
}
|
2017-08-30 08:48:13 +00:00
|
|
|
tera.extend(&GUTENBERG_TERA)?;
|
|
|
|
// the `extend` above already does it but hey
|
|
|
|
tera.build_inheritance_chains()?;
|
2017-08-23 10:17:24 +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-08-23 10:17:24 +00:00
|
|
|
config: config,
|
2017-03-03 08:12:40 +00:00
|
|
|
pages: HashMap::new(),
|
2017-05-09 11:24:44 +00:00
|
|
|
sections: HashMap::new(),
|
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-05-16 04:37:00 +00:00
|
|
|
tags: None,
|
|
|
|
categories: None,
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-09 12:47:02 +00:00
|
|
|
/// Get all the orphan (== without section) pages in the site
|
|
|
|
pub fn get_all_orphan_pages(&self) -> Vec<&Page> {
|
|
|
|
let mut pages_in_sections = vec![];
|
|
|
|
let mut orphans = vec![];
|
|
|
|
|
|
|
|
for s in self.sections.values() {
|
|
|
|
pages_in_sections.extend(s.all_pages_path());
|
|
|
|
}
|
|
|
|
|
|
|
|
for page in self.pages.values() {
|
2017-05-15 10:53:39 +00:00
|
|
|
if !pages_in_sections.contains(&page.file.path) {
|
2017-05-09 12:47:02 +00:00
|
|
|
orphans.push(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
orphans
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:25:45 +00:00
|
|
|
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-06-22 03:01:45 +00:00
|
|
|
let (section_entries, page_entries): (Vec<_>, Vec<_>) = glob(&content_glob)
|
|
|
|
.unwrap()
|
|
|
|
.filter_map(|e| e.ok())
|
2017-07-11 13:51:02 +00:00
|
|
|
.partition(|entry| entry.as_path().file_name().unwrap() == "_index.md");
|
2017-06-22 03:01:45 +00:00
|
|
|
|
|
|
|
let sections = {
|
|
|
|
let config = &self.config;
|
|
|
|
|
|
|
|
section_entries
|
|
|
|
.into_par_iter()
|
|
|
|
.filter(|entry| entry.as_path().file_name().unwrap() == "_index.md")
|
|
|
|
.map(|entry| {
|
|
|
|
let path = entry.as_path();
|
2017-07-11 13:51:02 +00:00
|
|
|
Section::from_file(path, config)
|
2017-09-27 14:37:17 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
2017-06-22 03:01:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let pages = {
|
|
|
|
let config = &self.config;
|
|
|
|
|
|
|
|
page_entries
|
|
|
|
.into_par_iter()
|
|
|
|
.filter(|entry| entry.as_path().file_name().unwrap() != "_index.md")
|
|
|
|
.map(|entry| {
|
|
|
|
let path = entry.as_path();
|
2017-07-11 13:51:02 +00:00
|
|
|
Page::from_file(path, config)
|
2017-09-27 14:37:17 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
2017-06-22 03:01:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Kinda duplicated code for add_section/add_page but necessary to do it that
|
|
|
|
// way because of the borrow checker
|
|
|
|
for section in sections {
|
|
|
|
let s = section?;
|
|
|
|
self.add_section(s, false)?;
|
|
|
|
}
|
|
|
|
|
2017-05-15 10:53:39 +00:00
|
|
|
// Insert a default index section if necessary so we don't need to create
|
|
|
|
// a _index.md to render the index page
|
2017-05-12 11:24:44 +00:00
|
|
|
let index_path = self.base_path.join("content").join("_index.md");
|
2017-05-12 07:30:01 +00:00
|
|
|
if !self.sections.contains_key(&index_path) {
|
2017-05-12 11:24:44 +00:00
|
|
|
let mut index_section = Section::default();
|
|
|
|
index_section.permalink = self.config.make_permalink("");
|
2017-10-24 18:11:39 +00:00
|
|
|
index_section.file.parent = self.base_path.join("content");
|
2017-10-24 18:32:46 +00:00
|
|
|
index_section.file.relative = "_index.md".to_string();
|
2017-05-12 11:24:44 +00:00
|
|
|
self.sections.insert(index_path, index_section);
|
2017-05-12 07:30:01 +00:00
|
|
|
}
|
2017-03-21 07:57:00 +00:00
|
|
|
|
2017-05-22 11:28:43 +00:00
|
|
|
let mut pages_insert_anchors = HashMap::new();
|
2017-06-22 12:37:03 +00:00
|
|
|
for page in pages {
|
|
|
|
let p = page?;
|
|
|
|
pages_insert_anchors.insert(p.file.path.clone(), self.find_parent_section_insert_anchor(&p.file.parent.clone()));
|
|
|
|
self.add_page(p, false)?;
|
2017-05-22 11:28:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 20:57:29 +00:00
|
|
|
self.render_markdown()?;
|
|
|
|
self.populate_sections();
|
|
|
|
self.populate_tags_and_categories();
|
2017-06-20 04:29:14 +00:00
|
|
|
|
2018-01-09 20:57:29 +00:00
|
|
|
self.register_tera_global_fns();
|
2017-06-20 04:29:14 +00:00
|
|
|
|
2018-01-09 20:57:29 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Render the markdown of all pages/sections
|
|
|
|
/// Used in a build and in `serve` if a shortcode has changed
|
|
|
|
pub fn render_markdown(&mut self) -> Result<()> {
|
|
|
|
// Another silly thing needed to not borrow &self in parallel and
|
|
|
|
// make the borrow checker happy
|
|
|
|
let permalinks = &self.permalinks;
|
|
|
|
let tera = &self.tera;
|
|
|
|
let config = &self.config;
|
|
|
|
|
|
|
|
// TODO: avoid the duplication with function above for that part
|
|
|
|
// This is needed in the first place because of silly borrow checker
|
|
|
|
let mut pages_insert_anchors = HashMap::new();
|
|
|
|
for (_, p) in &self.pages {
|
|
|
|
pages_insert_anchors.insert(p.file.path.clone(), self.find_parent_section_insert_anchor(&p.file.parent.clone()));
|
2017-05-12 09:05:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 20:57:29 +00:00
|
|
|
self.pages.par_iter_mut()
|
|
|
|
.map(|(_, page)| {
|
|
|
|
let insert_anchor = pages_insert_anchors[&page.file.path];
|
|
|
|
page.render_markdown(permalinks, tera, config, insert_anchor)
|
|
|
|
})
|
|
|
|
.fold(|| Ok(()), Result::and)
|
|
|
|
.reduce(|| Ok(()), Result::and)?;
|
2017-03-21 07:57:00 +00:00
|
|
|
|
2018-01-09 20:57:29 +00:00
|
|
|
self.sections.par_iter_mut()
|
|
|
|
.map(|(_, section)| section.render_markdown(permalinks, tera, config))
|
|
|
|
.fold(|| Ok(()), Result::and)
|
|
|
|
.reduce(|| Ok(()), Result::and)?;
|
2017-05-03 14:16:09 +00:00
|
|
|
|
2017-03-21 07:57:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-09-26 08:25:55 +00:00
|
|
|
pub fn register_tera_global_fns(&mut self) {
|
2018-01-12 23:10:19 +00:00
|
|
|
self.tera.register_global_function("trans", global_fns::make_trans(self.config.clone()));
|
2017-09-26 08:25:55 +00:00
|
|
|
self.tera.register_global_function("get_page", global_fns::make_get_page(&self.pages));
|
|
|
|
self.tera.register_global_function("get_section", global_fns::make_get_section(&self.sections));
|
2017-11-16 17:08:06 +00:00
|
|
|
self.tera.register_global_function(
|
|
|
|
"get_taxonomy_url",
|
|
|
|
global_fns::make_get_taxonomy_url(self.tags.clone(), self.categories.clone())
|
|
|
|
);
|
2017-08-07 14:29:58 +00:00
|
|
|
self.tera.register_global_function(
|
|
|
|
"get_url",
|
|
|
|
global_fns::make_get_url(self.permalinks.clone(), self.config.clone())
|
|
|
|
);
|
2017-05-17 10:04:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 13:37:01 +00:00
|
|
|
/// Add a page to the site
|
|
|
|
/// The `render` parameter is used in the serve command, when rebuilding a page.
|
|
|
|
/// If `true`, it will also render the markdown for that page
|
2018-01-29 17:40:12 +00:00
|
|
|
/// Returns the previous page struct if there was one at the same path
|
2017-06-22 03:01:45 +00:00
|
|
|
pub fn add_page(&mut self, page: Page, render: bool) -> Result<Option<Page>> {
|
|
|
|
let path = page.file.path.clone();
|
2017-05-15 10:53:39 +00:00
|
|
|
self.permalinks.insert(page.file.relative.clone(), page.permalink.clone());
|
|
|
|
let prev = self.pages.insert(page.file.path.clone(), page);
|
2017-03-21 07:57:00 +00:00
|
|
|
|
2017-05-13 13:37:01 +00:00
|
|
|
if render {
|
2017-06-22 03:01:45 +00:00
|
|
|
let insert_anchor = self.find_parent_section_insert_anchor(&self.pages[&path].file.parent);
|
2017-08-23 10:17:24 +00:00
|
|
|
let page = self.pages.get_mut(&path).unwrap();
|
2017-05-22 11:28:43 +00:00
|
|
|
page.render_markdown(&self.permalinks, &self.tera, &self.config, insert_anchor)?;
|
2017-05-13 13:37:01 +00:00
|
|
|
}
|
2017-05-12 11:24:44 +00:00
|
|
|
|
2017-05-13 13:37:01 +00:00
|
|
|
Ok(prev)
|
2017-03-21 07:57:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 13:37:01 +00:00
|
|
|
/// Add a section to the site
|
|
|
|
/// The `render` parameter is used in the serve command, when rebuilding a page.
|
|
|
|
/// If `true`, it will also render the markdown for that page
|
2018-01-29 17:40:12 +00:00
|
|
|
/// Returns the previous section struct if there was one at the same path
|
2017-06-22 03:01:45 +00:00
|
|
|
pub fn add_section(&mut self, section: Section, render: bool) -> Result<Option<Section>> {
|
|
|
|
let path = section.file.path.clone();
|
2017-05-15 10:53:39 +00:00
|
|
|
self.permalinks.insert(section.file.relative.clone(), section.permalink.clone());
|
|
|
|
let prev = self.sections.insert(section.file.path.clone(), section);
|
2017-05-11 05:33:23 +00:00
|
|
|
|
2017-05-13 13:37:01 +00:00
|
|
|
if render {
|
2017-08-23 10:17:24 +00:00
|
|
|
let section = self.sections.get_mut(&path).unwrap();
|
2017-05-13 13:37:01 +00:00
|
|
|
section.render_markdown(&self.permalinks, &self.tera, &self.config)?;
|
2017-05-12 09:05:00 +00:00
|
|
|
}
|
2017-05-13 13:37:01 +00:00
|
|
|
|
|
|
|
Ok(prev)
|
2017-04-21 07:21:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:28:43 +00:00
|
|
|
/// Finds the insert_anchor for the parent section of the directory at `path`.
|
|
|
|
/// Defaults to `AnchorInsert::None` if no parent section found
|
|
|
|
pub fn find_parent_section_insert_anchor(&self, parent_path: &PathBuf) -> InsertAnchor {
|
|
|
|
match self.sections.get(&parent_path.join("_index.md")) {
|
2017-09-27 14:37:17 +00:00
|
|
|
Some(s) => s.meta.insert_anchor_links.unwrap(),
|
2017-05-22 11:28:43 +00:00
|
|
|
None => InsertAnchor::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2017-05-12 11:24:44 +00:00
|
|
|
pub fn populate_sections(&mut self) {
|
2017-09-12 07:13:10 +00:00
|
|
|
let mut grandparent_paths: HashMap<PathBuf, Vec<PathBuf>> = HashMap::new();
|
|
|
|
|
2017-05-13 13:37:01 +00:00
|
|
|
for section in self.sections.values_mut() {
|
2017-05-15 10:53:39 +00:00
|
|
|
if let Some(ref grand_parent) = section.file.grand_parent {
|
2017-09-12 07:13:10 +00:00
|
|
|
grandparent_paths
|
|
|
|
.entry(grand_parent.to_path_buf())
|
|
|
|
.or_insert_with(|| vec![])
|
|
|
|
.push(section.file.path.clone());
|
2017-05-12 07:30:01 +00:00
|
|
|
}
|
2017-05-13 13:37:01 +00:00
|
|
|
// Make sure the pages of a section are empty since we can call that many times on `serve`
|
|
|
|
section.pages = vec![];
|
|
|
|
section.ignored_pages = vec![];
|
2017-03-14 12:25:45 +00:00
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2017-05-13 13:37:01 +00:00
|
|
|
for page in self.pages.values() {
|
2017-05-15 10:53:39 +00:00
|
|
|
let parent_section_path = page.file.parent.join("_index.md");
|
|
|
|
if self.sections.contains_key(&parent_section_path) {
|
2018-01-29 17:40:12 +00:00
|
|
|
// TODO: use references instead of cloning to avoid having to call populate_section on
|
|
|
|
// content change
|
2017-05-15 10:53:39 +00:00
|
|
|
self.sections.get_mut(&parent_section_path).unwrap().pages.push(page.clone());
|
2017-05-13 13:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-19 11:34:02 +00:00
|
|
|
|
2017-09-12 07:13:10 +00:00
|
|
|
self.sort_sections_pages(None);
|
|
|
|
// TODO: remove this clone
|
|
|
|
let sections = self.sections.clone();
|
|
|
|
|
2017-05-13 13:37:01 +00:00
|
|
|
for section in self.sections.values_mut() {
|
2017-09-26 08:04:18 +00:00
|
|
|
if let Some(paths) = grandparent_paths.get(§ion.file.parent) {
|
|
|
|
section.subsections = paths
|
|
|
|
.iter()
|
|
|
|
.map(|p| sections[p].clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
section.subsections
|
|
|
|
.sort_by(|a, b| a.meta.weight.unwrap().cmp(&b.meta.weight.unwrap()));
|
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
}
|
2017-05-13 13:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sorts the pages of the section at the given path
|
|
|
|
/// By default will sort all sections but can be made to only sort a single one by providing a path
|
|
|
|
pub fn sort_sections_pages(&mut self, only: Option<&Path>) {
|
2017-05-15 03:23:19 +00:00
|
|
|
for (path, section) in &mut self.sections {
|
2017-05-13 13:37:01 +00:00
|
|
|
if let Some(p) = only {
|
|
|
|
if p != path {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-07-01 10:13:21 +00:00
|
|
|
let pages = mem::replace(&mut section.pages, vec![]);
|
|
|
|
let (sorted_pages, cannot_be_sorted_pages) = sort_pages(pages, section.meta.sort_by());
|
2017-07-11 13:51:02 +00:00
|
|
|
section.pages = populate_previous_and_next_pages(&sorted_pages);
|
2017-05-13 13:37:01 +00:00
|
|
|
section.ignored_pages = cannot_be_sorted_pages;
|
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 04:37:00 +00:00
|
|
|
/// Find all the tags and categories if it's asked in the config
|
2017-03-21 07:57:00 +00:00
|
|
|
pub fn populate_tags_and_categories(&mut self) {
|
2018-03-12 19:11:03 +00:00
|
|
|
let generate_tags_pages = self.config.generate_tags_pages;
|
|
|
|
let generate_categories_pages = self.config.generate_categories_pages;
|
2017-05-16 04:37:00 +00:00
|
|
|
if !generate_tags_pages && !generate_categories_pages {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-20 03:42:43 +00:00
|
|
|
|
2017-05-16 04:37:00 +00:00
|
|
|
// TODO: can we pass a reference?
|
|
|
|
let (tags, categories) = Taxonomy::find_tags_and_categories(
|
2017-10-01 05:04:30 +00:00
|
|
|
&self.config,
|
2017-09-25 09:55:43 +00:00
|
|
|
self.pages
|
|
|
|
.values()
|
|
|
|
.filter(|p| !p.is_draft())
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.as_slice()
|
2017-05-16 04:37:00 +00:00
|
|
|
);
|
|
|
|
if generate_tags_pages {
|
|
|
|
self.tags = Some(tags);
|
|
|
|
}
|
|
|
|
if generate_categories_pages {
|
|
|
|
self.categories = Some(categories);
|
2017-03-20 03:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-23 10:17:24 +00:00
|
|
|
/// Copy the file at the given path into the public folder
|
|
|
|
pub fn copy_static_file<P: AsRef<Path>>(&self, path: P, base_path: &PathBuf) -> Result<()> {
|
|
|
|
let relative_path = path.as_ref().strip_prefix(base_path).unwrap();
|
2017-04-18 05:07:02 +00:00
|
|
|
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-08-23 10:17:24 +00:00
|
|
|
/// Copy the content of the given folder into the `public` folder
|
|
|
|
fn copy_static_directory(&self, path: &PathBuf) -> Result<()> {
|
|
|
|
for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) {
|
|
|
|
let relative_path = entry.path().strip_prefix(path).unwrap();
|
2017-04-18 05:07:02 +00:00
|
|
|
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());
|
2017-08-23 10:17:24 +00:00
|
|
|
self.copy_static_file(entry_fullpath, path)?;
|
2017-03-09 07:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-08 04:21:45 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-08-23 10:17:24 +00:00
|
|
|
/// Copy the main `static` folder and the theme `static` folder if a theme is used
|
|
|
|
pub fn copy_static_directories(&self) -> Result<()> {
|
|
|
|
// The user files will overwrite the theme files
|
|
|
|
if let Some(ref theme) = self.config.theme {
|
|
|
|
self.copy_static_directory(
|
|
|
|
&self.base_path.join("themes").join(theme).join("static")
|
|
|
|
)?;
|
|
|
|
}
|
2017-10-25 12:49:54 +00:00
|
|
|
// We're fine with missing static folders
|
|
|
|
if self.static_path.exists() {
|
|
|
|
self.copy_static_directory(&self.static_path)?;
|
|
|
|
}
|
2017-08-23 10:17:24 +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
|
2018-01-22 17:11:25 +00:00
|
|
|
remove_dir_all(&self.output_path).chain_err(|| "Couldn't delete output directory")?;
|
2017-03-10 11:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-05-03 08:52:49 +00:00
|
|
|
/// Renders a single content page
|
2017-06-29 12:14:08 +00:00
|
|
|
pub fn render_page(&self, page: &Page) -> Result<()> {
|
2017-05-16 04:37:00 +00:00
|
|
|
ensure_directory_exists(&self.output_path)?;
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2017-05-01 08:10:22 +00:00
|
|
|
// Copy the nesting of the content directory if we have sections for that page
|
2017-05-08 10:29:37 +00:00
|
|
|
let mut current_path = self.output_path.to_path_buf();
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2017-05-01 08:10:22 +00:00
|
|
|
for component in page.path.split('/') {
|
|
|
|
current_path.push(component);
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2017-05-01 08:10:22 +00:00
|
|
|
if !current_path.exists() {
|
|
|
|
create_directory(¤t_path)?;
|
2017-03-03 08:12:40 +00:00
|
|
|
}
|
2017-05-01 08:10:22 +00:00
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2017-05-01 08:10:22 +00:00
|
|
|
// Make sure the folder exists
|
|
|
|
create_directory(¤t_path)?;
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-05-01 08:10:22 +00:00
|
|
|
// Finally, create a index.html file there with the page rendered
|
2017-06-29 12:14:08 +00:00
|
|
|
let output = page.render_html(&self.tera, &self.config)?;
|
2017-05-16 05:54:50 +00:00
|
|
|
create_file(¤t_path.join("index.html"), &self.inject_livereload(output))?;
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-05-01 08:10:22 +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()))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-05-16 04:37:00 +00:00
|
|
|
/// Deletes the `public` directory and builds the site
|
2017-03-10 11:39:58 +00:00
|
|
|
pub fn build(&self) -> Result<()> {
|
|
|
|
self.clean()?;
|
2017-06-16 14:09:01 +00:00
|
|
|
// Render aliases first to allow overwriting
|
|
|
|
self.render_aliases()?;
|
2017-05-08 10:29:37 +00:00
|
|
|
self.render_sections()?;
|
|
|
|
self.render_orphan_pages()?;
|
2017-03-10 11:39:58 +00:00
|
|
|
self.render_sitemap()?;
|
2018-03-12 19:11:03 +00:00
|
|
|
if self.config.generate_rss {
|
2017-03-12 03:59:28 +00:00
|
|
|
self.render_rss_feed()?;
|
|
|
|
}
|
2017-03-20 12:40:03 +00:00
|
|
|
self.render_robots()?;
|
2017-05-13 13:37:01 +00:00
|
|
|
// `render_categories` and `render_tags` will check whether the config allows
|
|
|
|
// them to render or not
|
|
|
|
self.render_categories()?;
|
|
|
|
self.render_tags()?;
|
2017-03-20 12:40:03 +00:00
|
|
|
|
2017-08-23 10:17:24 +00:00
|
|
|
if let Some(ref theme) = self.config.theme {
|
|
|
|
let theme_path = self.base_path.join("themes").join(theme);
|
|
|
|
if theme_path.join("sass").exists() {
|
|
|
|
self.compile_sass(&theme_path)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-12 19:11:03 +00:00
|
|
|
if self.config.compile_sass {
|
2017-08-23 10:17:24 +00:00
|
|
|
self.compile_sass(&self.base_path)?;
|
2017-07-06 13:19:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 10:17:24 +00:00
|
|
|
self.copy_static_directories()
|
2017-03-10 11:39:58 +00:00
|
|
|
}
|
2017-06-16 14:09:01 +00:00
|
|
|
|
2018-03-16 21:20:03 +00:00
|
|
|
pub fn compile_sass(&self, base_path: &Path) -> Result<()> {
|
2017-07-06 13:19:15 +00:00
|
|
|
ensure_directory_exists(&self.output_path)?;
|
|
|
|
|
2018-03-16 21:20:03 +00:00
|
|
|
let sass_path = {
|
|
|
|
let mut sass_path = PathBuf::from(base_path);
|
|
|
|
sass_path.push("sass");
|
|
|
|
sass_path
|
|
|
|
};
|
|
|
|
|
2018-03-17 18:57:03 +00:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.output_style = OutputStyle::Compressed;
|
|
|
|
let mut compiled_paths = self.compile_sass_glob(&sass_path, "scss", options.clone())?;
|
|
|
|
|
|
|
|
options.indented_syntax = true;
|
|
|
|
compiled_paths.extend(self.compile_sass_glob(&sass_path, "sass", options)?);
|
|
|
|
|
|
|
|
compiled_paths.sort();
|
|
|
|
for window in compiled_paths.windows(2) {
|
|
|
|
if window[0].1 == window[1].1 {
|
|
|
|
bail!("SASS path conflict: \"{}\" and \"{}\" both compile to \"{}\"", window[0].0.display(), window[1].0.display(), window[0].1.display());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compile_sass_glob(&self, sass_path: &Path, extension: &str, options: Options) -> Result<Vec<(PathBuf, PathBuf)>> {
|
|
|
|
let glob_string = format!("{}/**/*.{}", sass_path.display(), extension);
|
|
|
|
let files = glob(&glob_string)
|
2017-07-06 13:19:15 +00:00
|
|
|
.unwrap()
|
|
|
|
.filter_map(|e| e.ok())
|
2017-07-11 13:51:02 +00:00
|
|
|
.filter(|entry| !entry.as_path().file_name().unwrap().to_string_lossy().starts_with('_'))
|
2017-07-06 13:19:15 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2018-03-17 18:57:03 +00:00
|
|
|
let mut compiled_paths = Vec::new();
|
2017-07-06 13:19:15 +00:00
|
|
|
for file in files {
|
2018-03-17 18:57:03 +00:00
|
|
|
let css = compile_file(&file, options.clone())?;
|
2017-07-06 13:19:15 +00:00
|
|
|
|
2018-03-16 21:20:03 +00:00
|
|
|
let path_inside_sass = file.strip_prefix(&sass_path).unwrap();
|
|
|
|
let parent_inside_sass = path_inside_sass.parent();
|
|
|
|
let css_output_path = self.output_path.join(path_inside_sass).with_extension("css");
|
|
|
|
|
|
|
|
if parent_inside_sass.is_some() {
|
|
|
|
create_dir_all(&css_output_path.parent().unwrap())?;
|
|
|
|
}
|
2018-03-17 18:57:03 +00:00
|
|
|
|
2018-03-16 21:20:03 +00:00
|
|
|
create_file(&css_output_path, &css)?;
|
2018-03-17 18:57:03 +00:00
|
|
|
compiled_paths.push((path_inside_sass.to_owned(), css_output_path));
|
2017-07-06 13:19:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 18:57:03 +00:00
|
|
|
Ok(compiled_paths)
|
2017-07-06 13:19:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:09:01 +00:00
|
|
|
pub fn render_aliases(&self) -> Result<()> {
|
|
|
|
for page in self.pages.values() {
|
|
|
|
if let Some(ref aliases) = page.meta.aliases {
|
|
|
|
for alias in aliases {
|
|
|
|
let mut output_path = self.output_path.to_path_buf();
|
2017-07-11 13:51:02 +00:00
|
|
|
for component in alias.split('/') {
|
2017-06-16 14:09:01 +00:00
|
|
|
output_path.push(&component);
|
|
|
|
|
|
|
|
if !output_path.exists() {
|
|
|
|
create_directory(&output_path)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
create_file(&output_path.join("index.html"), &render_redirect_template(&page.permalink, &self.tera)?)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-03-10 11:39:58 +00:00
|
|
|
|
2017-05-03 08:52:49 +00:00
|
|
|
/// Renders robots.txt
|
2017-05-13 13:37:01 +00:00
|
|
|
pub fn render_robots(&self) -> Result<()> {
|
2017-05-16 04:37:00 +00:00
|
|
|
ensure_directory_exists(&self.output_path)?;
|
2017-03-20 12:40:03 +00:00
|
|
|
create_file(
|
2017-05-16 05:54:50 +00:00
|
|
|
&self.output_path.join("robots.txt"),
|
2017-08-23 10:17:24 +00:00
|
|
|
&render_template("robots.txt", &self.tera, &Context::new(), self.config.theme.clone())?
|
2017-03-20 12:40:03 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-05-16 04:37:00 +00:00
|
|
|
/// Renders all categories and the single category pages if there are some
|
2017-05-13 13:37:01 +00:00
|
|
|
pub fn render_categories(&self) -> Result<()> {
|
2017-05-16 04:37:00 +00:00
|
|
|
if let Some(ref categories) = self.categories {
|
|
|
|
self.render_taxonomy(categories)?;
|
2017-05-13 13:37:01 +00:00
|
|
|
}
|
2017-05-16 04:37:00 +00:00
|
|
|
|
|
|
|
Ok(())
|
2017-05-13 13:37:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 04:37:00 +00:00
|
|
|
/// Renders all tags and the single tag pages if there are some
|
2017-05-13 13:37:01 +00:00
|
|
|
pub fn render_tags(&self) -> Result<()> {
|
2017-05-16 04:37:00 +00:00
|
|
|
if let Some(ref tags) = self.tags {
|
|
|
|
self.render_taxonomy(tags)?;
|
2017-05-13 13:37:01 +00:00
|
|
|
}
|
2017-03-20 03:42:43 +00:00
|
|
|
|
2017-05-16 04:37:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-03-07 06:01:20 +00:00
|
|
|
|
2017-05-16 04:37:00 +00:00
|
|
|
fn render_taxonomy(&self, taxonomy: &Taxonomy) -> Result<()> {
|
2017-05-30 10:23:07 +00:00
|
|
|
if taxonomy.items.is_empty() {
|
|
|
|
return Ok(())
|
|
|
|
}
|
|
|
|
|
2017-05-16 04:37:00 +00:00
|
|
|
ensure_directory_exists(&self.output_path)?;
|
|
|
|
let output_path = self.output_path.join(&taxonomy.get_list_name());
|
|
|
|
let list_output = taxonomy.render_list(&self.tera, &self.config)?;
|
2017-03-10 11:39:58 +00:00
|
|
|
create_directory(&output_path)?;
|
2017-05-16 05:54:50 +00:00
|
|
|
create_file(&output_path.join("index.html"), &self.inject_livereload(list_output))?;
|
2017-03-07 06:01:20 +00:00
|
|
|
|
2017-07-05 10:34:41 +00:00
|
|
|
taxonomy
|
|
|
|
.items
|
|
|
|
.par_iter()
|
|
|
|
.map(|item| {
|
|
|
|
let single_output = taxonomy.render_single_item(item, &self.tera, &self.config)?;
|
|
|
|
create_directory(&output_path.join(&item.slug))?;
|
|
|
|
create_file(
|
|
|
|
&output_path.join(&item.slug).join("index.html"),
|
|
|
|
&self.inject_livereload(single_output)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.fold(|| Ok(()), Result::and)
|
|
|
|
.reduce(|| Ok(()), Result::and)
|
2017-03-06 14:45:57 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 13:37:01 +00:00
|
|
|
/// What it says on the tin
|
|
|
|
pub fn render_sitemap(&self) -> Result<()> {
|
2017-05-16 04:37:00 +00:00
|
|
|
ensure_directory_exists(&self.output_path)?;
|
|
|
|
|
2017-03-06 14:45:57 +00:00
|
|
|
let mut context = Context::new();
|
2017-07-04 23:27:27 +00:00
|
|
|
|
|
|
|
context.add(
|
|
|
|
"pages",
|
2017-09-25 09:55:43 +00:00
|
|
|
&self.pages
|
|
|
|
.values()
|
|
|
|
.filter(|p| !p.is_draft())
|
2018-01-14 17:03:57 +00:00
|
|
|
.map(|p| {
|
|
|
|
let date = match p.meta.date {
|
|
|
|
Some(ref d) => Some(d.to_string()),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
SitemapEntry::new(p.permalink.clone(), date)
|
|
|
|
})
|
2017-09-25 09:55:43 +00:00
|
|
|
.collect::<Vec<_>>()
|
2017-07-04 23:27:27 +00:00
|
|
|
);
|
|
|
|
context.add(
|
|
|
|
"sections",
|
2017-09-25 09:55:43 +00:00
|
|
|
&self.sections
|
|
|
|
.values()
|
|
|
|
.map(|s| SitemapEntry::new(s.permalink.clone(), None))
|
|
|
|
.collect::<Vec<_>>()
|
2017-07-04 23:27:27 +00:00
|
|
|
);
|
2017-03-20 03:42:43 +00:00
|
|
|
|
|
|
|
let mut categories = vec![];
|
2017-05-16 04:37:00 +00:00
|
|
|
if let Some(ref c) = self.categories {
|
|
|
|
let name = c.get_list_name();
|
2017-07-04 23:27:27 +00:00
|
|
|
categories.push(SitemapEntry::new(self.config.make_permalink(&name), None));
|
2017-05-16 04:37:00 +00:00
|
|
|
for item in &c.items {
|
2017-03-22 11:59:49 +00:00
|
|
|
categories.push(
|
2017-07-04 23:27:27 +00:00
|
|
|
SitemapEntry::new(self.config.make_permalink(&format!("{}/{}", &name, item.slug)), None),
|
2017-03-22 11:59:49 +00:00
|
|
|
);
|
2017-03-20 03:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
context.add("categories", &categories);
|
|
|
|
|
|
|
|
let mut tags = vec![];
|
2017-05-16 04:37:00 +00:00
|
|
|
if let Some(ref t) = self.tags {
|
|
|
|
let name = t.get_list_name();
|
2017-07-04 23:27:27 +00:00
|
|
|
tags.push(SitemapEntry::new(self.config.make_permalink(&name), None));
|
2017-05-16 04:37:00 +00:00
|
|
|
for item in &t.items {
|
2017-03-22 11:59:49 +00:00
|
|
|
tags.push(
|
2017-07-04 23:27:27 +00:00
|
|
|
SitemapEntry::new(self.config.make_permalink(&format!("{}/{}", &name, item.slug)), None),
|
2017-03-22 11:59:49 +00:00
|
|
|
);
|
2017-03-20 03:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
context.add("tags", &tags);
|
2017-10-01 03:51:43 +00:00
|
|
|
context.add("config", &self.config);
|
2017-03-20 03:42:43 +00:00
|
|
|
|
2017-08-23 10:17:24 +00:00
|
|
|
let sitemap = &render_template("sitemap.xml", &self.tera, &context, self.config.theme.clone())?;
|
2017-03-06 14:45:57 +00:00
|
|
|
|
2017-08-31 09:01:26 +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
|
|
|
|
2017-05-13 13:37:01 +00:00
|
|
|
pub fn render_rss_feed(&self) -> Result<()> {
|
2017-05-16 04:37:00 +00:00
|
|
|
ensure_directory_exists(&self.output_path)?;
|
2017-05-08 10:29:37 +00:00
|
|
|
|
2017-03-07 07:43:27 +00:00
|
|
|
let mut context = Context::new();
|
2017-05-08 10:29:37 +00:00
|
|
|
let pages = self.pages.values()
|
2017-09-25 09:55:43 +00:00
|
|
|
.filter(|p| p.meta.date.is_some() && !p.is_draft())
|
2017-05-11 06:45:19 +00:00
|
|
|
.cloned()
|
2017-05-08 10:29:37 +00:00
|
|
|
.collect::<Vec<Page>>();
|
2017-03-07 07:43:27 +00:00
|
|
|
|
|
|
|
// Don't generate a RSS feed if none of the pages has a date
|
|
|
|
if pages.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2017-07-05 03:18:37 +00:00
|
|
|
|
2017-05-08 10:29:37 +00:00
|
|
|
let (sorted_pages, _) = sort_pages(pages, SortBy::Date);
|
2018-01-14 17:03:57 +00:00
|
|
|
context.add("last_build_date", &sorted_pages[0].meta.date.clone().map(|d| d.to_string()));
|
2017-07-15 03:51:32 +00:00
|
|
|
// limit to the last n elements)
|
2018-03-12 19:11:03 +00:00
|
|
|
context.add("pages", &sorted_pages.iter().take(self.config.rss_limit).collect::<Vec<_>>());
|
2017-03-07 07:43:27 +00:00
|
|
|
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-08-23 10:17:24 +00:00
|
|
|
let feed = &render_template("rss.xml", &self.tera, &context, self.config.theme.clone())?;
|
2017-03-07 07:43:27 +00:00
|
|
|
|
2017-08-31 09:01:26 +00:00
|
|
|
create_file(&self.output_path.join("rss.xml"), feed)?;
|
2017-03-14 12:25:45 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-05-12 13:32:35 +00:00
|
|
|
/// Renders a single section
|
2017-05-13 13:37:01 +00:00
|
|
|
pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> {
|
2017-05-16 04:37:00 +00:00
|
|
|
ensure_directory_exists(&self.output_path)?;
|
2017-05-12 13:32:35 +00:00
|
|
|
let public = self.output_path.clone();
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-05-12 13:32:35 +00:00
|
|
|
let mut output_path = public.to_path_buf();
|
2017-05-15 10:53:39 +00:00
|
|
|
for component in §ion.file.components {
|
2017-05-12 13:32:35 +00:00
|
|
|
output_path.push(component);
|
2017-03-14 12:25:45 +00:00
|
|
|
|
2017-05-12 13:32:35 +00:00
|
|
|
if !output_path.exists() {
|
|
|
|
create_directory(&output_path)?;
|
2017-05-11 05:33:23 +00:00
|
|
|
}
|
2017-05-12 13:32:35 +00:00
|
|
|
}
|
2017-05-11 05:33:23 +00:00
|
|
|
|
2017-05-13 13:37:01 +00:00
|
|
|
if render_pages {
|
2017-06-22 12:37:03 +00:00
|
|
|
section
|
|
|
|
.pages
|
|
|
|
.par_iter()
|
2017-07-11 13:51:02 +00:00
|
|
|
.map(|p| self.render_page(p))
|
2017-06-22 12:37:03 +00:00
|
|
|
.fold(|| Ok(()), Result::and)
|
|
|
|
.reduce(|| Ok(()), Result::and)?;
|
2017-05-12 13:32:35 +00:00
|
|
|
}
|
2017-05-11 05:33:23 +00:00
|
|
|
|
2017-05-12 13:32:35 +00:00
|
|
|
if !section.meta.should_render() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2017-07-25 07:56:13 +00:00
|
|
|
if let Some(ref redirect_to) = section.meta.redirect_to {
|
|
|
|
let permalink = self.config.make_permalink(redirect_to);
|
|
|
|
create_file(&output_path.join("index.html"), &render_redirect_template(&permalink, &self.tera)?)?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2017-05-12 13:32:35 +00:00
|
|
|
if section.meta.is_paginated() {
|
|
|
|
self.render_paginated(&output_path, section)?;
|
|
|
|
} else {
|
2017-07-06 09:51:36 +00:00
|
|
|
let output = section.render_html(&self.tera, &self.config)?;
|
2017-05-16 05:54:50 +00:00
|
|
|
create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
|
2017-05-08 10:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-10-24 18:11:39 +00:00
|
|
|
/// Used only on reload
|
2017-05-13 13:37:01 +00:00
|
|
|
pub fn render_index(&self) -> Result<()> {
|
2017-10-24 18:11:39 +00:00
|
|
|
self.render_section(
|
|
|
|
&self.sections[&self.base_path.join("content").join("_index.md")],
|
|
|
|
false
|
|
|
|
)
|
2017-05-13 13:37:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 13:32:35 +00:00
|
|
|
/// Renders all sections
|
2017-05-13 13:37:01 +00:00
|
|
|
pub fn render_sections(&self) -> Result<()> {
|
2017-06-22 12:37:03 +00:00
|
|
|
self.sections
|
|
|
|
.values()
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.into_par_iter()
|
|
|
|
.map(|s| self.render_section(s, true))
|
|
|
|
.fold(|| Ok(()), Result::and)
|
|
|
|
.reduce(|| Ok(()), Result::and)
|
2017-05-12 13:32:35 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 10:29:37 +00:00
|
|
|
/// Renders all pages that do not belong to any sections
|
2017-05-13 13:37:01 +00:00
|
|
|
pub fn render_orphan_pages(&self) -> Result<()> {
|
2017-05-16 04:37:00 +00:00
|
|
|
ensure_directory_exists(&self.output_path)?;
|
2017-05-08 10:29:37 +00:00
|
|
|
|
2017-05-09 12:47:02 +00:00
|
|
|
for page in self.get_all_orphan_pages() {
|
2017-06-29 12:14:08 +00:00
|
|
|
self.render_page(page)?;
|
2017-05-03 08:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Renders a list of pages when the section/index is wanting pagination.
|
2017-07-06 09:51:36 +00:00
|
|
|
pub fn render_paginated(&self, output_path: &Path, section: &Section) -> Result<()> {
|
2017-05-16 04:37:00 +00:00
|
|
|
ensure_directory_exists(&self.output_path)?;
|
2017-05-08 10:29:37 +00:00
|
|
|
|
2017-05-03 08:52:49 +00:00
|
|
|
let paginate_path = match section.meta.paginate_path {
|
|
|
|
Some(ref s) => s.clone(),
|
|
|
|
None => unreachable!()
|
|
|
|
};
|
|
|
|
|
2017-05-08 10:29:37 +00:00
|
|
|
let paginator = Paginator::new(§ion.pages, section);
|
2017-06-22 12:37:03 +00:00
|
|
|
let folder_path = output_path.join(&paginate_path);
|
|
|
|
create_directory(&folder_path)?;
|
2017-07-06 09:51:36 +00:00
|
|
|
|
2017-06-22 12:37:03 +00:00
|
|
|
paginator
|
|
|
|
.pagers
|
|
|
|
.par_iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, pager)| {
|
|
|
|
let page_path = folder_path.join(&format!("{}", i + 1));
|
|
|
|
create_directory(&page_path)?;
|
2017-07-06 09:51:36 +00:00
|
|
|
let output = paginator.render_pager(pager, &self.config, &self.tera)?;
|
2017-06-22 12:37:03 +00:00
|
|
|
if i > 0 {
|
|
|
|
create_file(&page_path.join("index.html"), &self.inject_livereload(output))?;
|
|
|
|
} else {
|
|
|
|
create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
|
|
|
|
create_file(&page_path.join("index.html"), &render_redirect_template(§ion.permalink, &self.tera)?)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.fold(|| Ok(()), Result::and)
|
|
|
|
.reduce(|| Ok(()), Result::and)
|
2017-03-07 07:43:27 +00:00
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
}
|