Skip to content

Fix #1087 stage preprocessor generated files in temp dir #1144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,18 @@ impl MDBook {

/// Run the entire build process for a particular `Renderer`.
pub fn execute_build_process(&self, renderer: &dyn Renderer) -> Result<()> {
// The temporary preprocessor output directory
let preproc_out_dir = TempFileBuilder::new()
.prefix("mdbook-pre-")
.tempdir()
.expect("Failed to create temporary output dir for preprocessor");

let mut preprocessed_book = self.book.clone();
let preprocess_ctx = PreprocessorContext::new(
self.root.clone(),
// TempDir::into_path disables the auto delete option, so we need to
// use the long way to get to the path.
preproc_out_dir.path().to_path_buf(),
self.config.clone(),
renderer.name().to_string(),
);
Expand All @@ -197,12 +206,17 @@ impl MDBook {
}

info!("Running the {} backend", renderer.name());
self.render(&preprocessed_book, renderer)?;
self.render(&preprocessed_book, renderer, &preprocess_ctx.output_dir)?;

Ok(())
}

fn render(&self, preprocessed_book: &Book, renderer: &dyn Renderer) -> Result<()> {
fn render(
&self,
preprocessed_book: &Book,
renderer: &dyn Renderer,
preproc_out_dir: &PathBuf,
) -> Result<()> {
let name = renderer.name();
let build_dir = self.build_dir_for(name);

Expand All @@ -211,6 +225,7 @@ impl MDBook {
preprocessed_book.clone(),
self.config.clone(),
build_dir,
preproc_out_dir.clone(),
);

renderer
Expand Down Expand Up @@ -241,10 +256,15 @@ impl MDBook {
.collect();

let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir()?;
let preproc_out_dir = TempFileBuilder::new().prefix("mdbook-pre-").tempdir()?;

// FIXME: Is "test" the proper renderer name to use here?
let preprocess_context =
PreprocessorContext::new(self.root.clone(), self.config.clone(), "test".to_string());
let preprocess_context = PreprocessorContext::new(
self.root.clone(),
preproc_out_dir.path().to_path_buf(), //TempDir::into_path disables the auto delete option
self.config.clone(),
"test".to_string(),
);

let book = LinkPreprocessor::new().run(&preprocess_context, self.book.clone())?;
// Index Preprocessor is disabled so that chapter paths continue to point to the
Expand Down
2 changes: 2 additions & 0 deletions src/preprocess/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ mod tests {
use super::*;
use crate::MDBook;
use std::path::Path;
use std::path::PathBuf;

fn book_example() -> MDBook {
let example = Path::new(env!("CARGO_MANIFEST_DIR")).join("book-example");
Expand All @@ -182,6 +183,7 @@ mod tests {
let md = book_example();
let ctx = PreprocessorContext::new(
md.root.clone(),
PathBuf::from(""),
md.config.clone(),
"some-renderer".to_string(),
);
Expand Down
11 changes: 10 additions & 1 deletion src/preprocess/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ use std::path::PathBuf;
pub struct PreprocessorContext {
/// The location of the book directory on disk.
pub root: PathBuf,
/// The directory where a preprocessor can write generated files to
/// Generated files are copied directly to the book output dir
pub output_dir: PathBuf,
/// The book configuration (`book.toml`).
pub config: Config,
/// The `Renderer` this preprocessor is being used with.
Expand All @@ -32,9 +35,15 @@ pub struct PreprocessorContext {

impl PreprocessorContext {
/// Create a new `PreprocessorContext`.
pub(crate) fn new(root: PathBuf, config: Config, renderer: String) -> Self {
pub(crate) fn new(
root: PathBuf,
output_dir: PathBuf,
config: Config,
renderer: String,
) -> Self {
PreprocessorContext {
root,
output_dir: output_dir,
config,
renderer,
mdbook_version: crate::MDBOOK_VERSION.to_string(),
Expand Down
1 change: 1 addition & 0 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ impl Renderer for HtmlHandlebars {

// Copy all remaining files, avoid a recursive copy from/to the book build dir
utils::fs::copy_files_except_ext(&src_dir, &destination, true, Some(&build_dir), &["md"])?;
utils::fs::copy_files_except_ext(&ctx.preproc_output_dir, &destination, true, None, &[])?;

Ok(())
}
Expand Down
11 changes: 10 additions & 1 deletion src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub struct RenderContext {
pub version: String,
/// The book's root directory.
pub root: PathBuf,
/// Location of preprocessor generated output files (e.g. images)
preproc_output_dir: PathBuf,
/// A loaded representation of the book itself.
pub book: Book,
/// The loaded configuration file.
Expand All @@ -72,7 +74,13 @@ pub struct RenderContext {

impl RenderContext {
/// Create a new `RenderContext`.
pub fn new<P, Q>(root: P, book: Book, config: Config, destination: Q) -> RenderContext
pub fn new<P, Q>(
root: P,
book: Book,
config: Config,
destination: Q,
preproc_output_dir: P,
) -> RenderContext
where
P: Into<PathBuf>,
Q: Into<PathBuf>,
Expand All @@ -83,6 +91,7 @@ impl RenderContext {
version: crate::MDBOOK_VERSION.to_string(),
root: root.into(),
destination: destination.into(),
preproc_output_dir: preproc_output_dir.into(),
__non_exhaustive: (),
}
}
Expand Down