Skip to content

Commit cad76a9

Browse files
Factor out replace_all preprocessor
1 parent 01df904 commit cad76a9

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

src/book/mod.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl MDBook {
8989

9090
let renderers = determine_renderers(&config);
9191

92-
let preprocessors = vec![];
92+
let preprocessors = determine_preprocessors(&config);
9393

9494
Ok(MDBook {
9595
root,
@@ -215,16 +215,18 @@ impl MDBook {
215215

216216
let temp_dir = TempDir::new("mdbook")?;
217217

218+
let replace_all_preprocessor = preprocess::links::ReplaceAllPreprocessor {
219+
src_dir: self.source_dir(),
220+
};
221+
222+
replace_all_preprocessor.run(&mut self.book)?;
223+
218224
for item in self.iter() {
219225
if let BookItem::Chapter(ref ch) = *item {
220226
if !ch.path.as_os_str().is_empty() {
221227
let path = self.source_dir().join(&ch.path);
222-
let base = path.parent()
223-
.ok_or_else(|| String::from("Invalid bookitem path!"))?;
224-
let content = utils::fs::file_to_string(&path)?;
225-
// Parse and expand links
226-
let content = preprocess::links::replace_all(&content, base)?;
227228
println!("[*]: Testing file: {:?}", path);
229+
let content = utils::fs::file_to_string(&path)?;
228230

229231
// write preprocessed file to tempdir
230232
let path = temp_dir.path().join(&ch.path);
@@ -322,6 +324,13 @@ fn determine_renderers(config: &Config) -> Vec<Box<Renderer>> {
322324
renderers
323325
}
324326

327+
/// Look at the `Config` and try to figure out what preprocessors to run.
328+
fn determine_preprocessors(config: &Config) -> Vec<Box<Preprocessor>> {
329+
let mut preprocessors: Vec<Box<Preprocessor>> = Vec::new();
330+
331+
preprocessors
332+
}
333+
325334
fn interpret_custom_renderer(key: &str, table: &Value) -> Box<Renderer> {
326335
// look for the `command` field, falling back to using the key
327336
// prepended by "mdbook-"

src/preprocess/links.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,35 @@ use utils::fs::file_to_string;
55
use utils::take_lines;
66
use errors::*;
77

8+
use super::Preprocessor;
9+
use book::{Book, BookItem};
10+
811
const ESCAPE_CHAR: char = '\\';
912

10-
pub fn replace_all<P: AsRef<Path>>(s: &str, path: P) -> Result<String> {
13+
pub struct ReplaceAllPreprocessor {
14+
pub src_dir: PathBuf
15+
}
16+
17+
impl Preprocessor for ReplaceAllPreprocessor {
18+
fn run(&self, book: &mut Book) -> Result<()> {
19+
for section in &mut book.sections {
20+
match *section {
21+
BookItem::Chapter(ref mut ch) => {
22+
let content = ::std::mem::replace(&mut ch.content, String::new());
23+
let base = ch.path.parent()
24+
.map(|dir| self.src_dir.join(dir))
25+
.ok_or_else(|| String::from("Invalid bookitem path!"))?;
26+
ch.content = replace_all(&content, base)?
27+
}
28+
_ => {}
29+
}
30+
}
31+
32+
Ok(())
33+
}
34+
}
35+
36+
fn replace_all<P: AsRef<Path>>(s: &str, path: P) -> Result<String> {
1137
// When replacing one thing in a string by something with a different length,
1238
// the indices after that will not correspond,
1339
// we therefore have to store the difference to correct this

src/preprocess/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
pub mod links;
22

3-
pub trait Preprocessor {
3+
use book::Book;
4+
use errors::*;
5+
46

7+
pub trait Preprocessor {
8+
fn run(&self, book: &mut Book) -> Result<()>;
59
}

src/renderer/html_handlebars/hbs_renderer.rs

-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use renderer::html_handlebars::helpers;
2-
use preprocess;
32
use renderer::{RenderContext, Renderer};
43
use book::{Book, BookItem, Chapter};
54
use config::{Config, HtmlConfig, Playpen};
@@ -50,12 +49,6 @@ impl HtmlHandlebars {
5049
match *item {
5150
BookItem::Chapter(ref ch) => {
5251
let content = ch.content.clone();
53-
let base = ch.path.parent()
54-
.map(|dir| ctx.src_dir.join(dir))
55-
.expect("All chapters must have a parent directory");
56-
57-
// Parse and expand links
58-
let content = preprocess::links::replace_all(&content, base)?;
5952
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
6053
print_content.push_str(&content);
6154

@@ -322,7 +315,6 @@ impl Renderer for HtmlHandlebars {
322315
let ctx = RenderItemContext {
323316
handlebars: &handlebars,
324317
destination: destination.to_path_buf(),
325-
src_dir: src_dir.clone(),
326318
data: data.clone(),
327319
is_index: i == 0,
328320
html_config: html_config.clone(),
@@ -634,7 +626,6 @@ fn partition_source(s: &str) -> (String, String) {
634626
struct RenderItemContext<'a> {
635627
handlebars: &'a Handlebars,
636628
destination: PathBuf,
637-
src_dir: PathBuf,
638629
data: serde_json::Map<String, serde_json::Value>,
639630
is_index: bool,
640631
html_config: HtmlConfig,

0 commit comments

Comments
 (0)