Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: add BuildMode::Skip #2419

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 55 additions & 62 deletions components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub enum BuildMode {
Disk,
/// In memory for the content -> `zola serve`
Memory,
/// Don't build; set to not render or not build
Skip,
}

#[derive(Debug)]
Expand Down Expand Up @@ -635,22 +637,22 @@ impl Site {
components: &[&str],
filename: &str,
content: String,
create_dirs: bool,
build_mode: Option<BuildMode>,
) -> Result<PathBuf> {
let write_dirs = self.build_mode == BuildMode::Disk || create_dirs;
let build_mode = build_mode.unwrap_or(self.build_mode);

let mut site_path = RelativePathBuf::new();
let mut current_path = self.output_path.to_path_buf();

if build_mode == BuildMode::Skip {
return Ok(current_path);
}

for component in components {
current_path.push(component);
site_path.push(component);
}

if write_dirs {
create_directory(&current_path)?;
}

let final_content = if !filename.ends_with("html") || !self.config.minify_html {
content
} else {
Expand All @@ -660,7 +662,7 @@ impl Site {
}
};

match self.build_mode {
match build_mode {
BuildMode::Disk => {
let end_path = current_path.join(filename);
create_file(&end_path, &final_content)?;
Expand All @@ -671,35 +673,36 @@ impl Site {

SITE_CONTENT.write().unwrap().insert(site_path, final_content);
}
BuildMode::Skip => unreachable!(),
}

Ok(current_path)
}

fn copy_asset(&self, src: &Path, dest: &Path) -> Result<()> {
copy_file_if_needed(src, dest, self.config.hard_link_static)
fn copy_assets(&self, parent: &Path, assets: &[impl AsRef<Path>], dest: &Path) -> Result<()> {
for asset in assets {
let asset_path = asset.as_ref();
copy_file_if_needed(
asset_path,
&dest.join(
asset_path.strip_prefix(parent).expect("Couldn't get filename from page asset"),
),
self.config.hard_link_static,
)?;
}

Ok(())
}

/// Renders a single content page
pub fn render_page(&self, page: &Page) -> Result<()> {
let output = page.render_html(&self.tera, &self.config, &self.library.read().unwrap())?;
let content = self.inject_livereload(output);
let components: Vec<&str> = page.path.split('/').collect();
let current_path =
self.write_content(&components, "index.html", content, !page.assets.is_empty())?;
let current_path = self.write_content(&components, "index.html", content, None)?;

// 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();
self.copy_asset(
asset_path,
&current_path.join(
asset_path
.strip_prefix(page.file.path.parent().unwrap())
.expect("Couldn't get filename from page asset"),
),
)?;
}
self.copy_assets(page.file.path.parent().unwrap(), &page.assets, &current_path)?;

Ok(())
}
Expand Down Expand Up @@ -834,7 +837,12 @@ impl Site {
Ok(())
}

fn render_alias(&self, alias: &str, permalink: &str) -> Result<()> {
fn render_alias(
&self,
alias: &str,
permalink: &str,
build_mode: Option<BuildMode>,
) -> Result<()> {
let mut split = alias.split('/').collect::<Vec<_>>();

// If the alias ends with an html file name, use that instead of mapping
Expand All @@ -848,7 +856,7 @@ impl Site {
None => "index.html",
};
let content = render_redirect_template(permalink, &self.tera)?;
self.write_content(&split, page_name, content, false)?;
self.write_content(&split, page_name, content, build_mode)?;
Ok(())
}

Expand All @@ -858,12 +866,13 @@ impl Site {
let library = self.library.read().unwrap();
for (_, page) in &library.pages {
for alias in &page.meta.aliases {
self.render_alias(alias, &page.permalink)?;
self.render_alias(alias, &page.permalink, None)?;
}
}
for (_, section) in &library.sections {
let build_mode = if section.meta.render { None } else { Some(BuildMode::Skip) };
for alias in &section.meta.aliases {
self.render_alias(alias, &section.permalink)?;
self.render_alias(alias, &section.permalink, None)?;
}
}
Ok(())
Expand All @@ -876,7 +885,7 @@ impl Site {
context.insert("lang", &self.config.default_language);
let output = render_template("404.html", &self.tera, context, &self.config.theme)?;
let content = self.inject_livereload(output);
self.write_content(&[], "404.html", content, false)?;
self.write_content(&[], "404.html", content, None)?;
Ok(())
}

Expand All @@ -885,16 +894,13 @@ impl Site {
let mut context = Context::new();
context.insert("config", &self.config.serialize(&self.config.default_language));
let content = render_template("robots.txt", &self.tera, context, &self.config.theme)?;
self.write_content(&[], "robots.txt", content, false)?;
self.write_content(&[], "robots.txt", content, None)?;
Ok(())
}

/// Renders all taxonomies
pub fn render_taxonomies(&self) -> Result<()> {
for taxonomy in &self.taxonomies {
if !taxonomy.kind.render {
continue;
}
self.render_taxonomy(taxonomy)?;
}

Expand All @@ -916,7 +922,8 @@ impl Site {
let list_output =
taxonomy.render_all_terms(&self.tera, &self.config, &self.library.read().unwrap())?;
let content = self.inject_livereload(list_output);
self.write_content(&components, "index.html", content, false)?;
let build_mode = if taxonomy.kind.render { None } else { Some(BuildMode::Skip) };
self.write_content(&components, "index.html", content, build_mode)?;

let library = self.library.read().unwrap();
taxonomy
Expand All @@ -936,12 +943,13 @@ impl Site {
&self.tera,
&self.config.theme,
),
build_mode,
)?;
} else {
let single_output =
taxonomy.render_term(item, &self.tera, &self.config, &library)?;
let content = self.inject_livereload(single_output);
self.write_content(&comp, "index.html", content, false)?;
self.write_content(&comp, "index.html", content, build_mode)?;
}

if taxonomy.kind.feed {
Expand Down Expand Up @@ -980,7 +988,7 @@ impl Site {
let mut context = Context::new();
context.insert("entries", &all_sitemap_entries);
let sitemap = render_template("sitemap.xml", &self.tera, context, &self.config.theme)?;
self.write_content(&[], "sitemap.xml", sitemap, false)?;
self.write_content(&[], "sitemap.xml", sitemap, None)?;
return Ok(());
}

Expand All @@ -993,7 +1001,7 @@ impl Site {
context.insert("entries", &chunk);
let sitemap = render_template("sitemap.xml", &self.tera, context, &self.config.theme)?;
let file_name = format!("sitemap{}.xml", i + 1);
self.write_content(&[], &file_name, sitemap, false)?;
self.write_content(&[], &file_name, sitemap, None)?;
let mut sitemap_url = self.config.make_permalink(&file_name);
sitemap_url.pop(); // Remove trailing slash
sitemap_index.push(sitemap_url);
Expand All @@ -1008,7 +1016,7 @@ impl Site {
main_context,
&self.config.theme,
)?;
self.write_content(&[], "sitemap.xml", sitemap, false)?;
self.write_content(&[], "sitemap.xml", sitemap, None)?;

Ok(())
}
Expand Down Expand Up @@ -1039,10 +1047,10 @@ impl Site {
&components.iter().map(|x| x.as_ref()).collect::<Vec<_>>(),
feed_filename,
feed,
false,
None,
)?;
} else {
self.write_content(&[], feed_filename, feed, false)?;
self.write_content(&[], feed_filename, feed, None)?;
}
Ok(())
}
Expand All @@ -1051,7 +1059,6 @@ impl Site {
pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> {
let mut output_path = self.output_path.clone();
let mut components: Vec<&str> = Vec::new();
let create_directories = self.build_mode == BuildMode::Disk || !section.assets.is_empty();

if section.lang != self.config.default_language {
components.push(&section.lang);
Expand All @@ -1063,10 +1070,6 @@ impl Site {
output_path.push(component);
}

if create_directories {
create_directory(&output_path)?;
}

if section.meta.generate_feed {
let library = &self.library.read().unwrap();
let pages = section.pages.iter().map(|k| library.pages.get(k).unwrap()).collect();
Expand All @@ -1082,17 +1085,7 @@ impl Site {
}

// Copy any asset we found previously into the same directory as the index.html
for asset in &section.assets {
let asset_path = asset.as_path();
self.copy_asset(
asset_path,
&output_path.join(
asset_path
.strip_prefix(section.file.path.parent().unwrap())
.expect("Failed to get asset filename for section"),
),
)?;
}
self.copy_assets(&section.file.path.parent().unwrap(), &section.assets, &output_path)?;

if render_pages {
section
Expand All @@ -1102,9 +1095,7 @@ impl Site {
.collect::<Result<()>>()?;
}

if !section.meta.render {
return Ok(());
}
let build_mode = if section.meta.render { None } else { Some(BuildMode::Skip) };

if let Some(ref redirect_to) = section.meta.redirect_to {
let permalink: Cow<String> = if is_external_link(redirect_to) {
Expand All @@ -1116,7 +1107,7 @@ impl Site {
&components,
"index.html",
render_redirect_template(&permalink, &self.tera)?,
create_directories,
build_mode,
)?;

return Ok(());
Expand All @@ -1126,12 +1117,13 @@ impl Site {
self.render_paginated(
components,
&Paginator::from_section(section, &self.library.read().unwrap()),
build_mode,
)?;
} else {
let output =
section.render_html(&self.tera, &self.config, &self.library.read().unwrap())?;
let content = self.inject_livereload(output);
self.write_content(&components, "index.html", content, false)?;
self.write_content(&components, "index.html", content, build_mode)?;
}

Ok(())
Expand Down Expand Up @@ -1163,6 +1155,7 @@ impl Site {
&self,
components: Vec<&'a str>,
paginator: &'a Paginator,
build_mode: Option<BuildMode>,
) -> Result<()> {
let index_components = components.clone();

Expand All @@ -1183,14 +1176,14 @@ impl Site {
let content = self.inject_livereload(output);

if pager.index > 1 {
self.write_content(&pager_components, "index.html", content, false)?;
self.write_content(&pager_components, "index.html", content, build_mode)?;
} else {
self.write_content(&index_components, "index.html", content, false)?;
self.write_content(&index_components, "index.html", content, build_mode)?;
self.write_content(
&pager_components,
"index.html",
render_redirect_template(&paginator.permalink, &self.tera)?,
false,
build_mode,
)?;
}

Expand Down
10 changes: 6 additions & 4 deletions components/site/src/sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ pub fn find_entries<'a>(
}

for s in library.sections.values() {
if s.meta.render {
let mut entry = SitemapEntry::new(Cow::Borrowed(&s.permalink), &None);
entry.add_extra(&s.meta.extra);
entries.insert(entry);
if !s.meta.render {
continue;
}

let mut entry = SitemapEntry::new(Cow::Borrowed(&s.permalink), &None);
entry.add_extra(&s.meta.extra);
entries.insert(entry);

if let Some(paginate_by) = s.paginate_by() {
let number_pagers = (s.pages.len() as f64 / paginate_by as f64).ceil() as isize;
for i in 1..=number_pagers {
Expand Down
Loading