From 0d62578c7b4fa560535e542b51d366352203fa8e Mon Sep 17 00:00:00 2001 From: Jaime Valdemoros Date: Wed, 17 Jan 2018 09:44:52 +0000 Subject: [PATCH] Make Preprocessor context store config and root --- src/book/mod.rs | 9 ++------- src/preprocess/links.rs | 4 +++- src/preprocess/mod.rs | 10 +++++++++- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 2bf1b032..43f5932a 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -157,9 +157,7 @@ impl MDBook { debug!("[fn]: build"); let mut preprocessed_book = self.book.clone(); - let preprocess_ctx = PreprocessorContext { - src_dir: self.source_dir(), - }; + let preprocess_ctx = PreprocessorContext::new(self.root.clone(), self.config.clone()); for preprocessor in &self.preprocessors { preprocessor.run(&preprocess_ctx, &mut preprocessed_book)?; @@ -222,10 +220,7 @@ impl MDBook { let temp_dir = TempDir::new("mdbook")?; - let src_dir = self.source_dir(); - let preprocess_context = PreprocessorContext { - src_dir - }; + let preprocess_context = PreprocessorContext::new(self.root.clone(), self.config.clone()); LinkPreprocessor::new().run(&preprocess_context, &mut self.book)?; diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 72bc6c8f..c3cab8bf 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -24,11 +24,13 @@ impl Preprocessor for LinkPreprocessor { } fn run(&self, ctx: &PreprocessorContext, book: &mut Book) -> Result<()> { + let src_dir = ctx.root.join(&ctx.config.book.src); + for section in &mut book.sections { match *section { BookItem::Chapter(ref mut ch) => { let base = ch.path.parent() - .map(|dir| ctx.src_dir.join(dir)) + .map(|dir| src_dir.join(dir)) .ok_or_else(|| String::from("Invalid bookitem path!"))?; let content = replace_all(&ch.content, base)?; ch.content = content diff --git a/src/preprocess/mod.rs b/src/preprocess/mod.rs index c31080a1..091604d6 100644 --- a/src/preprocess/mod.rs +++ b/src/preprocess/mod.rs @@ -3,12 +3,20 @@ pub use self::links::LinkPreprocessor; mod links; use book::Book; +use config::Config; use errors::*; use std::path::PathBuf; pub struct PreprocessorContext { - pub src_dir: PathBuf + pub root: PathBuf, + pub config: Config, +} + +impl PreprocessorContext { + pub fn new(root: PathBuf, config: Config) -> Self { + PreprocessorContext { root, config } + } } pub trait Preprocessor {