@@ -741,8 +741,8 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
741741
742742 let mut crate_data = BTreeMap :: new ( ) ;
743743 crate_data. insert ( "doc" . to_owned ( ) , Json :: String ( crate_doc) ) ;
744- crate_data. insert ( "items " . to_owned ( ) , Json :: Array ( crate_items) ) ;
745- crate_data. insert ( "paths " . to_owned ( ) , Json :: Array ( crate_paths) ) ;
744+ crate_data. insert ( "i " . to_owned ( ) , Json :: Array ( crate_items) ) ;
745+ crate_data. insert ( "p " . to_owned ( ) , Json :: Array ( crate_paths) ) ;
746746
747747 // Collect the index into a string
748748 format ! ( "searchIndex[{}] = {};" ,
@@ -914,12 +914,44 @@ themePicker.onblur = handleThemeButtonsBlur;
914914 write ( cx. dst . join ( "COPYRIGHT.txt" ) ,
915915 static_files:: COPYRIGHT ) ?;
916916
917- fn collect ( path : & Path , krate : & str , key : & str ) -> io:: Result < ( Vec < String > , Vec < String > ) > {
917+ fn collect (
918+ path : & Path ,
919+ krate : & str ,
920+ key : & str ,
921+ for_search_index : bool ,
922+ ) -> io:: Result < ( Vec < String > , Vec < String > , Vec < String > ) > {
923+ use minifier:: js;
924+
918925 let mut ret = Vec :: new ( ) ;
919926 let mut krates = Vec :: new ( ) ;
927+ let mut variables = Vec :: new ( ) ;
928+
929+ let mut krate = krate. to_owned ( ) ;
930+
920931 if path. exists ( ) {
921932 for line in BufReader :: new ( File :: open ( path) ?) . lines ( ) {
922933 let line = line?;
934+ if for_search_index && line. starts_with ( "var r_" ) {
935+ variables. push ( line. clone ( ) ) ;
936+ // We need to check if the crate name has been put into a variable as well.
937+ let tokens = js:: simple_minify ( & line) . apply ( js:: clean_tokens) ;
938+ let mut pos = 0 ;
939+ while pos < tokens. len ( ) {
940+ if let Some ( ( var_pos, Some ( value_pos) ) ) =
941+ js:: get_variable_name_and_value_positions ( & tokens, pos) {
942+ if let Some ( s) = tokens. 0 [ value_pos] . get_string ( ) {
943+ if & s[ 1 ..s. len ( ) - 1 ] == krate {
944+ if let Some ( var) = tokens[ var_pos] . get_other ( ) {
945+ krate = var. to_owned ( ) ;
946+ break
947+ }
948+ }
949+ }
950+ }
951+ pos += 1 ;
952+ }
953+ continue ;
954+ }
923955 if !line. starts_with ( key) {
924956 continue ;
925957 }
@@ -933,7 +965,7 @@ themePicker.onblur = handleThemeButtonsBlur;
933965 . unwrap_or_else ( || String :: new ( ) ) ) ;
934966 }
935967 }
936- Ok ( ( ret, krates) )
968+ Ok ( ( ret, krates, variables ) )
937969 }
938970
939971 fn show_item ( item : & IndexItem , krate : & str ) -> String {
@@ -948,7 +980,7 @@ themePicker.onblur = handleThemeButtonsBlur;
948980
949981 let dst = cx. dst . join ( "aliases.js" ) ;
950982 {
951- let ( mut all_aliases, _) = try_err ! ( collect( & dst, & krate. name, "ALIASES" ) , & dst) ;
983+ let ( mut all_aliases, _, _ ) = try_err ! ( collect( & dst, & krate. name, "ALIASES" , false ) , & dst) ;
952984 let mut w = try_err ! ( File :: create( & dst) , & dst) ;
953985 let mut output = String :: with_capacity ( 100 ) ;
954986 for ( alias, items) in & cache. aliases {
@@ -1035,7 +1067,9 @@ themePicker.onblur = handleThemeButtonsBlur;
10351067 }
10361068
10371069 let dst = cx. dst . join ( "source-files.js" ) ;
1038- let ( mut all_sources, _krates) = try_err ! ( collect( & dst, & krate. name, "sourcesIndex" ) , & dst) ;
1070+ let ( mut all_sources, _krates, _) = try_err ! ( collect( & dst, & krate. name, "sourcesIndex" ,
1071+ false ) ,
1072+ & dst) ;
10391073 all_sources. push ( format ! ( "sourcesIndex[\" {}\" ] = {};" ,
10401074 & krate. name,
10411075 hierarchy. to_json_string( ) ) ) ;
@@ -1049,20 +1083,22 @@ themePicker.onblur = handleThemeButtonsBlur;
10491083
10501084 // Update the search index
10511085 let dst = cx. dst . join ( "search-index.js" ) ;
1052- let ( mut all_indexes, mut krates) = try_err ! ( collect( & dst, & krate. name, "searchIndex" ) , & dst) ;
1086+ let ( mut all_indexes, mut krates, variables) = try_err ! ( collect( & dst,
1087+ & krate. name,
1088+ "searchIndex" ,
1089+ true ) , & dst) ;
10531090 all_indexes. push ( search_index) ;
10541091
10551092 // Sort the indexes by crate so the file will be generated identically even
10561093 // with rustdoc running in parallel.
10571094 all_indexes. sort ( ) ;
10581095 let mut w = try_err ! ( File :: create( & dst) , & dst) ;
1059- try_err ! ( writeln!( & mut w, "var N = null;var searchIndex = {{}};" ) , & dst) ;
1060- for index in & all_indexes {
1061- try_err ! ( write_minify_replacer( & mut w, & * index, options. enable_minification,
1062- & [ ( minifier:: js:: Keyword :: Null , "N" ) ] ) ,
1063- & dst) ;
1064- }
1065- try_err ! ( writeln!( & mut w, "initSearch(searchIndex);addSearchOptions(searchIndex);" ) , & dst) ;
1096+ try_err ! ( writeln!( & mut w, "var N=null,E=\" \" ,T=\" t\" ,U=\" u\" ,searchIndex={{}};" ) , & dst) ;
1097+ try_err ! ( write_minify_replacer( & mut w,
1098+ & format!( "{}\n {}" , variables. join( "" ) , all_indexes. join( "\n " ) ) ,
1099+ options. enable_minification) ,
1100+ & dst) ;
1101+ try_err ! ( write!( & mut w, "initSearch(searchIndex);addSearchOptions(searchIndex);" ) , & dst) ;
10661102
10671103 if options. enable_index_page {
10681104 if let Some ( index_page) = options. index_page . clone ( ) {
@@ -1161,8 +1197,9 @@ themePicker.onblur = handleThemeButtonsBlur;
11611197 remote_item_type. css_class( ) ,
11621198 remote_path[ remote_path. len( ) - 1 ] ) ) ;
11631199
1164- let ( mut all_implementors, _) = try_err ! ( collect( & mydst, & krate. name, "implementors" ) ,
1165- & mydst) ;
1200+ let ( mut all_implementors, _, _) = try_err ! ( collect( & mydst, & krate. name, "implementors" ,
1201+ false ) ,
1202+ & mydst) ;
11661203 all_implementors. push ( implementors) ;
11671204 // Sort the implementors by crate so the file will be generated
11681205 // identically even with rustdoc running in parallel.
@@ -1216,14 +1253,50 @@ fn write_minify(dst: PathBuf, contents: &str, enable_minification: bool) -> Resu
12161253 }
12171254}
12181255
1219- fn write_minify_replacer < W : Write > ( dst : & mut W ,
1220- contents : & str ,
1221- enable_minification : bool ,
1222- keywords_to_replace : & [ ( minifier:: js:: Keyword , & str ) ] )
1223- -> io:: Result < ( ) > {
1256+ fn write_minify_replacer < W : Write > (
1257+ dst : & mut W ,
1258+ contents : & str ,
1259+ enable_minification : bool ,
1260+ ) -> io:: Result < ( ) > {
1261+ use minifier:: js:: { Keyword , ReservedChar , Token } ;
1262+
12241263 if enable_minification {
12251264 writeln ! ( dst, "{}" ,
1226- minifier:: js:: minify_and_replace_keywords( contents, keywords_to_replace) )
1265+ minifier:: js:: simple_minify( contents)
1266+ . apply( |f| {
1267+ // We keep backlines.
1268+ minifier:: js:: clean_tokens_except( f, |c| {
1269+ c. get_char( ) != Some ( ReservedChar :: Backline )
1270+ } )
1271+ } )
1272+ . apply( |f| {
1273+ minifier:: js:: replace_token_with( f, |t| {
1274+ match * t {
1275+ Token :: Keyword ( Keyword :: Null ) => Some ( Token :: Other ( "N" ) ) ,
1276+ Token :: String ( s) => {
1277+ let s = & s[ 1 ..s. len( ) -1 ] ; // The quotes are included
1278+ if s. is_empty( ) {
1279+ Some ( Token :: Other ( "E" ) )
1280+ } else if s == "t" {
1281+ Some ( Token :: Other ( "T" ) )
1282+ } else if s == "u" {
1283+ Some ( Token :: Other ( "U" ) )
1284+ } else {
1285+ None
1286+ }
1287+ }
1288+ _ => None ,
1289+ }
1290+ } )
1291+ } )
1292+ . apply( |f| {
1293+ // We add a backline after the newly created variables.
1294+ minifier:: js:: aggregate_strings_with_separation(
1295+ f,
1296+ Token :: Char ( ReservedChar :: Backline ) ,
1297+ )
1298+ } )
1299+ . to_string( ) )
12271300 } else {
12281301 writeln ! ( dst, "{}" , contents)
12291302 }
0 commit comments