@@ -43,7 +43,7 @@ function editor_download_all(brut_exercises, brut_index, callback) {
43
43
} ) . then ( function ( blob ) { callback ( blob ) } ) ;
44
44
}
45
45
46
- function editor_read_exercise ( loaded_zip , path ) {
46
+ function editor_read_exercise ( loaded_zip , path , id ) {
47
47
return new Promise ( function ( resolve , reject ) {
48
48
var descr = loaded_zip . file ( path + "descr.md" ) . async ( "string" ) ;
49
49
var meta = loaded_zip . file ( path + "meta.json" ) . async ( "string" ) ;
@@ -54,8 +54,9 @@ function editor_read_exercise(loaded_zip, path) {
54
54
var solution = loaded_zip . file ( path + "solution.ml" ) . async ( "string" ) ;
55
55
Promise . all ( [ descr , meta , prelude , prepare , template , test , solution ] )
56
56
. then ( function ( values ) {
57
+ var result = { exercise : { } , metadata : { } } ;
57
58
result . exercise . max_score = 0 ;
58
- result . exercise . id = "" ;
59
+ result . exercise . id = id ;
59
60
result . exercise . descr = values [ 0 ] ;
60
61
var brut_meta = values [ 1 ] ;
61
62
var meta = brut_meta . replace ( / \r ? \n | \r / g, " " ) ;
@@ -69,27 +70,26 @@ function editor_read_exercise(loaded_zip, path) {
69
70
} )
70
71
} )
71
72
}
72
- /*
73
+
73
74
//also to keep in sync
74
75
function editor_import ( brut_data , callback ) {
75
76
var zip = new JSZip ( ) ;
76
77
zip . loadAsync ( brut_data )
77
78
. 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
+ } )
91
93
92
- callback(JSON.stringify(result));
93
- });
94
94
} ) ;
95
- }*/
95
+ }
0 commit comments