Make Preprocessor context store config and root

This commit is contained in:
Jaime Valdemoros 2018-01-17 09:44:52 +00:00
parent 47cc57177d
commit 0d62578c7b
3 changed files with 14 additions and 9 deletions

View File

@ -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)?;

View File

@ -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

View File

@ -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 {