Run preprocessors in `mdbook test`

While adding support for translations[1] to Comprehensive Rust 🦀, I
noticed that `mdbook test` doesn’t execute preprocessors the same way
as `mdbook build`.

This PR makes the two commands use the same code to find and execute
preprocessors.

[1]: https://github.com/google/comprehensive-rust/pull/130
This commit is contained in:
Martin Geisler 2023-01-15 11:44:46 +01:00
parent 41a6f0d43e
commit b09aa0e65c
1 changed files with 27 additions and 10 deletions

View File

@ -196,21 +196,26 @@ impl MDBook {
Ok(()) Ok(())
} }
/// Run the entire build process for a particular [`Renderer`]. /// Run preprocessors and return the final book.
pub fn execute_build_process(&self, renderer: &dyn Renderer) -> Result<()> { pub fn preprocess_book(&self, renderer: &dyn Renderer) -> Result<(Book, PreprocessorContext)> {
let mut preprocessed_book = self.book.clone();
let preprocess_ctx = PreprocessorContext::new( let preprocess_ctx = PreprocessorContext::new(
self.root.clone(), self.root.clone(),
self.config.clone(), self.config.clone(),
renderer.name().to_string(), renderer.name().to_string(),
); );
let mut preprocessed_book = self.book.clone();
for preprocessor in &self.preprocessors { for preprocessor in &self.preprocessors {
if preprocessor_should_run(&**preprocessor, renderer, &self.config) { if preprocessor_should_run(&**preprocessor, renderer, &self.config) {
debug!("Running the {} preprocessor.", preprocessor.name()); debug!("Running the {} preprocessor.", preprocessor.name());
preprocessed_book = preprocessor.run(&preprocess_ctx, preprocessed_book)?; preprocessed_book = preprocessor.run(&preprocess_ctx, preprocessed_book)?;
} }
} }
Ok((preprocessed_book, preprocess_ctx))
}
/// Run the entire build process for a particular [`Renderer`].
pub fn execute_build_process(&self, renderer: &dyn Renderer) -> Result<()> {
let (preprocessed_book, preprocess_ctx) = self.preprocess_book(renderer)?;
let name = renderer.name(); let name = renderer.name();
let build_dir = self.build_dir_for(name); let build_dir = self.build_dir_for(name);
@ -264,13 +269,25 @@ impl MDBook {
let mut chapter_found = false; let mut chapter_found = false;
struct TestRenderer;
impl Renderer for TestRenderer {
// FIXME: Is "test" the proper renderer name to use here? // FIXME: Is "test" the proper renderer name to use here?
let preprocess_context = fn name(&self) -> &str {
PreprocessorContext::new(self.root.clone(), self.config.clone(), "test".to_string()); "test"
}
let book = LinkPreprocessor::new().run(&preprocess_context, self.book.clone())?; fn render(&self, _: &RenderContext) -> Result<()> {
// Index Preprocessor is disabled so that chapter paths continue to point to the Ok(())
// actual markdown files. }
}
// Index Preprocessor is disabled so that chapter paths
// continue to point to the actual markdown files.
self.preprocessors = determine_preprocessors(&self.config)?
.into_iter()
.filter(|pre| pre.name() != IndexPreprocessor::NAME)
.collect();
let (book, _) = self.preprocess_book(&TestRenderer)?;
let mut failed = false; let mut failed = false;
for item in book.iter() { for item in book.iter() {