Serialize page/section assets only once

This commit is contained in:
Vincent Prouillet 2018-10-24 11:49:09 +02:00
parent 4e3d231ca9
commit 0f6c0736cb
3 changed files with 20 additions and 8 deletions

View file

@ -31,6 +31,8 @@ pub struct Page {
pub raw_content: String,
/// All the non-md files we found next to the .md file
pub assets: Vec<PathBuf>,
/// All the non-md files we found next to the .md file as string for use in templates
pub serialized_assets: Vec<String>,
/// The HTML rendered of the page
pub content: String,
/// The slug of that page.
@ -74,6 +76,7 @@ impl Page {
ancestors: vec![],
raw_content: "".to_string(),
assets: vec![],
serialized_assets: vec![],
content: "".to_string(),
slug: "".to_string(),
path: "".to_string(),
@ -168,6 +171,8 @@ impl Page {
} else {
page.assets = assets;
}
page.serialized_assets = page.serialize_assets();
} else {
page.assets = vec![];
}
@ -222,7 +227,7 @@ impl Page {
}
/// Creates a vectors of asset URLs.
pub fn serialize_assets(&self) -> Vec<String> {
fn serialize_assets(&self) -> Vec<String> {
self.assets.iter()
.filter_map(|asset| asset.file_name())
.filter_map(|filename| filename.to_str())
@ -247,6 +252,7 @@ impl Default for Page {
ancestors: vec![],
raw_content: "".to_string(),
assets: vec![],
serialized_assets: vec![],
content: "".to_string(),
slug: "".to_string(),
path: "".to_string(),

View file

@ -35,6 +35,8 @@ pub struct Section {
pub content: String,
/// All the non-md files we found next to the .md file
pub assets: Vec<PathBuf>,
/// All the non-md files we found next to the .md file as string for use in templates
pub serialized_assets: Vec<String>,
/// All direct pages of that section
pub pages: Vec<Key>,
/// All pages that cannot be sorted in this section
@ -65,6 +67,7 @@ impl Section {
permalink: "".to_string(),
raw_content: "".to_string(),
assets: vec![],
serialized_assets: vec![],
content: "".to_string(),
pages: vec![],
ignored_pages: vec![],
@ -119,6 +122,8 @@ impl Section {
section.assets = assets;
}
section.serialized_assets = section.serialize_assets();
Ok(section)
}
@ -179,7 +184,7 @@ impl Section {
}
/// Creates a vectors of asset URLs.
pub fn serialize_assets(&self) -> Vec<String> {
fn serialize_assets(&self) -> Vec<String> {
self.assets.iter()
.filter_map(|asset| asset.file_name())
.filter_map(|filename| filename.to_str())
@ -208,6 +213,7 @@ impl Default for Section {
permalink: "".to_string(),
raw_content: "".to_string(),
assets: vec![],
serialized_assets: vec![],
content: "".to_string(),
pages: vec![],
ignored_pages: vec![],

View file

@ -29,7 +29,7 @@ pub struct SerializingPage<'a> {
word_count: Option<usize>,
reading_time: Option<usize>,
toc: &'a [Header],
assets: Vec<String>,
assets: &'a [String],
draft: bool,
lighter: Option<Box<SerializingPage<'a>>>,
heavier: Option<Box<SerializingPage<'a>>>,
@ -75,7 +75,7 @@ impl<'a> SerializingPage<'a> {
word_count: page.word_count,
reading_time: page.reading_time,
toc: &page.toc,
assets: page.serialize_assets(),
assets: &page.serialized_assets,
draft: page.is_draft(),
lighter,
heavier,
@ -120,7 +120,7 @@ impl<'a> SerializingPage<'a> {
word_count: page.word_count,
reading_time: page.reading_time,
toc: &page.toc,
assets: page.serialize_assets(),
assets: &page.serialized_assets,
draft: page.is_draft(),
lighter: None,
heavier: None,
@ -145,7 +145,7 @@ pub struct SerializingSection<'a> {
word_count: Option<usize>,
reading_time: Option<usize>,
toc: &'a [Header],
assets: Vec<String>,
assets: &'a [String],
pages: Vec<SerializingPage<'a>>,
subsections: Vec<&'a str>,
}
@ -178,7 +178,7 @@ impl<'a> SerializingSection<'a> {
word_count: section.word_count,
reading_time: section.reading_time,
toc: &section.toc,
assets: section.serialize_assets(),
assets: &section.serialized_assets,
pages,
subsections,
}
@ -205,7 +205,7 @@ impl<'a> SerializingSection<'a> {
word_count: section.word_count,
reading_time: section.reading_time,
toc: &section.toc,
assets: section.serialize_assets(),
assets: &section.serialized_assets,
pages: vec![],
subsections: vec![],
}