From 1e8000e65a723931b35de29375bb3034158dad5d Mon Sep 17 00:00:00 2001 From: Chris Spiegel Date: Tue, 5 Dec 2017 18:53:30 -0800 Subject: [PATCH] Add a config option to disable section numbers in the TOC. --- book-example/src/format/config.md | 3 +++ src/config.rs | 5 +++++ src/renderer/html_handlebars/hbs_renderer.rs | 12 +++++++++--- src/renderer/html_handlebars/helpers/toc.rs | 4 ++-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index d1980a21..fe4b8548 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -35,6 +35,8 @@ This is general information about your book. - **src:** By default, the source directory is found in the directory named `src` directly under the root folder. But this is configurable with the `src` key in the configuration file. +- **section-numbers:** By default, sections are numbered in the table of + contents. Setting this to `false` disables numbering. **book.toml** ```toml @@ -43,6 +45,7 @@ title = "Example book" authors = ["John Doe", "Jane Doe"] description = "The example book covers examples." src = "my-src" # the source files will be found in `root/my-src` instead of `root/src` +section-numbers = false ``` ### Build options diff --git a/src/config.rs b/src/config.rs index d23e7a80..887c670e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -209,6 +209,8 @@ pub struct BookConfig { pub src: PathBuf, /// Does this book support more than one language? pub multilingual: bool, + /// Should sections be numbered in the TOC? + pub section_numbers: bool, } impl Default for BookConfig { @@ -219,6 +221,7 @@ impl Default for BookConfig { description: None, src: PathBuf::from("src"), multilingual: false, + section_numbers: true, } } } @@ -274,6 +277,7 @@ mod tests { description = "A completely useless book" multilingual = true src = "source" + section-numbers = false [build] build-dir = "outputs" @@ -300,6 +304,7 @@ mod tests { description: Some(String::from("A completely useless book")), multilingual: true, src: PathBuf::from("source"), + section_numbers: false, ..Default::default() }; let build_should_be = BuildConfig { diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 2dbe3231..6c6f7607 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -407,9 +407,15 @@ fn make_data(book: &MDBook, config: &Config) -> Result { - let section = s.iter() - .fold("".to_owned(), |s, i| s + &i.to_string() + "."); - chapter.insert("section".to_owned(), json!(section)); + let level = format!("{}", s.len()); + chapter.insert("level".to_owned(), json!(level)); + + if config.book.section_numbers { + 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, diff --git a/src/renderer/html_handlebars/helpers/toc.rs b/src/renderer/html_handlebars/helpers/toc.rs index 0c633bad..d759fb44 100644 --- a/src/renderer/html_handlebars/helpers/toc.rs +++ b/src/renderer/html_handlebars/helpers/toc.rs @@ -34,8 +34,8 @@ impl HelperDef for RenderToc { continue; } - let level = if let Some(s) = item.get("section") { - s.matches('.').count() + let level = if let Some(s) = item.get("level") { + s.parse().expect(&format!("Error: level expected integer, got \"{}\"", s)) } else { 1 };