Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(399)

Side by Side Diff: pkg/docgen/lib/dart2yaml.dart

Issue 20162006: Fixed YAML output for a list of maps (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 * This library is used to convert data from a map to a YAML string. 6 * This library is used to convert data from a map to a YAML string.
7 */ 7 */
8 library dart2yaml; 8 library dart2yaml;
9 9
10 /** 10 /**
11 * Gets a String representing the input Map in YAML format. 11 * Gets a String representing the input Map in YAML format.
12 */ 12 */
13 String getYamlString(Map documentData) { 13 String getYamlString(Map documentData) {
14 StringBuffer yaml = new StringBuffer(); 14 StringBuffer yaml = new StringBuffer();
15 _addLevel(yaml, documentData, 0); 15 _addLevel(yaml, documentData, 0);
16 return yaml.toString(); 16 return yaml.toString();
17 } 17 }
18 18
19 /** 19 /**
20 * This recursive function adds to its input StringBuffer and builds 20 * This recursive function adds to its input StringBuffer and builds
21 * a YAML string from the input Map. 21 * a YAML string from the input Map.
22 */ 22 */
Alan Knight 2013/07/25 17:46:42 This could use some documentation for the paramete
Tate Mandel 2013/07/25 22:01:53 Done.
23 // TODO(tmandel): Fix quotes with String objects. 23 void _addLevel(StringBuffer yaml, Map documentData, int level,
24 void _addLevel(StringBuffer yaml, Map documentData, int level) { 24 {bool isList: false}) {
25 documentData.keys.forEach( (key) { 25 documentData.keys.forEach((key) {
26 _calcSpaces(level, yaml); 26 _calcSpaces(level, yaml);
27 if (isList && key == documentData.keys.first) {
Alan Knight 2013/07/25 17:46:42 Since the order of keys is non-deterministic, I as
Tate Mandel 2013/07/25 22:01:53 documentData can have many elements, but the first
Alan Knight 2013/07/25 23:25:04 I think the ordering is actually quite messy. If I
Tate Mandel 2013/07/26 20:33:04 Done.
28 yaml.write("- ");
29 level++;
30 }
27 yaml.write("\"$key\" : "); 31 yaml.write("\"$key\" : ");
28
29 if (documentData[key] is Map) { 32 if (documentData[key] is Map) {
30 yaml.write("\n"); 33 yaml.write("\n");
31 _addLevel(yaml, documentData[key], level + 1); 34 _addLevel(yaml, documentData[key], level + 1);
32
33 } else if (documentData[key] is List) { 35 } else if (documentData[key] is List) {
34 var elements = documentData[key]; 36 var elements = documentData[key];
35 yaml.write("\n"); 37 yaml.write("\n");
36 elements.forEach( (element) { 38 elements.forEach( (element) {
37 if (element is Map) { 39 if (element is Map) {
38 _addLevel(yaml, element, level + 1); 40 _addLevel(yaml, element, level + 1, isList: true);
39 } else { 41 } else {
40 _calcSpaces(level + 1, yaml); 42 _calcSpaces(level + 1, yaml);
41 yaml.write("- ${_processElement(element)}"); 43 yaml.write("- ${_processElement(element)}");
42 } 44 }
43 }); 45 });
44
45 } else { 46 } else {
46 yaml.write(_processElement(documentData[key])); 47 yaml.write(_processElement(documentData[key]));
47 } 48 }
48 }); 49 });
49 } 50 }
50 51
51 /** 52 /**
52 * Returns an escaped String form of the inputted element. 53 * Returns an escaped String form of the inputted element.
53 */ 54 */
54 String _processElement(var element) { 55 String _processElement(var element) {
55 var contents = element.toString() 56 var contents = element.toString()
56 .replaceAll('\\', r'\\') 57 .replaceAll('\\', r'\\')
57 .replaceAll('"', r'\"') 58 .replaceAll('"', r'\"')
58 .replaceAll('\n', r'\n'); 59 .replaceAll('\n', r'\n');
59 return '"$contents"\n'; 60 return '"$contents"\n';
60 } 61 }
61 62
62 /** 63 /**
63 * Based on the depth in the file, this function returns the correct spacing 64 * Based on the depth in the file, this function returns the correct spacing
64 * for an element in the YAML output. 65 * for an element in the YAML output.
65 */ 66 */
66 void _calcSpaces(int spaceLevel, StringBuffer yaml) { 67 void _calcSpaces(int spaceLevel, StringBuffer yaml) {
67 for (int i = 0; i < spaceLevel; i++) { 68 for (int i = 0; i < spaceLevel; i++) {
68 yaml.write(" "); 69 yaml.write(" ");
69 } 70 }
70 } 71 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698