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` 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

View File

@ -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 {

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));
}
BookItem::Chapter(ref s, ref ch) => {
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,

View File

@ -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
};