| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * This library is used to convert data from a map to a YAML string. | |
| 7 */ | |
| 8 library docgen.dart2yaml; | |
| 9 | |
| 10 /** | |
| 11 * Gets a String representing the input Map in YAML format. | |
| 12 */ | |
| 13 String getYamlString(Map documentData) { | |
| 14 StringBuffer yaml = new StringBuffer(); | |
| 15 _addLevel(yaml, documentData, 0); | |
| 16 return yaml.toString(); | |
| 17 } | |
| 18 | |
| 19 /** | |
| 20 * This recursive function builds a YAML string from [documentData] and | |
| 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. | |
| 26 */ | |
| 27 void _addLevel(StringBuffer yaml, Map documentData, int level, | |
| 28 {bool isList: false}) { | |
| 29 // The order of the keys could be nondeterministic, but it is insufficient | |
| 30 // to just sort the keys no matter what, as their order could be significant | |
| 31 // (i.e. parameters to a method). The order of the keys should be enforced | |
| 32 // by the caller of this function. | |
| 33 var keys = documentData.keys.toList(); | |
| 34 keys.forEach((key) { | |
| 35 _calcSpaces(level, yaml); | |
| 36 // Only the first entry of the map should be preceeded with a '-' since | |
| 37 // the map is a member of an outer list and the map as a whole must be | |
| 38 // marked as a single member of that list. See example 2.4 at | |
| 39 // http://www.yaml.org/spec/1.2/spec.html#id2759963 | |
| 40 if (isList && key == keys.first) { | |
| 41 yaml.write("- "); | |
| 42 level++; | |
| 43 } | |
| 44 yaml.write("\"$key\" : "); | |
| 45 if (documentData[key] is Map) { | |
| 46 yaml.write("\n"); | |
| 47 _addLevel(yaml, documentData[key], level + 1); | |
| 48 } else if (documentData[key] is List) { | |
| 49 var elements = documentData[key]; | |
| 50 yaml.write("\n"); | |
| 51 elements.forEach( (element) { | |
| 52 if (element is Map) { | |
| 53 _addLevel(yaml, element, level + 1, isList: true); | |
| 54 } else { | |
| 55 _calcSpaces(level + 1, yaml); | |
| 56 yaml.write("- ${_processElement(element)}"); | |
| 57 } | |
| 58 }); | |
| 59 } else { | |
| 60 yaml.write(_processElement(documentData[key])); | |
| 61 } | |
| 62 }); | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Returns an escaped String form of the inputted element. | |
| 67 */ | |
| 68 String _processElement(var element) { | |
| 69 var contents = element.toString() | |
| 70 .replaceAll('\\', r'\\') | |
| 71 .replaceAll('"', r'\"') | |
| 72 .replaceAll('\n', r'\n'); | |
| 73 return '"$contents"\n'; | |
| 74 } | |
| 75 | |
| 76 /** | |
| 77 * Based on the depth in the file, this function returns the correct spacing | |
| 78 * for an element in the YAML output. | |
| 79 */ | |
| 80 void _calcSpaces(int spaceLevel, StringBuffer yaml) { | |
| 81 for (int i = 0; i < spaceLevel; i++) { | |
| 82 yaml.write(" "); | |
| 83 } | |
| 84 } | |
| OLD | NEW |