OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Library for extracting the documentation comments from files generated by | 6 * Library for extracting the documentation comments from files generated by |
7 * the HTML library. The comments are stored in a JSON file. | 7 * the HTML library. The comments are stored in a JSON file. |
8 * | 8 * |
9 * Comments must be in either the block style with leading *s: | 9 * Comments must be in either the block style with leading *s: |
10 * | 10 * |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 /// True if any errors were triggered through the conversion. | 31 /// True if any errors were triggered through the conversion. |
32 bool _anyErrors = false; | 32 bool _anyErrors = false; |
33 | 33 |
34 | 34 |
35 /** | 35 /** |
36 * Convert files on [htmlPath] and write JSON to [jsonPath]. | 36 * Convert files on [htmlPath] and write JSON to [jsonPath]. |
37 */ | 37 */ |
38 Future<bool> convert(Path htmlPath, Path jsonPath) { | 38 Future<bool> convert(Path htmlPath, Path jsonPath) { |
39 var completer = new Completer(); | 39 var completer = new Completer(); |
40 | 40 |
41 // TODO(amouravski): make this transform once I know what I want this file to | 41 // TODO(amouravski): make this transform once I know what I want this file to |
42 // return. | 42 // return. |
43 _convertFiles(htmlPath).then((convertedJson) { | 43 _convertFiles(htmlPath).then((convertedJson) { |
44 final jsonFile = new File.fromPath(jsonPath); | 44 final jsonFile = new File.fromPath(jsonPath); |
45 var writeJson = convertedJson; | 45 var writeJson = convertedJson; |
46 | 46 |
47 if (jsonFile.existsSync()) { | 47 if (jsonFile.existsSync()) { |
48 writeJson = _mergeJsonAndFile(convertedJson, jsonFile); | 48 writeJson = _mergeJsonAndFile(convertedJson, jsonFile); |
49 } | 49 } |
50 | 50 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 _anyErrors = true; | 98 _anyErrors = true; |
99 return; | 99 return; |
100 } | 100 } |
101 | 101 |
102 fileFutures.add(_convertFile(file)); | 102 fileFutures.add(_convertFile(file)); |
103 }; | 103 }; |
104 | 104 |
105 | 105 |
106 // Combine all JSON objects | 106 // Combine all JSON objects |
107 lister.onDone = (_) { | 107 lister.onDone = (_) { |
108 Futures.wait(fileFutures).then((jsonList) { | 108 Future.wait(fileFutures).then((jsonList) { |
109 var convertedJson = {}; | 109 var convertedJson = {}; |
110 jsonList.forEach((json) { | 110 jsonList.forEach((json) { |
111 final k = json.keys[0]; | 111 final k = json.keys[0]; |
112 convertedJson.putIfAbsent(k, () => json[k]); | 112 convertedJson.putIfAbsent(k, () => json[k]); |
113 }); | 113 }); |
114 completer.complete(convertedJson); | 114 completer.complete(convertedJson); |
115 }); | 115 }); |
116 }; | 116 }; |
117 | 117 |
118 // TODO(amouravski): add more error handling. | 118 // TODO(amouravski): add more error handling. |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 bool _listsEqual(List list1, List list2) { | 263 bool _listsEqual(List list1, List list2) { |
264 return list1.every((e) => list2.contains(e)) && | 264 return list1.every((e) => list2.contains(e)) && |
265 list2.every((e) => list1.contains(e)); | 265 list2.every((e) => list1.contains(e)); |
266 } | 266 } |
267 | 267 |
268 | 268 |
269 /** | 269 /** |
270 * Print JSON in a much nicer format. | 270 * Print JSON in a much nicer format. |
271 * | 271 * |
272 * For example: | 272 * For example: |
273 * | 273 * |
274 * {"foo":["bar","baz"],"boo":{"far:"faz"}} | 274 * {"foo":["bar","baz"],"boo":{"far:"faz"}} |
275 * | 275 * |
276 * becomes: | 276 * becomes: |
277 * | 277 * |
278 * { | 278 * { |
279 * "foo": | 279 * "foo": |
280 * [ | 280 * [ |
281 * "bar", | 281 * "bar", |
282 * "baz" | 282 * "baz" |
283 * ], | 283 * ], |
284 * "boo": | 284 * "boo": |
285 * { | 285 * { |
286 * "far": | 286 * "far": |
287 * "faz" | 287 * "faz" |
288 * } | 288 * } |
289 * } | 289 * } |
290 */ | 290 */ |
291 String prettyPrintJson(Object json, [String indentation = '']) { | 291 String prettyPrintJson(Object json, [String indentation = '']) { |
292 var output; | 292 var output; |
293 | 293 |
294 if (json is List) { | 294 if (json is List) { |
295 var recursiveOutput = | 295 var recursiveOutput = |
296 Strings.join(json.map((e) => | 296 Strings.join(json.map((e) => |
297 prettyPrintJson(e, '$indentation ')), ',\n'); | 297 prettyPrintJson(e, '$indentation ')), ',\n'); |
298 output = '$indentation[\n' | 298 output = '$indentation[\n' |
299 '$recursiveOutput' | 299 '$recursiveOutput' |
300 '\n$indentation]'; | 300 '\n$indentation]'; |
301 } else if (json is Map) { | 301 } else if (json is Map) { |
302 var keys = json.keys | 302 var keys = json.keys |
303 ..sort(); | 303 ..sort(); |
304 | 304 |
305 // TODO(amouravski): No newline after : | 305 // TODO(amouravski): No newline after : |
306 var mapList = keys.map((key) => | 306 var mapList = keys.map((key) => |
307 '$indentation${JSON.stringify(key)}:\n' | 307 '$indentation${JSON.stringify(key)}:\n' |
308 '${prettyPrintJson(json[key], '$indentation ')}'); | 308 '${prettyPrintJson(json[key], '$indentation ')}'); |
309 var recursiveOutput = Strings.join(mapList, ',\n'); | 309 var recursiveOutput = Strings.join(mapList, ',\n'); |
310 output = '$indentation{\n' | 310 output = '$indentation{\n' |
311 '$recursiveOutput' | 311 '$recursiveOutput' |
312 '\n$indentation}'; | 312 '\n$indentation}'; |
313 } else { | 313 } else { |
314 output = '$indentation${JSON.stringify(json)}'; | 314 output = '$indentation${JSON.stringify(json)}'; |
315 } | 315 } |
316 return output; | 316 return output; |
317 } | 317 } |
OLD | NEW |