1
Fork 0

Add a test

This commit is contained in:
Jan-Erik Rediger 2018-09-13 14:26:44 +02:00
parent c6a679d1a3
commit c87fdf7e3e

View file

@ -46,58 +46,111 @@ fn build_toc<'a>(toc: &[(i32, Cow<'a, str>)]) -> String {
result
}
impl Toc {
fn add_toc(chapter: &mut Chapter) -> Result<String> {
let mut buf = String::with_capacity(chapter.content.len());
let mut toc_found = false;
fn add_toc(content: &str) -> Result<String> {
let mut buf = String::with_capacity(content.len());
let mut toc_found = false;
let mut toc_content = vec![];
let mut current_header_level : Option<i32> = None;
let mut toc_content = vec![];
let mut current_header_level : Option<i32> = None;
for e in Parser::new(&chapter.content) {
if let Event::Html(html) = e {
if html == "<!-- toc -->\n" {
toc_found = true;
}
continue;
}
if !toc_found {
continue;
}
if let Event::Start(Header(lvl)) = e {
if lvl < 3 {
current_header_level = Some(lvl);
}
continue;
}
if let Event::End(Header(_)) = e {
current_header_level = None;
continue;
}
if current_header_level.is_none() {
continue;
}
if let Event::Text(header) = e {
toc_content.push((current_header_level.unwrap(), header));
for e in Parser::new(&content) {
if let Event::Html(html) = e {
if html == "<!-- toc -->\n" {
toc_found = true;
}
continue;
}
if !toc_found {
continue;
}
let toc_events = build_toc(&toc_content);
let toc_events = Parser::new(&toc_events).collect::<Vec<_>>();
let events = Parser::new(&chapter.content).map(|e| {
if let Event::Html(html) = e.clone() {
if html == "<!-- toc -->\n" {
return toc_events.clone();
}
if let Event::Start(Header(lvl)) = e {
if lvl < 3 {
current_header_level = Some(lvl);
}
vec![e]
}).flat_map(|e| e);
continue;
}
if let Event::End(Header(_)) = e {
current_header_level = None;
continue;
}
if current_header_level.is_none() {
continue;
}
cmark(events, &mut buf, None)
.map(|_| buf)
.map_err(|err| Error::from(format!("Markdown serialization failed: {}", err)))
if let Event::Text(header) = e {
toc_content.push((current_header_level.unwrap(), header));
}
}
let toc_events = build_toc(&toc_content);
let toc_events = Parser::new(&toc_events).collect::<Vec<_>>();
let events = Parser::new(&content).map(|e| {
if let Event::Html(html) = e.clone() {
if html == "<!-- toc -->\n" {
return toc_events.clone();
}
}
vec![e]
}).flat_map(|e| e);
cmark(events, &mut buf, None)
.map(|_| buf)
.map_err(|err| Error::from(format!("Markdown serialization failed: {}", err)))
}
impl Toc {
fn add_toc(chapter: &mut Chapter) -> Result<String> {
add_toc(&chapter.content)
}
}
#[cfg(test)]
mod test {
use super::add_toc;
#[test]
fn adds_toc() {
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).unwrap());
}
}