sections property

Iterable<TextSection> sections

Returns all sections.

Implementation

Iterable<TextSection> get sections sync* {
  if (!_hasDirectives || !_value.contains('::')) {
    yield (TextSection(const <String>[], _value));
    return;
  }
  var directives = <String>[];
  var sb = StringBuffer();
  var previousLineWasDirective = true;
  for (var line in _value.split('\n')) {
    if (line.startsWith('::')) {
      if (line.startsWith(':::')) {
        //
        // An escaped ordinary line
        //
        if (previousLineWasDirective) {
          previousLineWasDirective = false;
        } else {
          sb.write('\n');
        }
        sb.write(line.substring(1));
      } else {
        //
        // A directive line
        //
        if (!previousLineWasDirective) {
          if (directives.isNotEmpty || sb.isNotEmpty) {
            yield (TextSection(directives, sb.toString()));
          }
          sb.clear();
          previousLineWasDirective = true;
          directives = <String>[];
        }
        line = line.substring(2);

        // Support comments
        final i = line.indexOf('//');
        if (i >= 0) {
          line = line.substring(0, i);
        }
        directives.addAll(line.split(' ').where((d) => d.isNotEmpty));
      }
    } else {
      //
      // An ordinary line
      //
      if (previousLineWasDirective) {
        previousLineWasDirective = false;
      } else {
        sb.write('\n');
      }
      sb.write(line);
    }
  }
  yield (TextSection(directives, sb.toString()));
}