Factor out replace_all preprocessor
This commit is contained in:
parent
01df904bb3
commit
cad76a9f6c
|
@ -89,7 +89,7 @@ impl MDBook {
|
||||||
|
|
||||||
let renderers = determine_renderers(&config);
|
let renderers = determine_renderers(&config);
|
||||||
|
|
||||||
let preprocessors = vec![];
|
let preprocessors = determine_preprocessors(&config);
|
||||||
|
|
||||||
Ok(MDBook {
|
Ok(MDBook {
|
||||||
root,
|
root,
|
||||||
|
@ -215,16 +215,18 @@ impl MDBook {
|
||||||
|
|
||||||
let temp_dir = TempDir::new("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() {
|
for item in self.iter() {
|
||||||
if let BookItem::Chapter(ref ch) = *item {
|
if let BookItem::Chapter(ref ch) = *item {
|
||||||
if !ch.path.as_os_str().is_empty() {
|
if !ch.path.as_os_str().is_empty() {
|
||||||
let path = self.source_dir().join(&ch.path);
|
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);
|
println!("[*]: Testing file: {:?}", path);
|
||||||
|
let content = utils::fs::file_to_string(&path)?;
|
||||||
|
|
||||||
// write preprocessed file to tempdir
|
// write preprocessed file to tempdir
|
||||||
let path = temp_dir.path().join(&ch.path);
|
let path = temp_dir.path().join(&ch.path);
|
||||||
|
@ -322,6 +324,13 @@ fn determine_renderers(config: &Config) -> Vec<Box<Renderer>> {
|
||||||
renderers
|
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> {
|
fn interpret_custom_renderer(key: &str, table: &Value) -> Box<Renderer> {
|
||||||
// look for the `command` field, falling back to using the key
|
// look for the `command` field, falling back to using the key
|
||||||
// prepended by "mdbook-"
|
// prepended by "mdbook-"
|
||||||
|
|
|
@ -5,9 +5,35 @@ use utils::fs::file_to_string;
|
||||||
use utils::take_lines;
|
use utils::take_lines;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
|
use super::Preprocessor;
|
||||||
|
use book::{Book, BookItem};
|
||||||
|
|
||||||
const ESCAPE_CHAR: char = '\\';
|
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,
|
// When replacing one thing in a string by something with a different length,
|
||||||
// the indices after that will not correspond,
|
// the indices after that will not correspond,
|
||||||
// we therefore have to store the difference to correct this
|
// we therefore have to store the difference to correct this
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
pub mod links;
|
pub mod links;
|
||||||
|
|
||||||
pub trait Preprocessor {
|
use book::Book;
|
||||||
|
use errors::*;
|
||||||
|
|
||||||
|
|
||||||
|
pub trait Preprocessor {
|
||||||
|
fn run(&self, book: &mut Book) -> Result<()>;
|
||||||
}
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
use renderer::html_handlebars::helpers;
|
use renderer::html_handlebars::helpers;
|
||||||
use preprocess;
|
|
||||||
use renderer::{RenderContext, Renderer};
|
use renderer::{RenderContext, Renderer};
|
||||||
use book::{Book, BookItem, Chapter};
|
use book::{Book, BookItem, Chapter};
|
||||||
use config::{Config, HtmlConfig, Playpen};
|
use config::{Config, HtmlConfig, Playpen};
|
||||||
|
@ -50,12 +49,6 @@ impl HtmlHandlebars {
|
||||||
match *item {
|
match *item {
|
||||||
BookItem::Chapter(ref ch) => {
|
BookItem::Chapter(ref ch) => {
|
||||||
let content = ch.content.clone();
|
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);
|
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
|
||||||
print_content.push_str(&content);
|
print_content.push_str(&content);
|
||||||
|
|
||||||
|
@ -322,7 +315,6 @@ impl Renderer for HtmlHandlebars {
|
||||||
let ctx = RenderItemContext {
|
let ctx = RenderItemContext {
|
||||||
handlebars: &handlebars,
|
handlebars: &handlebars,
|
||||||
destination: destination.to_path_buf(),
|
destination: destination.to_path_buf(),
|
||||||
src_dir: src_dir.clone(),
|
|
||||||
data: data.clone(),
|
data: data.clone(),
|
||||||
is_index: i == 0,
|
is_index: i == 0,
|
||||||
html_config: html_config.clone(),
|
html_config: html_config.clone(),
|
||||||
|
@ -634,7 +626,6 @@ fn partition_source(s: &str) -> (String, String) {
|
||||||
struct RenderItemContext<'a> {
|
struct RenderItemContext<'a> {
|
||||||
handlebars: &'a Handlebars,
|
handlebars: &'a Handlebars,
|
||||||
destination: PathBuf,
|
destination: PathBuf,
|
||||||
src_dir: PathBuf,
|
|
||||||
data: serde_json::Map<String, serde_json::Value>,
|
data: serde_json::Map<String, serde_json::Value>,
|
||||||
is_index: bool,
|
is_index: bool,
|
||||||
html_config: HtmlConfig,
|
html_config: HtmlConfig,
|
||||||
|
|
Loading…
Reference in New Issue