@@ -37,27 +37,34 @@ pub fn parse_cmd(name: &str) -> Option<Box<Subcommand>> {
37
37
}
38
38
}
39
39
40
- fn write_toc ( book : & Book , path_to_root : & Path , out : & mut Write ) -> io:: Result < ( ) > {
40
+ fn write_toc ( book : & Book , current_page : & BookItem , out : & mut Write ) -> io:: Result < ( ) > {
41
41
fn walk_items ( items : & [ BookItem ] ,
42
42
section : & str ,
43
- path_to_root : & Path ,
43
+ current_page : & BookItem ,
44
44
out : & mut Write ) -> io:: Result < ( ) > {
45
45
for ( i, item) in items. iter ( ) . enumerate ( ) {
46
- try!( walk_item ( item, & format ! ( "{}{}." , section, i + 1 ) [ ..] , path_to_root , out) ) ;
46
+ try!( walk_item ( item, & format ! ( "{}{}." , section, i + 1 ) [ ..] , current_page , out) ) ;
47
47
}
48
48
Ok ( ( ) )
49
49
}
50
50
fn walk_item ( item : & BookItem ,
51
51
section : & str ,
52
- path_to_root : & Path ,
52
+ current_page : & BookItem ,
53
53
out : & mut Write ) -> io:: Result < ( ) > {
54
- try!( writeln ! ( out, "<li><a href='{}'><b>{}</b> {}</a>" ,
55
- path_to_root. join( & item. path. with_extension( "html" ) ) . display( ) ,
54
+ let class_string = if item. path == current_page. path {
55
+ "class='active'"
56
+ } else {
57
+ ""
58
+ } ;
59
+
60
+ try!( writeln ! ( out, "<li><a {} href='{}'><b>{}</b> {}</a>" ,
61
+ class_string,
62
+ item. path_to_root. join( & item. path. with_extension( "html" ) ) . display( ) ,
56
63
section,
57
64
item. title) ) ;
58
65
if !item. children . is_empty ( ) {
59
66
try!( writeln ! ( out, "<ul class='section'>" ) ) ;
60
- let _ = walk_items ( & item. children [ ..] , section, path_to_root , out) ;
67
+ let _ = walk_items ( & item. children [ ..] , section, current_page , out) ;
61
68
try!( writeln ! ( out, "</ul>" ) ) ;
62
69
}
63
70
try!( writeln ! ( out, "</li>" ) ) ;
@@ -67,7 +74,7 @@ fn write_toc(book: &Book, path_to_root: &Path, out: &mut Write) -> io::Result<()
67
74
68
75
try!( writeln ! ( out, "<div id='toc' class='mobile-hidden'>" ) ) ;
69
76
try!( writeln ! ( out, "<ul class='chapter'>" ) ) ;
70
- try!( walk_items ( & book. chapters [ ..] , "" , path_to_root , out) ) ;
77
+ try!( walk_items ( & book. chapters [ ..] , "" , & current_page , out) ) ;
71
78
try!( writeln ! ( out, "</ul>" ) ) ;
72
79
try!( writeln ! ( out, "</div>" ) ) ;
73
80
@@ -115,7 +122,7 @@ fn render(book: &Book, tgt: &Path) -> CliResult<()> {
115
122
<span class="bar"></span>
116
123
</button>
117
124
</div>"# ) ) ;
118
- let _ = write_toc ( book, & item. path_to_root , & mut toc) ;
125
+ let _ = write_toc ( book, & item, & mut toc) ;
119
126
try!( writeln ! ( & mut toc, "<div id='page-wrapper'>" ) ) ;
120
127
try!( writeln ! ( & mut toc, "<div id='page'>" ) ) ;
121
128
}
0 commit comments