Clean up LinkPreprocessor exports and use explicit PreprocessorContext struct
This commit is contained in:
parent
9c922cf26b
commit
b98ed3f794
|
@ -23,7 +23,7 @@ use toml::Value;
|
||||||
|
|
||||||
use utils;
|
use utils;
|
||||||
use renderer::{CmdRenderer, HtmlHandlebars, RenderContext, Renderer};
|
use renderer::{CmdRenderer, HtmlHandlebars, RenderContext, Renderer};
|
||||||
use preprocess::{self, Preprocessor};
|
use preprocess::{Preprocessor, LinkPreprocessor, PreprocessorContext};
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
@ -88,20 +88,16 @@ impl MDBook {
|
||||||
let livereload = None;
|
let livereload = None;
|
||||||
|
|
||||||
let renderers = determine_renderers(&config);
|
let renderers = determine_renderers(&config);
|
||||||
|
let preprocessors = determine_preprocessors(&config);
|
||||||
|
|
||||||
let mut md_book = MDBook {
|
Ok(MDBook {
|
||||||
root,
|
root,
|
||||||
config,
|
config,
|
||||||
book,
|
book,
|
||||||
renderers,
|
renderers,
|
||||||
livereload,
|
livereload,
|
||||||
preprocessors: vec![],
|
preprocessors,
|
||||||
};
|
})
|
||||||
|
|
||||||
let preprocessors = determine_preprocessors(&md_book);
|
|
||||||
md_book.preprocessors = preprocessors;
|
|
||||||
|
|
||||||
Ok(md_book)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a flat depth-first iterator over the elements of the book,
|
/// Returns a flat depth-first iterator over the elements of the book,
|
||||||
|
@ -161,9 +157,12 @@ 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 {
|
||||||
|
src_dir: self.source_dir(),
|
||||||
|
};
|
||||||
|
|
||||||
for preprocessor in &self.preprocessors {
|
for preprocessor in &self.preprocessors {
|
||||||
preprocessor.run(&mut preprocessed_book)?;
|
preprocessor.run(&preprocess_ctx, &mut preprocessed_book)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
for renderer in &self.renderers {
|
for renderer in &self.renderers {
|
||||||
|
@ -225,9 +224,11 @@ impl MDBook {
|
||||||
let temp_dir = TempDir::new("mdbook")?;
|
let temp_dir = TempDir::new("mdbook")?;
|
||||||
|
|
||||||
let src_dir = self.source_dir();
|
let src_dir = self.source_dir();
|
||||||
let replace_all_preprocessor = preprocess::links::LinkPreprocessor::new(src_dir);
|
let preprocess_context = PreprocessorContext {
|
||||||
|
src_dir
|
||||||
|
};
|
||||||
|
|
||||||
replace_all_preprocessor.run(&mut self.book)?;
|
LinkPreprocessor::new().run(&preprocess_context, &mut self.book)?;
|
||||||
|
|
||||||
for item in self.iter() {
|
for item in self.iter() {
|
||||||
if let BookItem::Chapter(ref ch) = *item {
|
if let BookItem::Chapter(ref ch) = *item {
|
||||||
|
@ -333,16 +334,14 @@ fn determine_renderers(config: &Config) -> Vec<Box<Renderer>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Look at the `MDBook` and try to figure out what preprocessors to run.
|
/// Look at the `MDBook` and try to figure out what preprocessors to run.
|
||||||
fn determine_preprocessors(md_book: &MDBook) -> Vec<Box<Preprocessor>> {
|
fn determine_preprocessors(config: &Config) -> Vec<Box<Preprocessor>> {
|
||||||
let mut preprocessors: Vec<Box<Preprocessor>> = Vec::new();
|
let mut preprocessors: Vec<Box<Preprocessor>> = Vec::new();
|
||||||
|
|
||||||
if let Some(preprocess_array) = md_book.config.get("preprocess").and_then(|o| o.as_array()) {
|
if let Some(preprocess_array) = config.get("preprocess").and_then(|o| o.as_array()) {
|
||||||
for key in preprocess_array.iter() {
|
for key in preprocess_array.iter() {
|
||||||
match key.as_str() {
|
match key.as_str() {
|
||||||
Some(key) if key == "links" => {
|
Some(key) if key == "links" => {
|
||||||
let src_dir = md_book.source_dir();
|
preprocessors.push(Box::new(LinkPreprocessor::new()))
|
||||||
let link_preprocessor = preprocess::links::LinkPreprocessor::new(src_dir);
|
|
||||||
preprocessors.push(Box::new(link_preprocessor))
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -350,9 +349,7 @@ fn determine_preprocessors(md_book: &MDBook) -> Vec<Box<Preprocessor>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if preprocessors.is_empty() {
|
if preprocessors.is_empty() {
|
||||||
let src_dir = md_book.source_dir();
|
preprocessors.push(Box::new(LinkPreprocessor::new()))
|
||||||
let link_preprocessor = preprocess::links::LinkPreprocessor::new(src_dir);
|
|
||||||
preprocessors.push(Box::new(link_preprocessor))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
preprocessors
|
preprocessors
|
||||||
|
|
|
@ -5,28 +5,26 @@ use utils::fs::file_to_string;
|
||||||
use utils::take_lines;
|
use utils::take_lines;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
use super::Preprocessor;
|
use super::{Preprocessor, PreprocessorContext};
|
||||||
use book::{Book, BookItem};
|
use book::{Book, BookItem};
|
||||||
|
|
||||||
const ESCAPE_CHAR: char = '\\';
|
const ESCAPE_CHAR: char = '\\';
|
||||||
|
|
||||||
pub struct LinkPreprocessor {
|
pub struct LinkPreprocessor;
|
||||||
src_dir: PathBuf
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LinkPreprocessor {
|
impl LinkPreprocessor {
|
||||||
pub fn new<P: Into<PathBuf>>(src_dir: P) -> Self {
|
pub fn new() -> Self {
|
||||||
LinkPreprocessor { src_dir: src_dir.into() }
|
LinkPreprocessor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Preprocessor for LinkPreprocessor {
|
impl Preprocessor for LinkPreprocessor {
|
||||||
fn run(&self, book: &mut Book) -> Result<()> {
|
fn run(&self, ctx: &PreprocessorContext, book: &mut Book) -> Result<()> {
|
||||||
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| self.src_dir.join(dir))
|
.map(|dir| ctx.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
|
||||||
|
|
|
@ -1,9 +1,16 @@
|
||||||
pub mod links;
|
pub use self::links::LinkPreprocessor;
|
||||||
|
|
||||||
|
mod links;
|
||||||
|
|
||||||
use book::Book;
|
use book::Book;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
pub struct PreprocessorContext {
|
||||||
|
pub src_dir: PathBuf
|
||||||
|
}
|
||||||
|
|
||||||
pub trait Preprocessor {
|
pub trait Preprocessor {
|
||||||
fn run(&self, book: &mut Book) -> Result<()>;
|
fn run(&self, ctx: &PreprocessorContext, book: &mut Book) -> Result<()>;
|
||||||
}
|
}
|
Loading…
Reference in New Issue