<commit_before>// Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/**
 * This library is used to convert data from a map to a YAML string.
 */
library dart2yaml;

/**
 * Gets a String representing the input Map in YAML format.
 */
String getYamlString(Map documentData) {
  StringBuffer yaml = new StringBuffer();
  _addLevel(yaml, documentData, 0);
  return yaml.toString();
}

/**
 * This recursive function adds to its input StringBuffer and builds
 * a YAML string from the input Map.
 */
// TODO(tmandel): Fix quotes with String objects.
void _addLevel(StringBuffer yaml, Map documentData, int level) {
  documentData.keys.forEach( (key) {
    _calcSpaces(level, yaml);
    yaml.write("\"$key\" : ");

    if (documentData[key] is Map) {
      yaml.write("\n");
      _addLevel(yaml, documentData[key], level + 1);

    } else if (documentData[key] is List) {
      var elements = documentData[key];
      yaml.write("\n");
      elements.forEach( (element) {
        if (element is Map) {
          _addLevel(yaml, element, level + 1);
        } else {
          _calcSpaces(level + 1, yaml);
          yaml.write("- ${_processElement(element)}");
        }
      });
      <commit_msg>Fixed YAML output for a list of maps<commit_after>// Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/**
 * This library is used to convert data from a map to a YAML string.
 */
library dart2yaml;

/**
 * Gets a String representing the input Map in YAML format.
 */
String getYamlString(Map documentData) {
  StringBuffer yaml = new StringBuffer();
  _addLevel(yaml, documentData, 0);
  return yaml.toString();
}

/**
 * This recursive function builds a YAML string from [documentData] and
 * adds it to [yaml].
 * The [level] input determines the indentation of the block being processed.
 * The [isList] input determines whether [documentData] is a member of an outer
 * lists of maps. A map must be preceeded with a '-' if it is to exist at the
 * same level of indentation in the YAML output as other members of the list.
 */
void _addLevel(StringBuffer yaml, Map documentData, int level,
               {bool isList: false}) {
  // Since the ordering of the keys could be non-deterministic, the keys
  // are sorted to ensure consistency in the output.
  var keys = documentData.keys.toList();
  keys.sort();
  keys.forEach((key) {
    _calcSpaces(level, yaml);
    // Only the first entry of the map should be preceeded with a '-' since
    // the map is a member of an outer list and the map as a whole must be 
    // marked as a single member of that list. See example 2.4 at
    // http://www.yaml.org/spec/1.2/spec.html#id2759963
    if (isList && key == keys.first) {
      yaml.write("- ");
      level++;
    }
    yaml.write("\"$key\" : ");
    if (documentData[key] is Map) {
      yaml.write("\n");
      _addLevel(yaml, documentData[key], level + 1);
    } else if (documentData[key] is List) {
      var elements = documentData[key];
      yaml.write("\n");
      elements.forEach( (element) {
        if (element is Map) {
          _addLevel(yaml, element, level + 1, isList: true);
        } else {
          _calcSpaces(level + 1, yaml);
          yaml.write("- ${_processElement(element)}");
        }
      });
{'fraction_non_alphanumeric': 0.11435796572070807, 'fraction_numerical': 0.00814835627985389, 'mean_word_length': 2.848648648648649, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4125689', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1030, 'n_words': 416}