Make Preprocessor context store config and root
This commit is contained in:
parent
47cc57177d
commit
0d62578c7b
|
@ -157,9 +157,7 @@ impl MDBook {
|
||||||
debug!("[fn]: build");
|
debug!("[fn]: build");
|
||||||
|
|
||||||
let mut preprocessed_book = self.book.clone();
|
let mut preprocessed_book = self.book.clone();
|
||||||
let preprocess_ctx = PreprocessorContext {
|
let preprocess_ctx = PreprocessorContext::new(self.root.clone(), self.config.clone());
|
||||||
src_dir: self.source_dir(),
|
|
||||||
};
|
|
||||||
|
|
||||||
for preprocessor in &self.preprocessors {
|
for preprocessor in &self.preprocessors {
|
||||||
preprocessor.run(&preprocess_ctx, &mut preprocessed_book)?;
|
preprocessor.run(&preprocess_ctx, &mut preprocessed_book)?;
|
||||||
|
@ -222,10 +220,7 @@ impl MDBook {
|
||||||
|
|
||||||
let temp_dir = TempDir::new("mdbook")?;
|
let temp_dir = TempDir::new("mdbook")?;
|
||||||
|
|
||||||
let src_dir = self.source_dir();
|
let preprocess_context = PreprocessorContext::new(self.root.clone(), self.config.clone());
|
||||||
let preprocess_context = PreprocessorContext {
|
|
||||||
src_dir
|
|
||||||
};
|
|
||||||
|
|
||||||
LinkPreprocessor::new().run(&preprocess_context, &mut self.book)?;
|
LinkPreprocessor::new().run(&preprocess_context, &mut self.book)?;
|
||||||
|
|
||||||
|
|
|
@ -24,11 +24,13 @@ impl Preprocessor for LinkPreprocessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, ctx: &PreprocessorContext, book: &mut Book) -> Result<()> {
|
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 {
|
for section in &mut book.sections {
|
||||||
match *section {
|
match *section {
|
||||||
BookItem::Chapter(ref mut ch) => {
|
BookItem::Chapter(ref mut ch) => {
|
||||||
let base = ch.path.parent()
|
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!"))?;
|
.ok_or_else(|| String::from("Invalid bookitem path!"))?;
|
||||||
let content = replace_all(&ch.content, base)?;
|
let content = replace_all(&ch.content, base)?;
|
||||||
ch.content = content
|
ch.content = content
|
||||||
|
|
|
@ -3,12 +3,20 @@ pub use self::links::LinkPreprocessor;
|
||||||
mod links;
|
mod links;
|
||||||
|
|
||||||
use book::Book;
|
use book::Book;
|
||||||
|
use config::Config;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub struct PreprocessorContext {
|
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 {
|
pub trait Preprocessor {
|
||||||
|
|
Loading…
Reference in New Issue