Skip to content

Commit ecf6626

Browse files
author
Pierre Gradot
committed
Flag defaults to false but is forced to true for new books
1 parent 91b4d27 commit ecf6626

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

src/cmd/init.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::get_book_dir;
22
use clap::{arg, ArgMatches, Command as ClapCommand};
3-
use mdbook::config;
43
use mdbook::errors::Result;
5-
use mdbook::MDBook;
4+
use mdbook::{Config, MDBook};
65
use std::io;
76
use std::io::Write;
87
use std::process::Command;
@@ -31,7 +30,12 @@ pub fn make_subcommand() -> ClapCommand {
3130
pub fn execute(args: &ArgMatches) -> Result<()> {
3231
let book_dir = get_book_dir(args);
3332
let mut builder = MDBook::init(&book_dir);
34-
let mut config = config::Config::default();
33+
34+
let mut config = Config::default();
35+
// We want new books to raise an error if a preprocessor is missing, but we want old books to simply emit a warning.
36+
// This is why BuildConfig::default() sets it to false and why we force it to true here
37+
config.build.error_on_missing_preprocessor = true;
38+
3539
// If flag `--theme` is present, copy theme to src
3640
if args.get_flag("theme") {
3741
let theme_dir = book_dir.join("theme");

src/config.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,10 @@ pub struct BuildConfig {
481481
/// Extra directories to trigger rebuild when watching/serving
482482
pub extra_watch_dirs: Vec<PathBuf>,
483483
/// 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,
484+
/// By default, the application raises a warning instead and continue generation,
486485
/// even if the book may be generated incorrectly.
486+
/// Set this flag to ̀false` to consider this an error, and exits the application
487+
/// if a preprocessor is missing.
487488
pub error_on_missing_preprocessor: bool,
488489
}
489490

@@ -494,7 +495,7 @@ impl Default for BuildConfig {
494495
create_missing: true,
495496
use_default_preprocessors: true,
496497
extra_watch_dirs: Vec::new(),
497-
error_on_missing_preprocessor: true,
498+
error_on_missing_preprocessor: false,
498499
}
499500
}
500501
}
@@ -1075,7 +1076,8 @@ mod tests {
10751076
create_missing: true,
10761077
use_default_preprocessors: true,
10771078
extra_watch_dirs: Vec::new(),
1078-
error_on_missing_preprocessor: true,
1079+
error_on_missing_preprocessor: false, // This flag is missing from "src" string,
1080+
// so it should be false to ensure backward compatibility
10791081
};
10801082

10811083
let html_should_be = HtmlConfig {

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\nerror-on-missing-preprocessor = 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 = false\nextra-watch-dirs = []\nuse-default-preprocessors = true\n"
9898
);
9999
}
100100

0 commit comments

Comments
 (0)