Chromium Code Reviews| 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 /** | 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 builds a YAML string from [documentData] and |
| 21 * a YAML string from the input Map. | 21 * adds it to [yaml]. |
| 22 * The [level] input determines the indentation of the block being processed. | |
| 23 * 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 | |
| 25 * same level of indentation in the YAML output as other members of the list. | |
| 22 */ | 26 */ |
| 23 // TODO(tmandel): Fix quotes with String objects. | 27 void _addLevel(StringBuffer yaml, Map documentData, int level, |
| 24 void _addLevel(StringBuffer yaml, Map documentData, int level) { | 28 {bool isList: false}) { |
| 25 documentData.keys.forEach( (key) { | 29 // Since the ordering of the keys could be non-deterministic, the keys |
| 30 // are sorted to ensure consistency in the output. | |
| 31 var keys = documentData.keys.toList(); | |
| 32 keys.sort((String first, String second) => first.compareTo(second)); | |
|
Alan Knight
2013/07/26 20:51:03
Real nit. You can omit the argument to sort and th
| |
| 33 keys.forEach((key) { | |
| 26 _calcSpaces(level, yaml); | 34 _calcSpaces(level, yaml); |
| 35 // 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 | |
| 37 // marked as a single member of that list. See example 2.4 at | |
| 38 // http://www.yaml.org/spec/1.2/spec.html#id2759963 | |
| 39 if (isList && key == keys.first) { | |
| 40 yaml.write("- "); | |
| 41 level++; | |
| 42 } | |
| 27 yaml.write("\"$key\" : "); | 43 yaml.write("\"$key\" : "); |
| 28 | |
| 29 if (documentData[key] is Map) { | 44 if (documentData[key] is Map) { |
| 30 yaml.write("\n"); | 45 yaml.write("\n"); |
| 31 _addLevel(yaml, documentData[key], level + 1); | 46 _addLevel(yaml, documentData[key], level + 1); |
| 32 | |
| 33 } else if (documentData[key] is List) { | 47 } else if (documentData[key] is List) { |
| 34 var elements = documentData[key]; | 48 var elements = documentData[key]; |
| 35 yaml.write("\n"); | 49 yaml.write("\n"); |
| 36 elements.forEach( (element) { | 50 elements.forEach( (element) { |
| 37 if (element is Map) { | 51 if (element is Map) { |
| 38 _addLevel(yaml, element, level + 1); | 52 _addLevel(yaml, element, level + 1, isList: true); |
| 39 } else { | 53 } else { |
| 40 _calcSpaces(level + 1, yaml); | 54 _calcSpaces(level + 1, yaml); |
| 41 yaml.write("- ${_processElement(element)}"); | 55 yaml.write("- ${_processElement(element)}"); |
| 42 } | 56 } |
| 43 }); | 57 }); |
| 44 | |
| 45 } else { | 58 } else { |
| 46 yaml.write(_processElement(documentData[key])); | 59 yaml.write(_processElement(documentData[key])); |
| 47 } | 60 } |
| 48 }); | 61 }); |
| 49 } | 62 } |
| 50 | 63 |
| 51 /** | 64 /** |
| 52 * Returns an escaped String form of the inputted element. | 65 * Returns an escaped String form of the inputted element. |
| 53 */ | 66 */ |
| 54 String _processElement(var element) { | 67 String _processElement(var element) { |
| 55 var contents = element.toString() | 68 var contents = element.toString() |
| 56 .replaceAll('\\', r'\\') | 69 .replaceAll('\\', r'\\') |
| 57 .replaceAll('"', r'\"') | 70 .replaceAll('"', r'\"') |
| 58 .replaceAll('\n', r'\n'); | 71 .replaceAll('\n', r'\n'); |
| 59 return '"$contents"\n'; | 72 return '"$contents"\n'; |
| 60 } | 73 } |
| 61 | 74 |
| 62 /** | 75 /** |
| 63 * Based on the depth in the file, this function returns the correct spacing | 76 * Based on the depth in the file, this function returns the correct spacing |
| 64 * for an element in the YAML output. | 77 * for an element in the YAML output. |
| 65 */ | 78 */ |
| 66 void _calcSpaces(int spaceLevel, StringBuffer yaml) { | 79 void _calcSpaces(int spaceLevel, StringBuffer yaml) { |
| 67 for (int i = 0; i < spaceLevel; i++) { | 80 for (int i = 0; i < spaceLevel; i++) { |
| 68 yaml.write(" "); | 81 yaml.write(" "); |
| 69 } | 82 } |
| 70 } | 83 } |
| OLD | NEW |