@@ -43,7 +43,7 @@ function editor_download_all(brut_exercises, brut_index, callback) {
4343 } ) . then ( function ( blob ) { callback ( blob ) } ) ;
4444}
4545
46- function editor_read_exercise ( loaded_zip , path ) {
46+ function editor_read_exercise ( loaded_zip , path , id ) {
4747 return new Promise ( function ( resolve , reject ) {
4848 var descr = loaded_zip . file ( path + "descr.md" ) . async ( "string" ) ;
4949 var meta = loaded_zip . file ( path + "meta.json" ) . async ( "string" ) ;
@@ -54,8 +54,9 @@ function editor_read_exercise(loaded_zip, path) {
5454 var solution = loaded_zip . file ( path + "solution.ml" ) . async ( "string" ) ;
5555 Promise . all ( [ descr , meta , prelude , prepare , template , test , solution ] )
5656 . then ( function ( values ) {
57+ var result = { exercise : { } , metadata : { } } ;
5758 result . exercise . max_score = 0 ;
58- result . exercise . id = "" ;
59+ result . exercise . id = id ;
5960 result . exercise . descr = values [ 0 ] ;
6061 var brut_meta = values [ 1 ] ;
6162 var meta = brut_meta . replace ( / \r ? \n | \r / g, " " ) ;
@@ -69,27 +70,26 @@ function editor_read_exercise(loaded_zip, path) {
6970 } )
7071 } )
7172}
72- /*
73+
7374//also to keep in sync
7475function editor_import ( brut_data , callback ) {
7576 var zip = new JSZip ( ) ;
7677 zip . loadAsync ( brut_data )
7778 . then ( function ( loaded_zip ) {
78- if (loaded_zip.file("index.json")) {
79- loaded_zip.forEach(function(relative_path, file) {
80- if (file.dir) {
81- new Promise(function(resolve, reject) {
82- editor_read_exercise(loaded_zip, relative_path)
83- .then(function(result) {
84-
85- })
86- })
87- }
88- }
89- }
90- var result = { exercise: {}, metadata: {} };
79+ var promises = [ ] ;
80+ loaded_zip . forEach ( function ( relative_path , file ) {
81+ if ( file . dir ) {
82+ var promise = editor_read_exercise ( loaded_zip , relative_path , file . name . replace ( / \/ / , "" ) ) ;
83+ promises . push ( promise ) ;
84+ }
85+ } )
86+ Promise . all ( promises ) . then ( function ( values ) {
87+ var result = values . reduce ( function ( acc , elt ) {
88+ acc [ elt . exercise . id ] = elt ;
89+ return acc ;
90+ } , { } )
91+ callback ( JSON . stringify ( result ) ) ;
92+ } )
9193
92- callback(JSON.stringify(result));
93- });
9494 } ) ;
95- }*/
95+ }
0 commit comments