No clone when rendering rss feed
This commit is contained in:
parent
f100d956c6
commit
14560f224e
|
@ -26,4 +26,4 @@ mod sorting;
|
||||||
pub use file_info::FileInfo;
|
pub use file_info::FileInfo;
|
||||||
pub use page::Page;
|
pub use page::Page;
|
||||||
pub use section::Section;
|
pub use section::Section;
|
||||||
pub use sorting::{sort_pages, populate_siblings};
|
pub use sorting::{sort_pages, populate_siblings, sort_pages_by_date};
|
||||||
|
|
|
@ -5,6 +5,21 @@ use rayon::prelude::*;
|
||||||
use page::Page;
|
use page::Page;
|
||||||
use front_matter::SortBy;
|
use front_matter::SortBy;
|
||||||
|
|
||||||
|
|
||||||
|
/// The comparison function of sorting pages by day
|
||||||
|
/// Used by the RSS rendering
|
||||||
|
/// To remove if `sort_pages` is changed to work on borrowed values
|
||||||
|
/// This cannot be used in `sort_pages` currently as it takes &&Page instead of &Page
|
||||||
|
pub fn sort_pages_by_date(a: &&Page, b: &&Page) -> Ordering {
|
||||||
|
let ord = b.meta.date().unwrap().cmp(&a.meta.date().unwrap());
|
||||||
|
if ord == Ordering::Equal {
|
||||||
|
a.permalink.cmp(&b.permalink)
|
||||||
|
} else {
|
||||||
|
ord
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Sort pages by the given criteria
|
/// Sort pages by the given criteria
|
||||||
///
|
///
|
||||||
/// Any pages that doesn't have a required field when the sorting method is other than none
|
/// Any pages that doesn't have a required field when the sorting method is other than none
|
||||||
|
|
|
@ -309,7 +309,7 @@ pub fn after_template_change(site: &mut Site, path: &Path) -> Result<()> {
|
||||||
|
|
||||||
match filename {
|
match filename {
|
||||||
"sitemap.xml" => site.render_sitemap(),
|
"sitemap.xml" => site.render_sitemap(),
|
||||||
"rss.xml" => site.render_rss_feed(None, None),
|
"rss.xml" => site.render_rss_feed(site.pages.values().collect(), None),
|
||||||
"robots.txt" => site.render_robots(),
|
"robots.txt" => site.render_robots(),
|
||||||
"single.html" | "list.html" => site.render_taxonomies(),
|
"single.html" | "list.html" => site.render_taxonomies(),
|
||||||
"page.html" => {
|
"page.html" => {
|
||||||
|
|
|
@ -22,7 +22,7 @@ fn setup_site(name: &str) -> Site {
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_render_aliases(b: &mut test::Bencher) {
|
fn bench_render_aliases(b: &mut test::Bencher) {
|
||||||
let mut site = setup_site("small-blog");
|
let mut site = setup_site("big-blog");
|
||||||
let tmp_dir = tempdir().expect("create temp dir");
|
let tmp_dir = tempdir().expect("create temp dir");
|
||||||
let public = &tmp_dir.path().join("public");
|
let public = &tmp_dir.path().join("public");
|
||||||
site.set_output_path(&public);
|
site.set_output_path(&public);
|
||||||
|
@ -31,7 +31,7 @@ fn bench_render_aliases(b: &mut test::Bencher) {
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_render_sitemap(b: &mut test::Bencher) {
|
fn bench_render_sitemap(b: &mut test::Bencher) {
|
||||||
let mut site = setup_site("small-blog");
|
let mut site = setup_site("big-blog");
|
||||||
let tmp_dir = tempdir().expect("create temp dir");
|
let tmp_dir = tempdir().expect("create temp dir");
|
||||||
let public = &tmp_dir.path().join("public");
|
let public = &tmp_dir.path().join("public");
|
||||||
site.set_output_path(&public);
|
site.set_output_path(&public);
|
||||||
|
@ -40,11 +40,11 @@ fn bench_render_sitemap(b: &mut test::Bencher) {
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_render_rss_feed(b: &mut test::Bencher) {
|
fn bench_render_rss_feed(b: &mut test::Bencher) {
|
||||||
let mut site = setup_site("small-blog");
|
let mut site = setup_site("big-blog");
|
||||||
let tmp_dir = tempdir().expect("create temp dir");
|
let tmp_dir = tempdir().expect("create temp dir");
|
||||||
let public = &tmp_dir.path().join("public");
|
let public = &tmp_dir.path().join("public");
|
||||||
site.set_output_path(&public);
|
site.set_output_path(&public);
|
||||||
b.iter(|| site.render_rss_feed(None, None).unwrap());
|
b.iter(|| site.render_rss_feed(site.pages.values().collect(), None).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
|
|
|
@ -36,9 +36,9 @@ use config::{Config, get_config};
|
||||||
use utils::fs::{create_file, copy_directory, create_directory, ensure_directory_exists};
|
use utils::fs::{create_file, copy_directory, create_directory, ensure_directory_exists};
|
||||||
use utils::templates::{render_template, rewrite_theme_paths};
|
use utils::templates::{render_template, rewrite_theme_paths};
|
||||||
use utils::net::get_available_port;
|
use utils::net::get_available_port;
|
||||||
use content::{Page, Section, populate_siblings, sort_pages};
|
use content::{Page, Section, populate_siblings, sort_pages, sort_pages_by_date};
|
||||||
use templates::{GUTENBERG_TERA, global_fns, render_redirect_template};
|
use templates::{GUTENBERG_TERA, global_fns, render_redirect_template};
|
||||||
use front_matter::{SortBy, InsertAnchor};
|
use front_matter::{InsertAnchor};
|
||||||
use taxonomies::{Taxonomy, find_taxonomies};
|
use taxonomies::{Taxonomy, find_taxonomies};
|
||||||
use pagination::Paginator;
|
use pagination::Paginator;
|
||||||
|
|
||||||
|
@ -515,7 +515,7 @@ impl Site {
|
||||||
self.render_orphan_pages()?;
|
self.render_orphan_pages()?;
|
||||||
self.render_sitemap()?;
|
self.render_sitemap()?;
|
||||||
if self.config.generate_rss {
|
if self.config.generate_rss {
|
||||||
self.render_rss_feed(None, None)?;
|
self.render_rss_feed(self.pages.values().collect(), None)?;
|
||||||
}
|
}
|
||||||
self.render_404()?;
|
self.render_404()?;
|
||||||
self.render_robots()?;
|
self.render_robots()?;
|
||||||
|
@ -695,9 +695,8 @@ impl Site {
|
||||||
.par_iter()
|
.par_iter()
|
||||||
.map(|item| {
|
.map(|item| {
|
||||||
if taxonomy.kind.rss {
|
if taxonomy.kind.rss {
|
||||||
// TODO: can we get rid of `clone()`?
|
|
||||||
self.render_rss_feed(
|
self.render_rss_feed(
|
||||||
Some(item.pages.clone()),
|
item.pages.iter().map(|p| p).collect(),
|
||||||
Some(&PathBuf::from(format!("{}/{}", taxonomy.kind.name, item.slug))),
|
Some(&PathBuf::from(format!("{}/{}", taxonomy.kind.name, item.slug))),
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
@ -769,27 +768,25 @@ impl Site {
|
||||||
/// Renders a RSS feed for the given path and at the given path
|
/// Renders a RSS feed for the given path and at the given path
|
||||||
/// If both arguments are `None`, it will render only the RSS feed for the whole
|
/// If both arguments are `None`, it will render only the RSS feed for the whole
|
||||||
/// site at the root folder.
|
/// site at the root folder.
|
||||||
pub fn render_rss_feed(&self, all_pages: Option<Vec<Page>>, base_path: Option<&PathBuf>) -> Result<()> {
|
pub fn render_rss_feed(&self, all_pages: Vec<&Page>, base_path: Option<&PathBuf>) -> Result<()> {
|
||||||
ensure_directory_exists(&self.output_path)?;
|
ensure_directory_exists(&self.output_path)?;
|
||||||
|
|
||||||
let mut context = Context::new();
|
let mut context = Context::new();
|
||||||
let pages = all_pages
|
let mut pages = all_pages
|
||||||
// TODO: avoid that cloned().
|
|
||||||
// It requires having `sort_pages` take references of Page
|
|
||||||
.unwrap_or_else(|| self.pages.values().cloned().collect::<Vec<_>>())
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|p| p.meta.date.is_some() && !p.is_draft())
|
.filter(|p| p.meta.date.is_some() && !p.is_draft())
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
pages.par_sort_unstable_by(sort_pages_by_date);
|
||||||
|
|
||||||
// Don't generate a RSS feed if none of the pages has a date
|
// Don't generate a RSS feed if none of the pages has a date
|
||||||
if pages.is_empty() {
|
if pages.is_empty() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let (sorted_pages, _) = sort_pages(pages, SortBy::Date);
|
context.insert("last_build_date", &pages[0].meta.date.clone().map(|d| d.to_string()));
|
||||||
context.insert("last_build_date", &sorted_pages[0].meta.date.clone().map(|d| d.to_string()));
|
|
||||||
// limit to the last n elements
|
// limit to the last n elements
|
||||||
context.insert("pages", &sorted_pages.iter().take(self.config.rss_limit).collect::<Vec<_>>());
|
context.insert("pages", &pages.iter().take(self.config.rss_limit).collect::<Vec<_>>());
|
||||||
context.insert("config", &self.config);
|
context.insert("config", &self.config);
|
||||||
|
|
||||||
let rss_feed_url = if let Some(ref base) = base_path {
|
let rss_feed_url = if let Some(ref base) = base_path {
|
||||||
|
|
Loading…
Reference in a new issue