Skip to content

Commit 769ebc6

Browse files
ppom0Keats
andauthored
Implement zola serve --store-html (#2750)
* Implement zola serve --store-html Fixes #2377 * Apply maintainer suggestions * fix tests --------- Co-authored-by: Vincent Prouillet <[email protected]>
1 parent 8e3357e commit 769ebc6

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Exclude paginated pages in sitemap by default
1212
- Allow treating a missing highlight language as error
1313
- Handle more editors with change detection in `zola serve`
14+
- Add argument to `zola serve` to write HTML files to disk
1415
- Add optional parsing of Markdown definition lists
1516

1617

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)]
@@ -117,10 +119,10 @@ impl Site {
117119
}
118120

119121
/// Enable some `zola serve` related options
120-
pub fn enable_serve_mode(&mut self) {
122+
pub fn enable_serve_mode(&mut self, build_mode: BuildMode) {
121123
SITE_CONTENT.write().unwrap().clear();
122124
self.config.enable_serve_mode();
123-
self.build_mode = BuildMode::Memory;
125+
self.build_mode = build_mode;
124126
}
125127

126128
/// Set the site to load the drafts.
@@ -668,16 +670,20 @@ impl Site {
668670
};
669671

670672
match self.build_mode {
671-
BuildMode::Disk => {
673+
BuildMode::Disk | BuildMode::Both => {
672674
let end_path = current_path.join(filename);
673675
create_file(&end_path, &final_content)?;
674676
}
675-
BuildMode::Memory => {
677+
_ => (),
678+
}
679+
match self.build_mode {
680+
BuildMode::Memory | BuildMode::Both => {
676681
let site_path =
677682
if filename != "index.html" { site_path.join(filename) } else { site_path };
678683

679684
SITE_CONTENT.write().unwrap().insert(site_path, final_content);
680685
}
686+
_ => (),
681687
}
682688

683689
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(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

+7-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use ws::{Message, Sender, WebSocket};
4747

4848
use errors::{anyhow, Context, Error, Result};
4949
use site::sass::compile_sass;
50-
use site::{Site, SITE_CONTENT};
50+
use site::{BuildMode, Site, SITE_CONTENT};
5151
use utils::fs::{clean_site_output_folder, copy_file, create_directory};
5252

5353
use crate::fs_utils::{filter_events, ChangeKind, SimpleFileSystemEventKind};
@@ -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(if store_html { BuildMode::Both } else { BuildMode::Memory });
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+
false,
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)