Skip to content

Commit 8ba104f

Browse files
committed
Implement zola serve --store-html
Fixes getzola#2377
1 parent 967c54a commit 8ba104f

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

components/site/src/lib.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ pub enum BuildMode {
3838
Disk,
3939
/// In memory for the content -> `zola serve`
4040
Memory,
41+
/// Both on the filesystem and in memory
42+
Both,
4143
}
4244

4345
#[derive(Debug)]
@@ -114,10 +116,10 @@ impl Site {
114116
}
115117

116118
/// Enable some `zola serve` related options
117-
pub fn enable_serve_mode(&mut self) {
119+
pub fn enable_serve_mode(&mut self, both: bool) {
118120
SITE_CONTENT.write().unwrap().clear();
119121
self.config.enable_serve_mode();
120-
self.build_mode = BuildMode::Memory;
122+
self.build_mode = if both { BuildMode::Both } else { BuildMode::Memory };
121123
}
122124

123125
/// Set the site to load the drafts.
@@ -660,16 +662,20 @@ impl Site {
660662
};
661663

662664
match self.build_mode {
663-
BuildMode::Disk => {
665+
BuildMode::Disk | BuildMode::Both => {
664666
let end_path = current_path.join(filename);
665667
create_file(&end_path, &final_content)?;
666668
}
667-
BuildMode::Memory => {
669+
_ => (),
670+
}
671+
match self.build_mode {
672+
BuildMode::Memory | BuildMode::Both => {
668673
let site_path =
669674
if filename != "index.html" { site_path.join(filename) } else { site_path };
670675

671676
SITE_CONTENT.write().unwrap().insert(site_path, final_content);
672677
}
678+
_ => (),
673679
}
674680

675681
Ok(current_path)

src/cli.rs

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ pub enum Command {
8282
#[clap(short = 'O', long)]
8383
open: bool,
8484

85+
/// Also store HTML in the public/ folder (by default HTML is only stored in-memory)
86+
#[clap(short = 'H', long)]
87+
store_html: bool,
88+
8589
/// Only rebuild the minimum on change - useful when working on a specific page/section
8690
#[clap(short = 'f', long)]
8791
fast: bool,

src/cmd/serve.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ fn create_new_site(
367367
base_url: Option<&str>,
368368
config_file: &Path,
369369
include_drafts: bool,
370+
store_html: bool,
370371
mut no_port_append: bool,
371372
ws_port: Option<u16>,
372373
) -> Result<(Site, SocketAddr, String)> {
@@ -390,7 +391,7 @@ fn create_new_site(
390391
constructed_base_url.truncate(constructed_base_url.len() - 1);
391392
}
392393

393-
site.enable_serve_mode();
394+
site.enable_serve_mode(store_html);
394395
site.set_base_url(constructed_base_url.clone());
395396
if let Some(output_dir) = output_dir {
396397
if !force && output_dir.exists() {
@@ -427,6 +428,7 @@ pub fn serve(
427428
config_file: &Path,
428429
open: bool,
429430
include_drafts: bool,
431+
store_html: bool,
430432
fast_rebuild: bool,
431433
no_port_append: bool,
432434
utc_offset: UtcOffset,
@@ -442,6 +444,7 @@ pub fn serve(
442444
base_url,
443445
config_file,
444446
include_drafts,
447+
store_html,
445448
no_port_append,
446449
None,
447450
)?;
@@ -672,6 +675,7 @@ pub fn serve(
672675
base_url,
673676
config_file,
674677
include_drafts,
678+
store_html,
675679
no_port_append,
676680
ws_port,
677681
) {
@@ -916,6 +920,7 @@ mod tests {
916920
base_url.as_deref(),
917921
&config_file,
918922
include_drafts,
923+
store_html,
919924
no_port_append,
920925
ws_port,
921926
)

src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ fn main() {
8484
base_url,
8585
drafts,
8686
open,
87+
store_html,
8788
fast,
8889
no_port_append,
8990
extra_watch_path,
@@ -112,6 +113,7 @@ fn main() {
112113
&config_file,
113114
open,
114115
drafts,
116+
store_html,
115117
fast,
116118
no_port_append,
117119
UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC),

0 commit comments

Comments
 (0)