Skip to content

Commit

Permalink
You can normally use default preprocessors by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Aug 30, 2018
1 parent d0a6d7c commit 4d7027f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 41 deletions.
58 changes: 27 additions & 31 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,19 @@ fn default_preprocessors() -> Vec<Box<Preprocessor>> {
]
}

fn is_default_preprocessor(pre: &Preprocessor) -> bool {
let name = pre.name();
name == LinkPreprocessor::NAME || name == IndexPreprocessor::NAME
}

/// Look at the `MDBook` and try to figure out what preprocessors to run.
fn determine_preprocessors(config: &Config) -> Result<Vec<Box<Preprocessor>>> {
let preprocess_list = match config.build.preprocess {
Some(ref p) => p,
let preprocessor_keys = config.get("preprocessor")
.and_then(|value| value.as_table())
.map(|table| table.keys());

let preprocessor_keys = match preprocessor_keys {
Some(keys) => keys,
// If no preprocessor field is set, default to the LinkPreprocessor and
// IndexPreprocessor. This allows you to disable default preprocessors
// by setting "preprocess" to an empty list.
Expand All @@ -361,7 +370,7 @@ fn determine_preprocessors(config: &Config) -> Result<Vec<Box<Preprocessor>>> {

let mut preprocessors: Vec<Box<Preprocessor>> = Vec::new();

for key in preprocess_list {
for key in preprocessor_keys {
match key.as_ref() {
"links" => preprocessors.push(Box::new(LinkPreprocessor::new())),
"index" => preprocessors.push(Box::new(IndexPreprocessor::new())),
Expand All @@ -388,7 +397,16 @@ fn interpret_custom_renderer(key: &str, table: &Value) -> Box<Renderer> {
/// Check whether we should run a particular `Preprocessor` in combination
/// with the renderer, falling back to `Preprocessor::supports_renderer()`
/// method if the user doesn't say anything.
///
/// The `build.use-default-preprocessors` config option can be used to ensure
/// default preprocessors always run if they support the renderer.
fn preprocessor_should_run(preprocessor: &Preprocessor, renderer: &Renderer, cfg: &Config) -> bool {
if cfg.build.use_default_preprocessors &&
is_default_preprocessor(preprocessor) &&
preprocessor.supports_renderer(renderer.name()) {
return true;
}

let key = format!("preprocessor.{}.renderers", preprocessor.name());
let renderer_name = renderer.name();

Expand Down Expand Up @@ -449,8 +467,8 @@ mod tests {
fn config_defaults_to_link_and_index_preprocessor_if_not_set() {
let cfg = Config::default();

// make sure we haven't got anything in the `output` table
assert!(cfg.build.preprocess.is_none());
// make sure we haven't got anything in the `preprocessor` table
assert!(cfg.get("preprocessor").is_none());

let got = determine_preprocessors(&cfg);

Expand All @@ -460,45 +478,23 @@ mod tests {
assert_eq!(got.as_ref().unwrap()[1].name(), "index");
}

#[test]
fn config_doesnt_default_if_empty() {
let cfg_str: &'static str = r#"
[book]
title = "Some Book"
[build]
build-dir = "outputs"
create-missing = false
preprocess = []
"#;

let cfg = Config::from_str(cfg_str).unwrap();

// make sure we have something in the `output` table
assert!(cfg.build.preprocess.is_some());

let got = determine_preprocessors(&cfg);

assert!(got.is_ok());
assert!(got.unwrap().is_empty());
}

#[test]
fn config_complains_if_unimplemented_preprocessor() {
let cfg_str: &'static str = r#"
[book]
title = "Some Book"
[preprocessor.random]
[build]
build-dir = "outputs"
create-missing = false
preprocess = ["random"]
"#;

let cfg = Config::from_str(cfg_str).unwrap();

// make sure we have something in the `output` table
assert!(cfg.build.preprocess.is_some());
// make sure the `preprocessor.random` table exists
assert!(cfg.get_preprocessor("random").is_some());

let got = determine_preprocessors(&cfg);

Expand Down
14 changes: 6 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,16 +394,17 @@ pub struct BuildConfig {
/// Should non-existent markdown files specified in `SETTINGS.md` be created
/// if they don't exist?
pub create_missing: bool,
/// Which preprocessors should be applied
pub preprocess: Option<Vec<String>>,
/// Should the default preprocessors always be used when they are
/// compatible with the renderer?
pub use_default_preprocessors: bool,
}

impl Default for BuildConfig {
fn default() -> BuildConfig {
BuildConfig {
build_dir: PathBuf::from("book"),
create_missing: true,
preprocess: None,
use_default_preprocessors: true,
}
}
}
Expand Down Expand Up @@ -591,10 +592,7 @@ mod tests {
let build_should_be = BuildConfig {
build_dir: PathBuf::from("outputs"),
create_missing: false,
preprocess: Some(vec![
"first_preprocessor".to_string(),
"second_preprocessor".to_string(),
]),
use_default_preprocessors: true,
};
let playpen_should_be = Playpen {
editable: true,
Expand Down Expand Up @@ -696,7 +694,7 @@ mod tests {
let build_should_be = BuildConfig {
build_dir: PathBuf::from("my-book"),
create_missing: true,
preprocess: None,
use_default_preprocessors: true,
};

let html_should_be = HtmlConfig {
Expand Down
4 changes: 3 additions & 1 deletion src/preprocess/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use book::{Book, BookItem};
pub struct IndexPreprocessor;

impl IndexPreprocessor {
pub(crate) const NAME: &'static str = "index";

/// Create a new `IndexPreprocessor`.
pub fn new() -> Self {
IndexPreprocessor
Expand All @@ -19,7 +21,7 @@ impl IndexPreprocessor {

impl Preprocessor for IndexPreprocessor {
fn name(&self) -> &str {
"index"
Self::NAME
}

fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
Expand Down
4 changes: 3 additions & 1 deletion src/preprocess/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const MAX_LINK_NESTED_DEPTH: usize = 10;
pub struct LinkPreprocessor;

impl LinkPreprocessor {
pub(crate) const NAME: &'static str = "links";

/// Create a new `LinkPreprocessor`.
pub fn new() -> Self {
LinkPreprocessor
Expand All @@ -24,7 +26,7 @@ impl LinkPreprocessor {

impl Preprocessor for LinkPreprocessor {
fn name(&self) -> &str {
"links"
Self::NAME
}

fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
Expand Down

0 comments on commit 4d7027f

Please sign in to comment.