Factor out replace_all preprocessor

This commit is contained in:
Jaime Valdemoros 2018-01-07 16:21:46 +00:00
parent 01df904bb3
commit cad76a9f6c
4 changed files with 47 additions and 17 deletions

View File

@ -89,7 +89,7 @@ impl MDBook {
let renderers = determine_renderers(&config);
let preprocessors = vec![];
let preprocessors = determine_preprocessors(&config);
Ok(MDBook {
root,
@ -215,16 +215,18 @@ impl MDBook {
let temp_dir = TempDir::new("mdbook")?;
let replace_all_preprocessor = preprocess::links::ReplaceAllPreprocessor {
src_dir: self.source_dir(),
};
replace_all_preprocessor.run(&mut self.book)?;
for item in self.iter() {
if let BookItem::Chapter(ref ch) = *item {
if !ch.path.as_os_str().is_empty() {
let path = self.source_dir().join(&ch.path);
let base = path.parent()
.ok_or_else(|| String::from("Invalid bookitem path!"))?;
let content = utils::fs::file_to_string(&path)?;
// Parse and expand links
let content = preprocess::links::replace_all(&content, base)?;
println!("[*]: Testing file: {:?}", path);
let content = utils::fs::file_to_string(&path)?;
// write preprocessed file to tempdir
let path = temp_dir.path().join(&ch.path);
@ -322,6 +324,13 @@ fn determine_renderers(config: &Config) -> Vec<Box<Renderer>> {
renderers
}
/// Look at the `Config` and try to figure out what preprocessors to run.
fn determine_preprocessors(config: &Config) -> Vec<Box<Preprocessor>> {
let mut preprocessors: Vec<Box<Preprocessor>> = Vec::new();
preprocessors
}
fn interpret_custom_renderer(key: &str, table: &Value) -> Box<Renderer> {
// look for the `command` field, falling back to using the key
// prepended by "mdbook-"

View File

@ -5,9 +5,35 @@ use utils::fs::file_to_string;
use utils::take_lines;
use errors::*;
use super::Preprocessor;
use book::{Book, BookItem};
const ESCAPE_CHAR: char = '\\';
pub fn replace_all<P: AsRef<Path>>(s: &str, path: P) -> Result<String> {
pub struct ReplaceAllPreprocessor {
pub src_dir: PathBuf
}
impl Preprocessor for ReplaceAllPreprocessor {
fn run(&self, book: &mut Book) -> Result<()> {
for section in &mut book.sections {
match *section {
BookItem::Chapter(ref mut ch) => {
let content = ::std::mem::replace(&mut ch.content, String::new());
let base = ch.path.parent()
.map(|dir| self.src_dir.join(dir))
.ok_or_else(|| String::from("Invalid bookitem path!"))?;
ch.content = replace_all(&content, base)?
}
_ => {}
}
}
Ok(())
}
}
fn replace_all<P: AsRef<Path>>(s: &str, path: P) -> Result<String> {
// When replacing one thing in a string by something with a different length,
// the indices after that will not correspond,
// we therefore have to store the difference to correct this

View File

@ -1,5 +1,9 @@
pub mod links;
pub trait Preprocessor {
use book::Book;
use errors::*;
pub trait Preprocessor {
fn run(&self, book: &mut Book) -> Result<()>;
}

View File

@ -1,5 +1,4 @@
use renderer::html_handlebars::helpers;
use preprocess;
use renderer::{RenderContext, Renderer};
use book::{Book, BookItem, Chapter};
use config::{Config, HtmlConfig, Playpen};
@ -50,12 +49,6 @@ impl HtmlHandlebars {
match *item {
BookItem::Chapter(ref ch) => {
let content = ch.content.clone();
let base = ch.path.parent()
.map(|dir| ctx.src_dir.join(dir))
.expect("All chapters must have a parent directory");
// Parse and expand links
let content = preprocess::links::replace_all(&content, base)?;
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
print_content.push_str(&content);
@ -322,7 +315,6 @@ impl Renderer for HtmlHandlebars {
let ctx = RenderItemContext {
handlebars: &handlebars,
destination: destination.to_path_buf(),
src_dir: src_dir.clone(),
data: data.clone(),
is_index: i == 0,
html_config: html_config.clone(),
@ -634,7 +626,6 @@ fn partition_source(s: &str) -> (String, String) {
struct RenderItemContext<'a> {
handlebars: &'a Handlebars,
destination: PathBuf,
src_dir: PathBuf,
data: serde_json::Map<String, serde_json::Value>,
is_index: bool,
html_config: HtmlConfig,