Skip to content

Commit

Permalink
Implement zola serve --store-html
Browse files Browse the repository at this point in the history
  • Loading branch information
ppom0 committed Dec 27, 2024
1 parent cbd809a commit b267d3f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
14 changes: 10 additions & 4 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,
/// Both on the filesystem and in memory
Both,
}

#[derive(Debug)]
Expand Down Expand Up @@ -114,10 +116,10 @@ impl Site {
}

/// Enable some `zola serve` related options
pub fn enable_serve_mode(&mut self) {
pub fn enable_serve_mode(&mut self, both: bool) {
SITE_CONTENT.write().unwrap().clear();
self.config.enable_serve_mode();
self.build_mode = BuildMode::Memory;
self.build_mode = if both { BuildMode::Both } else { BuildMode::Memory };
}

/// Set the site to load the drafts.
Expand Down Expand Up @@ -660,16 +662,20 @@ impl Site {
};

match self.build_mode {
BuildMode::Disk => {
BuildMode::Disk | BuildMode::Both => {
let end_path = current_path.join(filename);
create_file(&end_path, &final_content)?;
}
BuildMode::Memory => {
_ => (),
}
match self.build_mode {
BuildMode::Memory | BuildMode::Both => {
let site_path =
if filename != "index.html" { site_path.join(filename) } else { site_path };

SITE_CONTENT.write().unwrap().insert(site_path, final_content);
}
_ => (),
}

Ok(current_path)
Expand Down
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ pub enum Command {
#[clap(short = 'O', long)]
open: bool,

/// Also store HTML in the public/ folder (by default HTML is only stored in-memory)
#[clap(short = 'H', long)]
store_html: bool,

/// Only rebuild the minimum on change - useful when working on a specific page/section
#[clap(short = 'f', long)]
fast: bool,
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ fn create_new_site(
base_url: Option<&str>,
config_file: &Path,
include_drafts: bool,
store_html: bool,
mut no_port_append: bool,
ws_port: Option<u16>,
) -> Result<(Site, SocketAddr, String)> {
Expand All @@ -390,7 +391,7 @@ fn create_new_site(
constructed_base_url.truncate(constructed_base_url.len() - 1);
}

site.enable_serve_mode();
site.enable_serve_mode(store_html);
site.set_base_url(constructed_base_url.clone());
if let Some(output_dir) = output_dir {
if !force && output_dir.exists() {
Expand Down Expand Up @@ -427,6 +428,7 @@ pub fn serve(
config_file: &Path,
open: bool,
include_drafts: bool,
store_html: bool,
fast_rebuild: bool,
no_port_append: bool,
utc_offset: UtcOffset,
Expand All @@ -441,6 +443,7 @@ pub fn serve(
base_url,
config_file,
include_drafts,
store_html,
no_port_append,
None,
)?;
Expand Down Expand Up @@ -666,6 +669,7 @@ pub fn serve(
base_url,
config_file,
include_drafts,
store_html,
no_port_append,
ws_port,
) {
Expand Down Expand Up @@ -893,6 +897,7 @@ mod tests {
base_url.as_deref(),
&config_file,
include_drafts,
store_html,
no_port_append,
ws_port,
)
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ fn main() {
base_url,
drafts,
open,
store_html,
fast,
no_port_append,
} => {
Expand Down Expand Up @@ -111,6 +112,7 @@ fn main() {
&config_file,
open,
drafts,
store_html,
fast,
no_port_append,
UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC),
Expand Down

0 comments on commit b267d3f

Please sign in to comment.