OLD | NEW |
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 import 'dart:collection'; | |
11 | |
12 /** | 10 /** |
13 * Gets a String representing the input Map in YAML format. | 11 * Gets a String representing the input Map in YAML format. |
14 */ | 12 */ |
15 String getYamlString(Map documentData) { | 13 String getYamlString(Map documentData) { |
16 StringBuffer yaml = new StringBuffer(); | 14 StringBuffer yaml = new StringBuffer(); |
17 _addLevel(yaml, documentData, 0); | 15 _addLevel(yaml, documentData, 0); |
18 return yaml.toString(); | 16 return yaml.toString(); |
19 } | 17 } |
20 | 18 |
21 /** | 19 /** |
22 * This recursive function builds a YAML string from [documentData] and | 20 * This recursive function builds a YAML string from [documentData] and |
23 * adds it to [yaml]. | 21 * adds it to [yaml]. |
24 * The [level] input determines the indentation of the block being processed. | 22 * The [level] input determines the indentation of the block being processed. |
25 * The [isList] input determines whether [documentData] is a member of an outer | 23 * The [isList] input determines whether [documentData] is a member of an outer |
26 * lists of maps. A map must be preceeded with a '-' if it is to exist at the | 24 * lists of maps. A map must be preceeded with a '-' if it is to exist at the |
27 * same level of indentation in the YAML output as other members of the list. | 25 * same level of indentation in the YAML output as other members of the list. |
28 */ | 26 */ |
29 void _addLevel(StringBuffer yaml, Map documentData, int level, | 27 void _addLevel(StringBuffer yaml, Map documentData, int level, |
30 {bool isList: false}) { | 28 {bool isList: false}) { |
31 // The order of the keys could be nondeterministic, but it is insufficient | 29 // The order of the keys could be nondeterministic, but it is insufficient |
32 // to just sort the keys no matter what, as their order could be significant | 30 // to just sort the keys no matter what, as their order could be significant |
33 // (i.e. parameters to a method). The order of the keys should be enforced | 31 // (i.e. parameters to a method). The order of the keys should be enforced |
34 // by the caller of this function. | 32 // by the caller of this function. |
35 var keys = documentData.keys.toList(); | 33 var keys = documentData.keys.toList(); |
36 keys.forEach((key) { | 34 keys.forEach((key) { |
37 _calcSpaces(level, yaml); | 35 _calcSpaces(level, yaml); |
38 // Only the first entry of the map should be preceeded with a '-' since | 36 // Only the first entry of the map should be preceeded with a '-' since |
39 // the map is a member of an outer list and the map as a whole must be | 37 // the map is a member of an outer list and the map as a whole must be |
40 // marked as a single member of that list. See example 2.4 at | 38 // marked as a single member of that list. See example 2.4 at |
41 // http://www.yaml.org/spec/1.2/spec.html#id2759963 | 39 // http://www.yaml.org/spec/1.2/spec.html#id2759963 |
42 if (isList && key == keys.first) { | 40 if (isList && key == keys.first) { |
43 yaml.write("- "); | 41 yaml.write("- "); |
44 level++; | 42 level++; |
45 } | 43 } |
46 yaml.write("\"$key\" : "); | 44 yaml.write("\"$key\" : "); |
47 if (documentData[key] is Map) { | 45 if (documentData[key] is Map) { |
48 yaml.write("\n"); | 46 yaml.write("\n"); |
49 _addLevel(yaml, documentData[key], level + 1); | 47 _addLevel(yaml, documentData[key], level + 1); |
(...skipping 27 matching lines...) Expand all Loading... |
77 | 75 |
78 /** | 76 /** |
79 * Based on the depth in the file, this function returns the correct spacing | 77 * Based on the depth in the file, this function returns the correct spacing |
80 * for an element in the YAML output. | 78 * for an element in the YAML output. |
81 */ | 79 */ |
82 void _calcSpaces(int spaceLevel, StringBuffer yaml) { | 80 void _calcSpaces(int spaceLevel, StringBuffer yaml) { |
83 for (int i = 0; i < spaceLevel; i++) { | 81 for (int i = 0; i < spaceLevel; i++) { |
84 yaml.write(" "); | 82 yaml.write(" "); |
85 } | 83 } |
86 } | 84 } |
OLD | NEW |