@@ -65,12 +65,10 @@ use html::item_type::ItemType;
65
65
use html:: layout;
66
66
use html:: markdown:: Markdown ;
67
67
use html:: markdown;
68
- use html:: escape:: Escape ;
69
68
use stability_summary;
70
69
71
70
/// A pair of name and its optional document.
72
- #[ derive( Clone , Eq , Ord , PartialEq , PartialOrd ) ]
73
- pub struct NameDoc ( String , Option < String > ) ;
71
+ pub type NameDoc = ( String , Option < String > ) ;
74
72
75
73
/// Major driving force in all rustdoc rendering. This contains information
76
74
/// about where in the tree-like hierarchy rendering is occurring and controls
@@ -96,12 +94,6 @@ pub struct Context {
96
94
/// This describes the layout of each page, and is not modified after
97
95
/// creation of the context (contains info like the favicon and added html).
98
96
pub layout : layout:: Layout ,
99
- /// This map is a list of what should be displayed on the sidebar of the
100
- /// current page. The key is the section header (traits, modules,
101
- /// functions), and the value is the list of containers belonging to this
102
- /// header. This map will change depending on the surrounding context of the
103
- /// page.
104
- pub sidebar : HashMap < String , Vec < NameDoc > > ,
105
97
/// This flag indicates whether [src] links should be generated or not. If
106
98
/// the source files are present in the html rendering, then this will be
107
99
/// `true`.
@@ -265,7 +257,6 @@ pub fn run(mut krate: clean::Crate,
265
257
passes : passes,
266
258
current : Vec :: new ( ) ,
267
259
root_path : String :: new ( ) ,
268
- sidebar : HashMap :: new ( ) ,
269
260
layout : layout:: Layout {
270
261
logo : "" . to_string ( ) ,
271
262
favicon : "" . to_string ( ) ,
@@ -1227,7 +1218,16 @@ impl Context {
1227
1218
clean:: ModuleItem ( m) => m,
1228
1219
_ => unreachable ! ( )
1229
1220
} ;
1230
- this. sidebar = this. build_sidebar ( & m) ;
1221
+
1222
+ // render sidebar-items.js used throughout this module
1223
+ {
1224
+ let items = this. build_sidebar_items ( & m) ;
1225
+ let js_dst = this. dst . join ( "sidebar-items.js" ) ;
1226
+ let mut js_out = BufferedWriter :: new ( try!( File :: create ( & js_dst) ) ) ;
1227
+ try!( write ! ( & mut js_out, "initSidebarItems({});" ,
1228
+ json:: as_json( & items) ) ) ;
1229
+ }
1230
+
1231
1231
for item in m. items {
1232
1232
f ( this, item) ;
1233
1233
}
@@ -1247,15 +1247,11 @@ impl Context {
1247
1247
}
1248
1248
}
1249
1249
1250
- fn build_sidebar ( & self , m : & clean:: Module ) -> HashMap < String , Vec < NameDoc > > {
1250
+ fn build_sidebar_items ( & self , m : & clean:: Module ) -> HashMap < String , Vec < NameDoc > > {
1251
1251
let mut map = HashMap :: new ( ) ;
1252
1252
for item in & m. items {
1253
1253
if self . ignore_private_item ( item) { continue }
1254
1254
1255
- // avoid putting foreign items to the sidebar.
1256
- if let & clean:: ForeignFunctionItem ( ..) = & item. inner { continue }
1257
- if let & clean:: ForeignStaticItem ( ..) = & item. inner { continue }
1258
-
1259
1255
let short = shortty ( item) . to_static_str ( ) ;
1260
1256
let myname = match item. name {
1261
1257
None => continue ,
@@ -1264,7 +1260,7 @@ impl Context {
1264
1260
let short = short. to_string ( ) ;
1265
1261
let v = map. entry ( short) . get ( ) . unwrap_or_else (
1266
1262
|vacant_entry| vacant_entry. insert ( Vec :: with_capacity ( 1 ) ) ) ;
1267
- v. push ( NameDoc ( myname, Some ( shorter_line ( item. doc_value ( ) ) ) ) ) ;
1263
+ v. push ( ( myname, Some ( shorter_line ( item. doc_value ( ) ) ) ) ) ;
1268
1264
}
1269
1265
1270
1266
for ( _, items) in & mut map {
@@ -2211,9 +2207,11 @@ impl<'a> fmt::Display for Sidebar<'a> {
2211
2207
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
2212
2208
let cx = self . cx ;
2213
2209
let it = self . item ;
2210
+ let parentlen = cx. current . len ( ) - if it. is_mod ( ) { 1 } else { 0 } ;
2211
+
2212
+ // this is not rendered via JS, as that would hamper the accessibility
2214
2213
try!( write ! ( fmt, "<p class='location'>" ) ) ;
2215
- let len = cx. current . len ( ) - if it. is_mod ( ) { 1 } else { 0 } ;
2216
- for ( i, name) in cx. current . iter ( ) . take ( len) . enumerate ( ) {
2214
+ for ( i, name) in cx. current . iter ( ) . take ( parentlen) . enumerate ( ) {
2217
2215
if i > 0 {
2218
2216
try!( write ! ( fmt, "::<wbr>" ) ) ;
2219
2217
}
@@ -2223,40 +2221,25 @@ impl<'a> fmt::Display for Sidebar<'a> {
2223
2221
}
2224
2222
try!( write ! ( fmt, "</p>" ) ) ;
2225
2223
2226
- fn block ( w : & mut fmt:: Formatter , short : & str , longty : & str ,
2227
- cur : & clean:: Item , cx : & Context ) -> fmt:: Result {
2228
- let items = match cx. sidebar . get ( short) {
2229
- Some ( items) => items,
2230
- None => return Ok ( ( ) )
2231
- } ;
2232
- try!( write ! ( w, "<div class='block {}'><h2>{}</h2>" , short, longty) ) ;
2233
- for & NameDoc ( ref name, ref doc) in items {
2234
- let curty = shortty ( cur) . to_static_str ( ) ;
2235
- let class = if cur. name . as_ref ( ) . unwrap ( ) == name &&
2236
- short == curty { "current" } else { "" } ;
2237
- try!( write ! ( w, "<a class='{ty} {class}' href='{href}{path}' \
2238
- title='{title}'>{name}</a>",
2239
- ty = short,
2240
- class = class,
2241
- href = if curty == "mod" { "../" } else { "" } ,
2242
- path = if short == "mod" {
2243
- format!( "{}/index.html" , name)
2244
- } else {
2245
- format!( "{}.{}.html" , short, name)
2246
- } ,
2247
- title = Escape ( doc. as_ref( ) . unwrap( ) ) ,
2248
- name = name) ) ;
2249
- }
2250
- try!( write ! ( w, "</div>" ) ) ;
2251
- Ok ( ( ) )
2224
+ // sidebar refers to the enclosing module, not this module
2225
+ let relpath = if shortty ( it) == ItemType :: Module { "../" } else { "" } ;
2226
+ try!( write ! ( fmt,
2227
+ "<script>window.sidebarCurrent = {{\
2228
+ name: '{name}', \
2229
+ ty: '{ty}', \
2230
+ relpath: '{path}'\
2231
+ }};</script>",
2232
+ name = it. name. as_ref( ) . map( |x| & x[ ..] ) . unwrap_or( "" ) ,
2233
+ ty = shortty( it) . to_static_str( ) ,
2234
+ path = relpath) ) ;
2235
+ if parentlen == 0 {
2236
+ // there is no sidebar-items.js beyond the crate root path
2237
+ // FIXME maybe dynamic crate loading can be merged here
2238
+ } else {
2239
+ try!( write ! ( fmt, "<script async src=\" {path}sidebar-items.js\" ></script>" ,
2240
+ path = relpath) ) ;
2252
2241
}
2253
2242
2254
- try!( block ( fmt, "mod" , "Modules" , it, cx) ) ;
2255
- try!( block ( fmt, "struct" , "Structs" , it, cx) ) ;
2256
- try!( block ( fmt, "enum" , "Enums" , it, cx) ) ;
2257
- try!( block ( fmt, "trait" , "Traits" , it, cx) ) ;
2258
- try!( block ( fmt, "fn" , "Functions" , it, cx) ) ;
2259
- try!( block ( fmt, "macro" , "Macros" , it, cx) ) ;
2260
2243
Ok ( ( ) )
2261
2244
}
2262
2245
}
0 commit comments