@@ -741,8 +741,8 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
741
741
742
742
let mut crate_data = BTreeMap :: new ( ) ;
743
743
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) ) ;
746
746
747
747
// Collect the index into a string
748
748
format ! ( "searchIndex[{}] = {};" ,
@@ -914,12 +914,44 @@ themePicker.onblur = handleThemeButtonsBlur;
914
914
write ( cx. dst . join ( "COPYRIGHT.txt" ) ,
915
915
static_files:: COPYRIGHT ) ?;
916
916
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
+
918
925
let mut ret = Vec :: new ( ) ;
919
926
let mut krates = Vec :: new ( ) ;
927
+ let mut variables = Vec :: new ( ) ;
928
+
929
+ let mut krate = krate. to_owned ( ) ;
930
+
920
931
if path. exists ( ) {
921
932
for line in BufReader :: new ( File :: open ( path) ?) . lines ( ) {
922
933
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
+ }
923
955
if !line. starts_with ( key) {
924
956
continue ;
925
957
}
@@ -933,7 +965,7 @@ themePicker.onblur = handleThemeButtonsBlur;
933
965
. unwrap_or_else ( || String :: new ( ) ) ) ;
934
966
}
935
967
}
936
- Ok ( ( ret, krates) )
968
+ Ok ( ( ret, krates, variables ) )
937
969
}
938
970
939
971
fn show_item ( item : & IndexItem , krate : & str ) -> String {
@@ -948,7 +980,7 @@ themePicker.onblur = handleThemeButtonsBlur;
948
980
949
981
let dst = cx. dst . join ( "aliases.js" ) ;
950
982
{
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) ;
952
984
let mut w = try_err ! ( File :: create( & dst) , & dst) ;
953
985
let mut output = String :: with_capacity ( 100 ) ;
954
986
for ( alias, items) in & cache. aliases {
@@ -1035,7 +1067,9 @@ themePicker.onblur = handleThemeButtonsBlur;
1035
1067
}
1036
1068
1037
1069
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) ;
1039
1073
all_sources. push ( format ! ( "sourcesIndex[\" {}\" ] = {};" ,
1040
1074
& krate. name,
1041
1075
hierarchy. to_json_string( ) ) ) ;
@@ -1049,20 +1083,22 @@ themePicker.onblur = handleThemeButtonsBlur;
1049
1083
1050
1084
// Update the search index
1051
1085
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) ;
1053
1090
all_indexes. push ( search_index) ;
1054
1091
1055
1092
// Sort the indexes by crate so the file will be generated identically even
1056
1093
// with rustdoc running in parallel.
1057
1094
all_indexes. sort ( ) ;
1058
1095
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) ;
1066
1102
1067
1103
if options. enable_index_page {
1068
1104
if let Some ( index_page) = options. index_page . clone ( ) {
@@ -1161,8 +1197,9 @@ themePicker.onblur = handleThemeButtonsBlur;
1161
1197
remote_item_type. css_class( ) ,
1162
1198
remote_path[ remote_path. len( ) - 1 ] ) ) ;
1163
1199
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) ;
1166
1203
all_implementors. push ( implementors) ;
1167
1204
// Sort the implementors by crate so the file will be generated
1168
1205
// identically even with rustdoc running in parallel.
@@ -1216,14 +1253,50 @@ fn write_minify(dst: PathBuf, contents: &str, enable_minification: bool) -> Resu
1216
1253
}
1217
1254
}
1218
1255
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
+
1224
1263
if enable_minification {
1225
1264
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( ) )
1227
1300
} else {
1228
1301
writeln ! ( dst, "{}" , contents)
1229
1302
}
0 commit comments