From cd3b37593b52853485d85e29ec02ddc233cfaf0e Mon Sep 17 00:00:00 2001 From: Chris Spiegel Date: Tue, 5 Dec 2017 18:53:17 -0800 Subject: [PATCH] Track the current section as a vector of i32 rather than a String. The vector is the "natural" format of the section (i.e. how it is initially created) and is more suitable for further processing than a string is. Creation of a string representation of the section is delayed until it's actually required. --- src/book/bookitem.rs | 3 ++- src/parse/summary.rs | 6 ++---- src/renderer/html_handlebars/hbs_renderer.rs | 4 +++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/book/bookitem.rs b/src/book/bookitem.rs index a2ec2cb0..c33804aa 100644 --- a/src/book/bookitem.rs +++ b/src/book/bookitem.rs @@ -2,10 +2,11 @@ use serde::{Serialize, Serializer}; use serde::ser::SerializeStruct; use std::path::PathBuf; +type Section = Vec; #[derive(Debug, Clone)] pub enum BookItem { - Chapter(String, Chapter), // String = section + Chapter(Section, Chapter), Affix(Chapter), Spacer, } diff --git a/src/parse/summary.rs b/src/parse/summary.rs index 1193b6f5..b4434ea5 100644 --- a/src/parse/summary.rs +++ b/src/parse/summary.rs @@ -110,9 +110,7 @@ fn parse_level(summary: &mut Vec<&str>, // Increment section let len = section.len() - 1; section[len] += 1; - let s = section.iter() - .fold("".to_owned(), |s, i| s + &i.to_string() + "."); - BookItem::Chapter(s, ch) + BookItem::Chapter(section.clone(), ch) } _ => parsed_item, } @@ -181,7 +179,7 @@ fn parse_line(l: &str) -> Option { debug!("[*]: Line is list element"); if let Some((name, path)) = read_link(line) { - return Some(BookItem::Chapter("0".to_owned(), Chapter::new(name, path))); + return Some(BookItem::Chapter(vec![0], Chapter::new(name, path))); } else { return None; } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 14e45c23..2dbe3231 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -407,7 +407,9 @@ fn make_data(book: &MDBook, config: &Config) -> Result { - chapter.insert("section".to_owned(), json!(s)); + let section = s.iter() + .fold("".to_owned(), |s, i| s + &i.to_string() + "."); + chapter.insert("section".to_owned(), json!(section)); chapter.insert("name".to_owned(), json!(ch.name)); let path = ch.path.to_str().ok_or_else(|| { io::Error::new(io::ErrorKind::Other,