From cad76a9f6c2aac121852b21aaa8b6d24604341a9 Mon Sep 17 00:00:00 2001 From: Jaime Valdemoros Date: Sun, 7 Jan 2018 16:21:46 +0000 Subject: [PATCH] Factor out replace_all preprocessor --- src/book/mod.rs | 21 ++++++++++----- src/preprocess/links.rs | 28 +++++++++++++++++++- src/preprocess/mod.rs | 6 ++++- src/renderer/html_handlebars/hbs_renderer.rs | 9 ------- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index a50726d3..10a59dfc 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -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> { renderers } +/// Look at the `Config` and try to figure out what preprocessors to run. +fn determine_preprocessors(config: &Config) -> Vec> { + let mut preprocessors: Vec> = Vec::new(); + + preprocessors +} + fn interpret_custom_renderer(key: &str, table: &Value) -> Box { // look for the `command` field, falling back to using the key // prepended by "mdbook-" diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index fbc1a2f5..6a92defe 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -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>(s: &str, path: P) -> Result { +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>(s: &str, path: P) -> Result { // 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 diff --git a/src/preprocess/mod.rs b/src/preprocess/mod.rs index 6f4fb21d..acc954ec 100644 --- a/src/preprocess/mod.rs +++ b/src/preprocess/mod.rs @@ -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<()>; } \ No newline at end of file diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 694186ee..d0ec14f7 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -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, is_index: bool, html_config: HtmlConfig,