Skip to content

Commit

Permalink
Implement zola serve --store-html (#2750)
Browse files Browse the repository at this point in the history
* Implement zola serve --store-html

Fixes #2377

* Apply maintainer suggestions

* fix tests

---------

Co-authored-by: Vincent Prouillet <balthek@gmail.com>
  • Loading branch information
ppom0 and Keats authored Jan 9, 2025
1 parent 8e3357e commit 769ebc6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Exclude paginated pages in sitemap by default
- Allow treating a missing highlight language as error
- Handle more editors with change detection in `zola serve`
- Add argument to `zola serve` to write HTML files to disk
- Add optional parsing of Markdown definition lists


Expand Down
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 @@ -117,10 +119,10 @@ impl Site {
}

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

/// Set the site to load the drafts.
Expand Down Expand Up @@ -668,16 +670,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(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
9 changes: 7 additions & 2 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use ws::{Message, Sender, WebSocket};

use errors::{anyhow, Context, Error, Result};
use site::sass::compile_sass;
use site::{Site, SITE_CONTENT};
use site::{BuildMode, Site, SITE_CONTENT};
use utils::fs::{clean_site_output_folder, copy_file, create_directory};

use crate::fs_utils::{filter_events, ChangeKind, SimpleFileSystemEventKind};
Expand Down 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(if store_html { BuildMode::Both } else { BuildMode::Memory });
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 @@ -442,6 +444,7 @@ pub fn serve(
base_url,
config_file,
include_drafts,
store_html,
no_port_append,
None,
)?;
Expand Down Expand Up @@ -672,6 +675,7 @@ pub fn serve(
base_url,
config_file,
include_drafts,
store_html,
no_port_append,
ws_port,
) {
Expand Down Expand Up @@ -916,6 +920,7 @@ mod tests {
base_url.as_deref(),
&config_file,
include_drafts,
false,
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,
extra_watch_path,
Expand Down Expand Up @@ -112,6 +113,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 769ebc6

Please sign in to comment.