Skip to content

Commit 91b4d27

Browse files
author
Pierre Gradot
committed
Add error_on_missing_preprocessor flag
1 parent 93d4647 commit 91b4d27

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

examples/nop-preprocessor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! {
5757
let renderer = sub_args
5858
.get_one::<String>("renderer")
5959
.expect("Required argument");
60-
let supported = pre.supports_renderer(renderer);
60+
let supported = pre.supports_renderer(renderer, false);
6161

6262
// Signal whether the renderer is supported by exiting with 1 or 0.
6363
if supported {
@@ -99,7 +99,7 @@ mod nop_lib {
9999
Ok(book)
100100
}
101101

102-
fn supports_renderer(&self, renderer: &str) -> bool {
102+
fn supports_renderer(&self, renderer: &str, _error_on_missing_preprocessor: bool) -> bool {
103103
renderer != "not-supported"
104104
}
105105
}

src/book/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,8 @@ fn preprocessor_should_run(
606606
) -> bool {
607607
// default preprocessors should be run by default (if supported)
608608
if cfg.build.use_default_preprocessors && is_default_preprocessor(preprocessor) {
609-
return preprocessor.supports_renderer(renderer.name());
609+
return preprocessor
610+
.supports_renderer(renderer.name(), cfg.build.error_on_missing_preprocessor);
610611
}
611612

612613
let key = format!("preprocessor.{}.renderers", preprocessor.name());
@@ -619,7 +620,7 @@ fn preprocessor_should_run(
619620
.any(|name| name == renderer_name);
620621
}
621622

622-
preprocessor.supports_renderer(renderer_name)
623+
preprocessor.supports_renderer(renderer_name, cfg.build.error_on_missing_preprocessor)
623624
}
624625

625626
#[cfg(test)]
@@ -877,7 +878,7 @@ mod tests {
877878
unimplemented!()
878879
}
879880

880-
fn supports_renderer(&self, _renderer: &str) -> bool {
881+
fn supports_renderer(&self, _renderer: &str, _error_on_missing_preprocessor: bool) -> bool {
881882
self.0
882883
}
883884
}

src/config.rs

+9
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,11 @@ pub struct BuildConfig {
480480
pub use_default_preprocessors: bool,
481481
/// Extra directories to trigger rebuild when watching/serving
482482
pub extra_watch_dirs: Vec<PathBuf>,
483+
/// Should missing a preprocessor be considered an error?
484+
/// By default, the application exits if a preprocessor is missing.
485+
/// Set this flag to ̀false` to raise a warning instead and continue generation,
486+
/// even if the book may be generated incorrectly.
487+
pub error_on_missing_preprocessor: bool,
483488
}
484489

485490
impl Default for BuildConfig {
@@ -489,6 +494,7 @@ impl Default for BuildConfig {
489494
create_missing: true,
490495
use_default_preprocessors: true,
491496
extra_watch_dirs: Vec::new(),
497+
error_on_missing_preprocessor: true,
492498
}
493499
}
494500
}
@@ -815,6 +821,7 @@ mod tests {
815821
build-dir = "outputs"
816822
create-missing = false
817823
use-default-preprocessors = true
824+
error-on-missing-preprocessor = false
818825
819826
[output.html]
820827
theme = "./themedir"
@@ -856,6 +863,7 @@ mod tests {
856863
create_missing: false,
857864
use_default_preprocessors: true,
858865
extra_watch_dirs: Vec::new(),
866+
error_on_missing_preprocessor: false,
859867
};
860868
let rust_should_be = RustConfig { edition: None };
861869
let playground_should_be = Playground {
@@ -1067,6 +1075,7 @@ mod tests {
10671075
create_missing: true,
10681076
use_default_preprocessors: true,
10691077
extra_watch_dirs: Vec::new(),
1078+
error_on_missing_preprocessor: true,
10701079
};
10711080

10721081
let html_should_be = HtmlConfig {

src/preprocess/cmd.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl Preprocessor for CmdPreprocessor {
134134
})
135135
}
136136

137-
fn supports_renderer(&self, renderer: &str) -> bool {
137+
fn supports_renderer(&self, renderer: &str, error_on_missing_preprocessor: bool) -> bool {
138138
debug!(
139139
"Checking if the \"{}\" preprocessor supports \"{}\"",
140140
self.name(),
@@ -164,11 +164,17 @@ impl Preprocessor for CmdPreprocessor {
164164

165165
if let Err(ref e) = outcome {
166166
if e.kind() == io::ErrorKind::NotFound {
167-
error!(
168-
"The command {} wasn't found, is the \"{}\" preprocessor installed?",
167+
let message = format!(
168+
"The command \"{}\" wasn't found, is the \"{}\" preprocessor installed?",
169169
self.cmd, self.name
170170
);
171-
std::process::exit(1);
171+
172+
if error_on_missing_preprocessor {
173+
error!("{message}");
174+
std::process::exit(1);
175+
} else {
176+
warn!("{message}");
177+
}
172178
}
173179
}
174180

src/preprocess/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ pub trait Preprocessor {
6363
/// particular renderer.
6464
///
6565
/// By default, always returns `true`.
66-
fn supports_renderer(&self, _renderer: &str) -> bool {
66+
/// Set `error_on_missing_preprocessor` to `true` to exit the application is a preprocessor is missing,
67+
/// or to `false` to simply raise a warning and continue generation.
68+
fn supports_renderer(&self, _renderer: &str, _error_on_missing_preprocessor: bool) -> bool {
6769
true
6870
}
6971
}

tests/custom_preprocessors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn example() -> CmdPreprocessor {
1515
fn example_supports_whatever() {
1616
let cmd = example();
1717

18-
let got = cmd.supports_renderer("whatever");
18+
let got = cmd.supports_renderer("whatever", false);
1919

2020
assert_eq!(got, true);
2121
}
@@ -24,7 +24,7 @@ fn example_supports_whatever() {
2424
fn example_doesnt_support_not_supported() {
2525
let cmd = example();
2626

27-
let got = cmd.supports_renderer("not-supported");
27+
let got = cmd.supports_renderer("not-supported", false);
2828

2929
assert_eq!(got, false);
3030
}

tests/init.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn run_mdbook_init_with_custom_book_and_src_locations() {
9494
let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap();
9595
assert_eq!(
9696
contents,
97-
"[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"in\"\n\n[build]\nbuild-dir = \"out\"\ncreate-missing = true\nextra-watch-dirs = []\nuse-default-preprocessors = true\n"
97+
"[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"in\"\n\n[build]\nbuild-dir = \"out\"\ncreate-missing = true\nerror-on-missing-preprocessor = true\nextra-watch-dirs = []\nuse-default-preprocessors = true\n"
9898
);
9999
}
100100

0 commit comments

Comments
 (0)