diff --git a/CHANGELOG.md b/CHANGELOG.md index 381853b..0e6f9d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ - Remove `section` from the `page` rendering context: this is too expensive. Use the global function `get_section` if you need to get it - Put back a 20 page limit on rss feed by default (configurable) +- Remove index page getting all sections: use the `get_section` global fn instead to +only get the ones you need +- Remove pages from pagers in pagination: they were not supposed to be there ## 0.0.7 (2017-06-19) diff --git a/README.md b/README.md index 83ddabc..5a7510d 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Each kind of page get their own variables: // TODO: detail the schema of the variables -- index.html: gets `section` representing the index section and all `sections` +- index.html: gets `section` representing the index section - page.html: gets `page` that contains the data for that page - section.html: gets `section` that contains the data for pages in it and its subsections - tags.html: gets `tags` @@ -65,6 +65,30 @@ the absolute URL of the current page and `current_path` that represents the path If you want to know all the data present in a template content, simply put `{{ __tera_context }}` in the templates and it will print it. +Gutenberg also ships with 3 Tera global functions: + +#### `get_page` +Takes a path to a `.md` file and returns the associated page + +```jinja2 +{% set page = get_page(path="blog/page2.md") %} +``` + +#### `get_section` +Takes a path to a `_index.md` file and returns the associated section + +```jinja2 +{% set section = get_page(path="blog/_index.md") %} +``` + +####` get_url` +Gets the permalink for a local file following the same convention as internal +link in markdown. + +```jinja2 +{% set url = get_url(link="./blog/_index.md") %} +``` + ### Static files Everything in the `static` folder will be copied into the output directory as-is. diff --git a/benches/site_load.rs b/benches/site_load.rs deleted file mode 100644 index 9d8287d..0000000 --- a/benches/site_load.rs +++ /dev/null @@ -1,157 +0,0 @@ -//! Benchmarking loading/markdown rendering of generated sites of various sizes - -#![feature(test)] -extern crate test; -extern crate gutenberg; - -use std::env; - -use gutenberg::Site; - - -#[bench] -fn bench_loading_small_blog(b: &mut test::Bencher) { - let mut path = env::current_dir().unwrap().to_path_buf(); - path.push("benches"); - path.push("small-blog"); - let mut site = Site::new(&path, "config.toml").unwrap(); - - b.iter(|| site.load().unwrap()); -} - -#[bench] -fn bench_loading_small_blog_with_syntax_highlighting(b: &mut test::Bencher) { - let mut path = env::current_dir().unwrap().to_path_buf(); - path.push("benches"); - path.push("small-blog"); - let mut site = Site::new(&path, "config.toml").unwrap(); - site.config.highlight_code = Some(true); - - b.iter(|| site.load().unwrap()); -} - -#[bench] -fn bench_loading_medium_blog(b: &mut test::Bencher) { - let mut path = env::current_dir().unwrap().to_path_buf(); - path.push("benches"); - path.push("medium-blog"); - let mut site = Site::new(&path, "config.toml").unwrap(); - - b.iter(|| site.load().unwrap()); -} - -#[bench] -fn bench_loading_medium_blog_with_syntax_highlighting(b: &mut test::Bencher) { - let mut path = env::current_dir().unwrap().to_path_buf(); - path.push("benches"); - path.push("medium-blog"); - let mut site = Site::new(&path, "config.toml").unwrap(); - site.config.highlight_code = Some(true); - - b.iter(|| site.load().unwrap()); -} - -#[bench] -fn bench_loading_big_blog(b: &mut test::Bencher) { - let mut path = env::current_dir().unwrap().to_path_buf(); - path.push("benches"); - path.push("big-blog"); - let mut site = Site::new(&path, "config.toml").unwrap(); - - b.iter(|| site.load().unwrap()); -} - -#[bench] -fn bench_loading_big_blog_with_syntax_highlighting(b: &mut test::Bencher) { - let mut path = env::current_dir().unwrap().to_path_buf(); - path.push("benches"); - path.push("big-blog"); - let mut site = Site::new(&path, "config.toml").unwrap(); - site.config.highlight_code = Some(true); - - b.iter(|| site.load().unwrap()); -} - -//#[bench] -//fn bench_loading_huge_blog(b: &mut test::Bencher) { -// let mut path = env::current_dir().unwrap().to_path_buf(); -// path.push("benches"); -// path.push("huge-blog"); -// let mut site = Site::new(&path, "config.toml").unwrap(); -// -// b.iter(|| site.load().unwrap()); -//} -// -//#[bench] -//fn bench_loading_huge_blog_with_syntax_highlighting(b: &mut test::Bencher) { -// let mut path = env::current_dir().unwrap().to_path_buf(); -// path.push("benches"); -// path.push("huge-blog"); -// let mut site = Site::new(&path, "config.toml").unwrap(); -// site.config.highlight_code = Some(true); -// -// b.iter(|| site.load().unwrap()); -//} - -#[bench] -fn bench_loading_small_kb(b: &mut test::Bencher) { - let mut path = env::current_dir().unwrap().to_path_buf(); - path.push("benches"); - path.push("small-kb"); - let mut site = Site::new(&path, "config.toml").unwrap(); - - b.iter(|| site.load().unwrap()); -} - -#[bench] -fn bench_loading_small_kb_with_syntax_highlighting(b: &mut test::Bencher) { - let mut path = env::current_dir().unwrap().to_path_buf(); - path.push("benches"); - path.push("small-kb"); - let mut site = Site::new(&path, "config.toml").unwrap(); - site.config.highlight_code = Some(true); - - b.iter(|| site.load().unwrap()); -} - -#[bench] -fn bench_loading_medium_kb(b: &mut test::Bencher) { - let mut path = env::current_dir().unwrap().to_path_buf(); - path.push("benches"); - path.push("medium-kb"); - let mut site = Site::new(&path, "config.toml").unwrap(); - - b.iter(|| site.load().unwrap()); -} - -#[bench] -fn bench_loading_medium_kb_with_syntax_highlighting(b: &mut test::Bencher) { - let mut path = env::current_dir().unwrap().to_path_buf(); - path.push("benches"); - path.push("medium-kb"); - let mut site = Site::new(&path, "config.toml").unwrap(); - site.config.highlight_code = Some(true); - - b.iter(|| site.load().unwrap()); -} - -//#[bench] -//fn bench_loading_huge_kb(b: &mut test::Bencher) { -// let mut path = env::current_dir().unwrap().to_path_buf(); -// path.push("benches"); -// path.push("huge-kb"); -// let mut site = Site::new(&path, "config.toml").unwrap(); -// -// b.iter(|| site.load().unwrap()); -//} -// -//#[bench] -//fn bench_loading_huge_kb_with_syntax_highlighting(b: &mut test::Bencher) { -// let mut path = env::current_dir().unwrap().to_path_buf(); -// path.push("benches"); -// path.push("huge-kb"); -// let mut site = Site::new(&path, "config.toml").unwrap(); -// site.config.highlight_code = Some(true); -// -// b.iter(|| site.load().unwrap()); -//} diff --git a/benches/sorting.rs b/benches/sorting.rs deleted file mode 100644 index 429d043..0000000 --- a/benches/sorting.rs +++ /dev/null @@ -1,27 +0,0 @@ -//! Benchmarking separate functions of Gutenberg - -#![feature(test)] -extern crate test; -extern crate gutenberg; -extern crate tempdir; - -use std::env; - -use tempdir::TempDir; -use gutenberg::{Site, sort_pages, SortBy}; - -fn setup_site(name: &str) -> Site { - let mut path = env::current_dir().unwrap().to_path_buf(); - path.push("benches"); - path.push(name); - let mut site = Site::new(&path, "config.toml").unwrap(); - site.load().unwrap(); - site -} - -#[bench] -fn bench_sort_pages_medium_blog(b: &mut test::Bencher) { - let mut site = setup_site("huge-blog"); - let section = site.sections.values().next().unwrap().clone(); - b.iter(|| sort_pages(section.pages.clone(), SortBy::None)); -} diff --git a/benches/unit.rs b/benches/unit.rs deleted file mode 100644 index ac0f000..0000000 --- a/benches/unit.rs +++ /dev/null @@ -1,73 +0,0 @@ -//! Benchmarking separate functions of Gutenberg - -#![feature(test)] -extern crate test; -extern crate gutenberg; -extern crate tempdir; - -use std::env; - -use tempdir::TempDir; -use gutenberg::{Site, sort_pages}; - - -fn setup_site(name: &str) -> Site { - let mut path = env::current_dir().unwrap().to_path_buf(); - path.push("benches"); - path.push(name); - let mut site = Site::new(&path, "config.toml").unwrap(); - site.load().unwrap(); - site -} - -#[bench] -fn bench_render_aliases(b: &mut test::Bencher) { - let mut site = setup_site("huge-blog"); - let tmp_dir = TempDir::new("benches").expect("create temp dir"); - let public = &tmp_dir.path().join("public"); - site.set_output_path(&public); - b.iter(|| site.render_aliases().unwrap()); -} -// -//#[bench] -//fn bench_render_sections_one_huge(b: &mut test::Bencher) { -// let mut site = setup_site("big-blog"); -// let tmp_dir = TempDir::new("benches").expect("create temp dir"); -// let public = &tmp_dir.path().join("public"); -// site.set_output_path(&public); -// b.iter(|| site.render_sections().unwrap()); -//} - -#[bench] -fn bench_render_small_section_with_pages_and_pagination(b: &mut test::Bencher) { - let mut site = setup_site("small-blog"); - let tmp_dir = TempDir::new("benches").expect("create temp dir"); - let public = &tmp_dir.path().join("public"); - site.set_output_path(&public); - b.iter(|| site.render_section(site.sections.values().next().unwrap(), true).unwrap()); -} - -#[bench] -fn bench_render_small_section_with_pages_and_no_pagination(b: &mut test::Bencher) { - let mut site = setup_site("small-blog"); - let tmp_dir = TempDir::new("benches").expect("create temp dir"); - let public = &tmp_dir.path().join("public"); - site.set_output_path(&public); - let mut section = site.sections.values().next().unwrap().clone(); - section.meta.paginate_by = None; - section.meta.template = None; - b.iter(|| site.render_section(§ion, true).unwrap()); -} - - -#[bench] -fn bench_sort_sections_pages_medium_blog(b: &mut test::Bencher) { - let mut site = setup_site("medium-blog"); - b.iter(|| site.sort_sections_pages(None)); -} - -#[bench] -fn bench_sort_sections_pages_medium_kb(b: &mut test::Bencher) { - let mut site = setup_site("medium-kb"); - b.iter(|| site.sort_sections_pages(None)); -} diff --git a/components/content/src/section.rs b/components/content/src/section.rs index dd27064..e4b63e4 100644 --- a/components/content/src/section.rs +++ b/components/content/src/section.rs @@ -104,7 +104,7 @@ impl Section { } /// Renders the page using the default layout, unless specified in front-matter - pub fn render_html(&self, sections: HashMap, tera: &Tera, config: &Config) -> Result { + pub fn render_html(&self, tera: &Tera, config: &Config) -> Result { let tpl_name = self.get_template_name(); let mut context = TeraContext::new(); @@ -112,9 +112,6 @@ impl Section { context.add("section", self); context.add("current_url", &self.permalink); context.add("current_path", &self.path); - if self.is_index() { - context.add("sections", §ions); - } tera.render(&tpl_name, &context) .chain_err(|| format!("Failed to render section '{}'", self.file.path.display())) diff --git a/components/pagination/src/lib.rs b/components/pagination/src/lib.rs index eded400..4daf1e2 100644 --- a/components/pagination/src/lib.rs +++ b/components/pagination/src/lib.rs @@ -10,7 +10,6 @@ extern crate content; extern crate front_matter; use std::collections::HashMap; -use std::path::PathBuf; use tera::{Tera, Context, to_value, Value}; @@ -41,6 +40,17 @@ impl<'a> Pager<'a> { pages, } } + + /// Returns a manually cloned Pager with the pages removed + /// for use as template context + fn clone_without_pages(&self) -> Pager<'a> { + Pager { + index: self.index, + permalink: self.permalink.clone(), + path: self.path.clone(), + pages: vec![], + } + } } #[derive(Clone, Debug, PartialEq)] @@ -125,7 +135,12 @@ impl<'a> Paginator<'a> { paginator.insert("first", to_value(&self.section.permalink).unwrap()); let last_pager = &self.pagers[self.pagers.len() - 1]; paginator.insert("last", to_value(&last_pager.permalink).unwrap()); - paginator.insert("pagers", to_value(&self.pagers).unwrap()); + paginator.insert( + "pagers", + to_value( + &self.pagers.iter().map(|p| p.clone_without_pages()).collect::>() + ).unwrap() + ); // Variables for this specific page if pager_index > 0 { @@ -147,16 +162,13 @@ impl<'a> Paginator<'a> { paginator } - pub fn render_pager(&self, pager: &Pager, config: &Config, sections: &HashMap, tera: &Tera) -> Result { + pub fn render_pager(&self, pager: &Pager, config: &Config, tera: &Tera) -> Result { let mut context = Context::new(); context.add("config", &config); context.add("section", self.section); context.add("current_url", &pager.permalink); context.add("current_path", &pager.path); context.add("paginator", &self.build_paginator_context(pager)); - if self.section.is_index() { - context.add("section", §ions); - } tera.render(&self.section.get_template_name(), &context) .chain_err(|| format!("Failed to render pager {} of section '{}'", pager.index, self.section.file.path.display())) diff --git a/benches/site_render.rs b/components/site/benches/render.rs similarity index 98% rename from benches/site_render.rs rename to components/site/benches/render.rs index 936f096..3208abe 100644 --- a/benches/site_render.rs +++ b/components/site/benches/render.rs @@ -2,12 +2,12 @@ #![feature(test)] extern crate test; -extern crate gutenberg; +extern crate site; extern crate tempdir; use std::env; -use gutenberg::Site; +use site::Site; use tempdir::TempDir; diff --git a/components/site/benches/site.rs b/components/site/benches/site.rs index c710371..6127f58 100644 --- a/components/site/benches/site.rs +++ b/components/site/benches/site.rs @@ -53,3 +53,14 @@ fn bench_render_categories(b: &mut test::Bencher) { site.set_output_path(&public); b.iter(|| site.render_categories().unwrap()); } + +#[bench] +fn bench_render_paginated(b: &mut test::Bencher) { + let mut site = setup_site("medium-blog"); + let tmp_dir = TempDir::new("benches").expect("create temp dir"); + let public = &tmp_dir.path().join("public"); + site.set_output_path(&public); + let section = site.sections.values().collect::>()[0]; + + b.iter(|| site.render_paginated(&public, section)); +} diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 8df9ce2..153c9f3 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -589,17 +589,6 @@ impl Site { Ok(()) } - /// Create a hashmap of paths to section - /// For example `content/posts/_index.md` key will be `posts` - /// The index section will always be called `index` so don't use a path such as - /// `content/index/_index.md` yourself - fn get_sections_map(&self) -> HashMap { - self.sections - .values() - .map(|s| (if s.is_index() { "index".to_string() } else { s.file.components.join("/") }, s.clone())) - .collect() - } - /// Renders a single section pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> { ensure_directory_exists(&self.output_path)?; @@ -630,11 +619,7 @@ impl Site { if section.meta.is_paginated() { self.render_paginated(&output_path, section)?; } else { - let output = section.render_html( - if section.is_index() { self.get_sections_map() } else { HashMap::new() }, - &self.tera, - &self.config, - )?; + let output = section.render_html(&self.tera, &self.config)?; create_file(&output_path.join("index.html"), &self.inject_livereload(output))?; } @@ -668,7 +653,7 @@ impl Site { } /// Renders a list of pages when the section/index is wanting pagination. - fn render_paginated(&self, output_path: &Path, section: &Section) -> Result<()> { + pub fn render_paginated(&self, output_path: &Path, section: &Section) -> Result<()> { ensure_directory_exists(&self.output_path)?; let paginate_path = match section.meta.paginate_path { @@ -679,6 +664,7 @@ impl Site { let paginator = Paginator::new(§ion.pages, section); let folder_path = output_path.join(&paginate_path); create_directory(&folder_path)?; + paginator .pagers .par_iter() @@ -686,7 +672,7 @@ impl Site { .map(|(i, pager)| { let page_path = folder_path.join(&format!("{}", i + 1)); create_directory(&page_path)?; - let output = paginator.render_pager(pager, &self.config, &self.sections, &self.tera)?; + let output = paginator.render_pager(pager, &self.config, &self.tera)?; if i > 0 { create_file(&page_path.join("index.html"), &self.inject_livereload(output))?; } else {