@@ -789,6 +789,37 @@ themePicker.onblur = handleThemeButtonsBlur;
789789 Ok ( ( ret, krates) )
790790 }
791791
792+ fn collect_json ( path : & Path , krate : & str ) -> io:: Result < ( Vec < String > , Vec < String > ) > {
793+ let mut ret = Vec :: new ( ) ;
794+ let mut krates = Vec :: new ( ) ;
795+
796+ if path. exists ( ) {
797+ for line in BufReader :: new ( File :: open ( path) ?) . lines ( ) {
798+ let line = line?;
799+ if !line. starts_with ( "\" " ) {
800+ continue ;
801+ }
802+ if line. starts_with ( & format ! ( "\" {}\" " , krate) ) {
803+ continue ;
804+ }
805+ if line. ends_with ( ",\\ " ) {
806+ ret. push ( line[ ..line. len ( ) - 2 ] . to_string ( ) ) ;
807+ } else {
808+ // Ends with "\\" (it's the case for the last added crate line)
809+ ret. push ( line[ ..line. len ( ) - 1 ] . to_string ( ) ) ;
810+ }
811+ krates. push (
812+ line. split ( '"' )
813+ . filter ( |s| !s. is_empty ( ) )
814+ . next ( )
815+ . map ( |s| s. to_owned ( ) )
816+ . unwrap_or_else ( String :: new) ,
817+ ) ;
818+ }
819+ }
820+ Ok ( ( ret, krates) )
821+ }
822+
792823 fn show_item ( item : & IndexItem , krate : & str ) -> String {
793824 format ! (
794825 "{{'crate':'{}','ty':{},'name':'{}','desc':'{}','p':'{}'{}}}" ,
@@ -909,18 +940,18 @@ themePicker.onblur = handleThemeButtonsBlur;
909940
910941 // Update the search index
911942 let dst = cx. dst . join ( & format ! ( "search-index{}.js" , cx. shared. resource_suffix) ) ;
912- let ( mut all_indexes, mut krates) = try_err ! ( collect ( & dst, & krate. name, "searchIndex" ) , & dst) ;
943+ let ( mut all_indexes, mut krates) = try_err ! ( collect_json ( & dst, & krate. name) , & dst) ;
913944 all_indexes. push ( search_index) ;
914945
915946 // Sort the indexes by crate so the file will be generated identically even
916947 // with rustdoc running in parallel.
917948 all_indexes. sort ( ) ;
918949 {
919- let mut v = String :: from ( "var searchIndex={}; \n " ) ;
920- v. push_str ( & all_indexes. join ( "\n " ) ) ;
950+ let mut v = String :: from ( "var searchIndex = JSON.parse('{ \\ \n " ) ;
951+ v. push_str ( & all_indexes. join ( ", \\ \n " ) ) ;
921952 // "addSearchOptions" has to be called first so the crate filtering can be set before the
922953 // search might start (if it's set into the URL for example).
923- v. push_str ( "\n addSearchOptions(searchIndex);initSearch(searchIndex);" ) ;
954+ v. push_str ( "\\ \n }'); \ n addSearchOptions(searchIndex);initSearch(searchIndex);" ) ;
924955 cx. shared . fs . write ( & dst, & v) ?;
925956 }
926957 if options. enable_index_page {
0 commit comments