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" ) ?
@@ -64,31 +48,27 @@ impl HelperDef for RenderToc {
6448 RenderErrorReason :: Other ( "Type error for `fold_level`, u64 expected" . to_owned ( ) )
6549 } ) ?;
6650
51+ // If true, then this is the iframe and we need target="_parent"
52+ let is_toc_html = rc
53+ . evaluate ( ctx, "@root/is_toc_html" ) ?
54+ . as_json ( )
55+ . as_bool ( )
56+ . unwrap_or ( false ) ;
57+
6758 out. write ( "<ol class=\" chapter\" >" ) ?;
6859
6960 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 ( ) ;
7561
7662 for item in chapters {
77- let ( section , level) = if let Some ( s) = item. get ( "section" ) {
63+ let ( _section , level) = if let Some ( s) = item. get ( "section" ) {
7864 ( s. as_str ( ) , s. matches ( '.' ) . count ( ) )
7965 } else {
8066 ( "" , 1 )
8167 } ;
8268
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- } ;
69+ // Expand if folding is disabled, or if levels that are larger than this would not
70+ // be folded.
71+ let is_expanded = !fold_enable || level - 1 < ( fold_level as usize ) ;
9272
9373 match level. cmp ( & current_level) {
9474 Ordering :: Greater => {
@@ -121,7 +101,7 @@ impl HelperDef for RenderToc {
121101 // Part title
122102 if let Some ( title) = item. get ( "part" ) {
123103 out. write ( "<li class=\" part-title\" >" ) ?;
124- out. write ( & bracket_escape ( title) ) ?;
104+ out. write ( & special_escape ( title) ) ?;
125105 out. write ( "</li>" ) ?;
126106 continue ;
127107 }
@@ -139,16 +119,12 @@ impl HelperDef for RenderToc {
139119 . replace ( '\\' , "/" ) ;
140120
141121 // Add link
142- out. write ( & utils:: fs:: path_to_root ( & current_path) ) ?;
143122 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 ( ">" ) ?;
123+ out. write ( if is_toc_html {
124+ "\" target=\" _parent\" >"
125+ } else {
126+ "\" >"
127+ } ) ?;
152128 path_exists = true ;
153129 }
154130 _ => {
@@ -167,7 +143,7 @@ impl HelperDef for RenderToc {
167143 }
168144
169145 if let Some ( name) = item. get ( "name" ) {
170- out. write ( & bracket_escape ( name) ) ?
146+ out. write ( & special_escape ( name) ) ?
171147 }
172148
173149 if path_exists {
0 commit comments