Add a config option to disable section numbers in the TOC.

This commit is contained in:
Chris Spiegel 2017-12-05 18:53:30 -08:00
parent cd3b37593b
commit 1e8000e65a
4 changed files with 19 additions and 5 deletions

View File

@ -35,6 +35,8 @@ This is general information about your book.
- **src:** By default, the source directory is found in the directory named - **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` `src` directly under the root folder. But this is configurable with the `src`
key in the configuration file. 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** **book.toml**
```toml ```toml
@ -43,6 +45,7 @@ title = "Example book"
authors = ["John Doe", "Jane Doe"] authors = ["John Doe", "Jane Doe"]
description = "The example book covers examples." description = "The example book covers examples."
src = "my-src" # the source files will be found in `root/my-src` instead of `root/src` src = "my-src" # the source files will be found in `root/my-src` instead of `root/src`
section-numbers = false
``` ```
### Build options ### Build options

View File

@ -209,6 +209,8 @@ pub struct BookConfig {
pub src: PathBuf, pub src: PathBuf,
/// Does this book support more than one language? /// Does this book support more than one language?
pub multilingual: bool, pub multilingual: bool,
/// Should sections be numbered in the TOC?
pub section_numbers: bool,
} }
impl Default for BookConfig { impl Default for BookConfig {
@ -219,6 +221,7 @@ impl Default for BookConfig {
description: None, description: None,
src: PathBuf::from("src"), src: PathBuf::from("src"),
multilingual: false, multilingual: false,
section_numbers: true,
} }
} }
} }
@ -274,6 +277,7 @@ mod tests {
description = "A completely useless book" description = "A completely useless book"
multilingual = true multilingual = true
src = "source" src = "source"
section-numbers = false
[build] [build]
build-dir = "outputs" build-dir = "outputs"
@ -300,6 +304,7 @@ mod tests {
description: Some(String::from("A completely useless book")), description: Some(String::from("A completely useless book")),
multilingual: true, multilingual: true,
src: PathBuf::from("source"), src: PathBuf::from("source"),
section_numbers: false,
..Default::default() ..Default::default()
}; };
let build_should_be = BuildConfig { let build_should_be = BuildConfig {

View File

@ -407,9 +407,15 @@ fn make_data(book: &MDBook, config: &Config) -> Result<serde_json::Map<String, s
chapter.insert("path".to_owned(), json!(path)); chapter.insert("path".to_owned(), json!(path));
} }
BookItem::Chapter(ref s, ref ch) => { BookItem::Chapter(ref s, ref ch) => {
let section = s.iter() let level = format!("{}", s.len());
.fold("".to_owned(), |s, i| s + &i.to_string() + "."); chapter.insert("level".to_owned(), json!(level));
chapter.insert("section".to_owned(), json!(section));
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)); chapter.insert("name".to_owned(), json!(ch.name));
let path = ch.path.to_str().ok_or_else(|| { let path = ch.path.to_str().ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, io::Error::new(io::ErrorKind::Other,

View File

@ -34,8 +34,8 @@ impl HelperDef for RenderToc {
continue; continue;
} }
let level = if let Some(s) = item.get("section") { let level = if let Some(s) = item.get("level") {
s.matches('.').count() s.parse().expect(&format!("Error: level expected integer, got \"{}\"", s))
} else { } else {
1 1
}; };