11use std:: path:: Path ;
22use std:: { cmp:: Ordering , collections:: BTreeMap } ;
33
4- use crate :: utils;
5- use crate :: utils:: bracket_escape;
4+ use crate :: utils:: special_escape;
65
76use handlebars:: {
87 Context , Handlebars , Helper , HelperDef , Output , RenderContext , RenderError , RenderErrorReason ,
@@ -32,21 +31,6 @@ impl HelperDef for RenderToc {
3231 RenderErrorReason :: Other ( "Could not decode the JSON data" . to_owned ( ) ) . into ( )
3332 } )
3433 } ) ?;
35- let current_path = rc
36- . evaluate ( ctx, "@root/path" ) ?
37- . as_json ( )
38- . as_str ( )
39- . ok_or_else ( || {
40- RenderErrorReason :: Other ( "Type error for `path`, string expected" . to_owned ( ) )
41- } ) ?
42- . replace ( '\"' , "" ) ;
43-
44- let current_section = rc
45- . evaluate ( ctx, "@root/section" ) ?
46- . as_json ( )
47- . as_str ( )
48- . map ( str:: to_owned)
49- . unwrap_or_default ( ) ;
5034
5135 let fold_enable = rc
5236 . evaluate ( ctx, "@root/fold_enable" ) ?
@@ -67,28 +51,17 @@ impl HelperDef for RenderToc {
6751 out. write ( "<ol class=\" chapter\" >" ) ?;
6852
6953 let mut current_level = 1 ;
70- // The "index" page, which has this attribute set, is supposed to alias the first chapter in
71- // the book, i.e. the first link. There seems to be no easy way to determine which chapter
72- // the "index" is aliasing from within the renderer, so this is used instead to force the
73- // first link to be active. See further below.
74- let mut is_first_chapter = ctx. data ( ) . get ( "is_index" ) . is_some ( ) ;
7554
7655 for item in chapters {
77- let ( section , level) = if let Some ( s) = item. get ( "section" ) {
56+ let ( _section , level) = if let Some ( s) = item. get ( "section" ) {
7857 ( s. as_str ( ) , s. matches ( '.' ) . count ( ) )
7958 } else {
8059 ( "" , 1 )
8160 } ;
8261
83- let is_expanded =
84- if !fold_enable || ( !section. is_empty ( ) && current_section. starts_with ( section) ) {
85- // Expand if folding is disabled, or if the section is an
86- // ancestor or the current section itself.
87- true
88- } else {
89- // Levels that are larger than this would be folded.
90- level - 1 < fold_level as usize
91- } ;
62+ // Expand if folding is disabled, or if levels that are larger than this would not
63+ // be folded.
64+ let is_expanded = !fold_enable || level - 1 < ( fold_level as usize ) ;
9265
9366 match level. cmp ( & current_level) {
9467 Ordering :: Greater => {
@@ -121,7 +94,7 @@ impl HelperDef for RenderToc {
12194 // Part title
12295 if let Some ( title) = item. get ( "part" ) {
12396 out. write ( "<li class=\" part-title\" >" ) ?;
124- out. write ( & bracket_escape ( title) ) ?;
97+ out. write ( & special_escape ( title) ) ?;
12598 out. write ( "</li>" ) ?;
12699 continue ;
127100 }
@@ -139,16 +112,8 @@ impl HelperDef for RenderToc {
139112 . replace ( '\\' , "/" ) ;
140113
141114 // Add link
142- out. write ( & utils:: fs:: path_to_root ( & current_path) ) ?;
143115 out. write ( & tmp) ?;
144- out. write ( "\" " ) ?;
145-
146- if path == & current_path || is_first_chapter {
147- is_first_chapter = false ;
148- out. write ( " class=\" active\" " ) ?;
149- }
150-
151- out. write ( ">" ) ?;
116+ out. write ( "\" >" ) ?;
152117 path_exists = true ;
153118 }
154119 _ => {
@@ -167,7 +132,7 @@ impl HelperDef for RenderToc {
167132 }
168133
169134 if let Some ( name) = item. get ( "name" ) {
170- out. write ( & bracket_escape ( name) ) ?
135+ out. write ( & special_escape ( name) ) ?
171136 }
172137
173138 if path_exists {
0 commit comments