1
1
use std:: path:: Path ;
2
2
use std:: { cmp:: Ordering , collections:: BTreeMap } ;
3
3
4
- use crate :: utils;
5
- use crate :: utils:: bracket_escape;
4
+ use crate :: utils:: special_escape;
6
5
7
6
use handlebars:: {
8
7
Context , Handlebars , Helper , HelperDef , Output , RenderContext , RenderError , RenderErrorReason ,
@@ -32,21 +31,6 @@ impl HelperDef for RenderToc {
32
31
RenderErrorReason :: Other ( "Could not decode the JSON data" . to_owned ( ) ) . into ( )
33
32
} )
34
33
} ) ?;
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 ( ) ;
50
34
51
35
let fold_enable = rc
52
36
. evaluate ( ctx, "@root/fold_enable" ) ?
@@ -67,28 +51,17 @@ impl HelperDef for RenderToc {
67
51
out. write ( "<ol class=\" chapter\" >" ) ?;
68
52
69
53
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 ( ) ;
75
54
76
55
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" ) {
78
57
( s. as_str ( ) , s. matches ( '.' ) . count ( ) )
79
58
} else {
80
59
( "" , 1 )
81
60
} ;
82
61
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 ) ;
92
65
93
66
match level. cmp ( & current_level) {
94
67
Ordering :: Greater => {
@@ -121,7 +94,7 @@ impl HelperDef for RenderToc {
121
94
// Part title
122
95
if let Some ( title) = item. get ( "part" ) {
123
96
out. write ( "<li class=\" part-title\" >" ) ?;
124
- out. write ( & bracket_escape ( title) ) ?;
97
+ out. write ( & special_escape ( title) ) ?;
125
98
out. write ( "</li>" ) ?;
126
99
continue ;
127
100
}
@@ -139,16 +112,8 @@ impl HelperDef for RenderToc {
139
112
. replace ( '\\' , "/" ) ;
140
113
141
114
// Add link
142
- out. write ( & utils:: fs:: path_to_root ( & current_path) ) ?;
143
115
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 ( "\" >" ) ?;
152
117
path_exists = true ;
153
118
}
154
119
_ => {
@@ -167,7 +132,7 @@ impl HelperDef for RenderToc {
167
132
}
168
133
169
134
if let Some ( name) = item. get ( "name" ) {
170
- out. write ( & bracket_escape ( name) ) ?
135
+ out. write ( & special_escape ( name) ) ?
171
136
}
172
137
173
138
if path_exists {
0 commit comments