1
Fork 0

Pass config everywhere

This commit is contained in:
Jan-Erik Rediger 2021-01-06 14:40:06 +01:00
parent 8916d17e4b
commit 9a9288085f

View file

@ -1,6 +1,6 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryFrom; use std::convert::{TryFrom, TryInto};
use std::fmt::Write; use std::fmt::Write;
use mdbook::book::{Book, BookItem, Chapter}; use mdbook::book::{Book, BookItem, Chapter};
@ -84,7 +84,7 @@ impl Preprocessor for Toc {
} }
if let BookItem::Chapter(ref mut chapter) = *item { if let BookItem::Chapter(ref mut chapter) = *item {
res = Some(Toc::add_toc(chapter, &cfg.marker).map(|md| { res = Some(Toc::add_toc(chapter, &cfg).map(|md| {
chapter.content = md; chapter.content = md;
})); }));
} }
@ -130,7 +130,7 @@ fn build_toc(toc: &[(u32, String, String)]) -> String {
result result
} }
fn add_toc(content: &str, marker: &str) -> Result<String> { fn add_toc(content: &str, cfg: &Config) -> Result<String> {
let mut buf = String::with_capacity(content.len()); let mut buf = String::with_capacity(content.len());
let mut toc_found = false; let mut toc_found = false;
@ -145,7 +145,7 @@ fn add_toc(content: &str, marker: &str) -> Result<String> {
opts.insert(Options::ENABLE_STRIKETHROUGH); opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TASKLISTS); opts.insert(Options::ENABLE_TASKLISTS);
let mark: Vec<Event> = Parser::new(marker).collect(); let mark: Vec<Event> = Parser::new(&cfg.marker).collect();
let mut mark_start = -1; let mut mark_start = -1;
let mut mark_loc = 0; let mut mark_loc = 0;
let mut c = -1; let mut c = -1;
@ -189,7 +189,7 @@ fn add_toc(content: &str, marker: &str) -> Result<String> {
*id_count += 1; *id_count += 1;
if level < 5 { if level <= cfg.max_level {
toc_content.push((level, header, slug)); toc_content.push((level, header, slug));
} }
@ -241,16 +241,32 @@ fn add_toc(content: &str, marker: &str) -> Result<String> {
} }
impl Toc { impl Toc {
fn add_toc(chapter: &mut Chapter, marker: &str) -> Result<String> { fn add_toc(chapter: &mut Chapter, cfg: &Config) -> Result<String> {
add_toc(&chapter.content, marker) add_toc(&chapter.content, cfg)
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::{add_toc, DEFAULT_MARKER}; use super::{add_toc, Config};
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
fn default<T: Default>() -> T {
Default::default()
}
fn with_marker<S: Into<String>>(marker: S) -> Config {
let mut cfg = Config::default();
cfg.marker = marker.into();
cfg
}
fn with_max_level(level: u32) -> Config {
let mut cfg = Config::default();
cfg.max_level = level;
cfg
}
#[test] #[test]
fn adds_toc() { fn adds_toc() {
let content = r#"# Chapter let content = r#"# Chapter
@ -292,7 +308,7 @@ mod test {
### Header 2.2.1"#; ### Header 2.2.1"#;
assert_eq!(expected, add_toc(content, DEFAULT_MARKER).unwrap()); assert_eq!(expected, add_toc(content, &default()).unwrap());
} }
#[test] #[test]
@ -325,7 +341,7 @@ mod test {
## Header 2.1"#; ## Header 2.1"#;
assert_eq!(expected, add_toc(content, DEFAULT_MARKER).unwrap()); assert_eq!(expected, add_toc(content, &default()).unwrap());
} }
#[test] #[test]
@ -347,7 +363,7 @@ mod test {
|------|------| |------|------|
|Row 1|Row 2|"#; |Row 1|Row 2|"#;
assert_eq!(expected, add_toc(content, DEFAULT_MARKER).unwrap()); assert_eq!(expected, add_toc(content, &default()).unwrap());
} }
#[test] #[test]
@ -396,7 +412,7 @@ mod test {
# Another header `with inline` code"#; # Another header `with inline` code"#;
assert_eq!(expected, add_toc(content, DEFAULT_MARKER).unwrap()); assert_eq!(expected, add_toc(content, &default()).unwrap());
} }
#[test] #[test]
@ -443,7 +459,7 @@ mod test {
## User Preferences"#; ## User Preferences"#;
assert_eq!(expected, add_toc(content, DEFAULT_MARKER).unwrap()); assert_eq!(expected, add_toc(content, &default()).unwrap());
} }
#[test] #[test]
@ -480,7 +496,7 @@ text"#;
text"#; text"#;
assert_eq!(expected, add_toc(content, DEFAULT_MARKER).unwrap()); assert_eq!(expected, add_toc(content, &default()).unwrap());
} }
#[test] #[test]
@ -525,7 +541,7 @@ text"#;
### Header 2.2.1"#; ### Header 2.2.1"#;
assert_eq!(expected, add_toc(content, &marker).unwrap()); assert_eq!(expected, add_toc(content, &with_marker(marker)).unwrap());
} }
#[test] #[test]
@ -561,7 +577,7 @@ text"#;
## Duplicate"#; ## Duplicate"#;
assert_eq!(expected, add_toc(content, DEFAULT_MARKER).unwrap()); assert_eq!(expected, add_toc(content, &default()).unwrap());
} }
#[test] #[test]
@ -607,6 +623,93 @@ text"#;
### Header 2.2.1"#; ### Header 2.2.1"#;
assert_eq!(expected, add_toc(content, &marker).unwrap()); assert_eq!(expected, add_toc(content, &with_marker(marker)).unwrap());
}
#[test]
fn lower_max_level() {
let content = r#"# Chapter
<!-- toc -->
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1
"#;
let expected = r#"# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
* [Header 2.2](#header-22)
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1"#;
assert_eq!(expected, add_toc(content, &with_max_level(2)).unwrap());
}
#[test]
fn higher_max_level() {
let content = r#"# Chapter
<!-- toc -->
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1
"#;
let expected = r#"# Chapter
* [Header 1](#header-1)
* [Header 1.1](#header-11)
* [Header 2](#header-2)
* [Header 2.1](#header-21)
* [Header 2.2](#header-22)
* [Header 2.2.1](#header-221)
# Header 1
## Header 1.1
# Header 2
## Header 2.1
## Header 2.2
### Header 2.2.1"#;
assert_eq!(expected, add_toc(content, &with_max_level(7)).unwrap());
} }
} }