parseMap function

Map<String, dynamic> parseMap(
  1. List<String> lines,
  2. String indentMarker, {
  3. int indentLevel = 0,
})

Parses a YAML Map from the List<String> lines of a file.

Implementation

Map<String, dynamic> parseMap(List<String> lines, String indentMarker,
    {int indentLevel = 0}) {
  Map<String, dynamic> map = {};

  for (int i = 0; i < lines.length; i++) {
    String whitespace = indentMarker * (indentLevel + 1);
    final String line = lines[i].trim();

    List<String> lineSplit = line.split(':');
    String key = lineSplit.first.trim();

    // if next line is indented compared to current line
    if (i < lines.length - 1 && lines[i + 1].indexOf(whitespace) == 0) {
      final int mapStartIndex = i + 1;
      int mapEndIndex = mapStartIndex + 1;

      while (mapEndIndex < lines.length &&
          (lines[mapEndIndex].indexOf(whitespace) == 0 ||
              lines[mapEndIndex].trim().isEmpty)) {
        mapEndIndex++;
      }

      // get YAML List or recursion
      map[key] = _isList(lines[i + 1])
          ? _createList(lines.sublist(mapStartIndex, mapEndIndex))
          : parseMap(lines.sublist(mapStartIndex, mapEndIndex), indentMarker,
              indentLevel: indentLevel + 1);
      i += (mapEndIndex - mapStartIndex);
    }
    // no further indentation
    else if (line.isNotEmpty) {
      map[key] = _replaceQuotation(
          line.replaceFirst('${lineSplit.first}:', '').trim());
    }
  }

  return map;
}