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