splitBlocks function

List<RawBlock> splitBlocks(
  1. String content
)

Zerlegt eine Datei zeilenbasiert in @journey:-Blöcke: Start in einer Kommentarzeile, Fortsetzung in unmittelbar folgenden Kommentarzeilen, Ende an Nicht-Kommentar-Zeile oder neuem @journey:-Block.

Implementation

List<RawBlock> splitBlocks(String content) {
  final lines = content.split('\n');
  final blocks = <RawBlock>[];
  String? type;
  StringBuffer? buffer;
  int startLine = 0;

  void flush() {
    if (type != null) {
      blocks.add(RawBlock(type: type!, text: buffer.toString(), line: startLine));
    }
    type = null;
    buffer = null;
  }

  for (var i = 0; i < lines.length; i++) {
    final trimmed = lines[i].trimLeft();
    if (!trimmed.startsWith('//')) {
      flush();
      continue;
    }
    // Kommentar-Inhalt ohne führende Slashes.
    final body = trimmed.replaceFirst(RegExp(r'^/{2,}'), '');
    final match = _journeyStart.firstMatch(body);
    if (match != null) {
      flush();
      type = match.group(1)!;
      startLine = i + 1;
      buffer = StringBuffer(body.substring(match.end));
    } else if (buffer != null) {
      buffer!.write('\n');
      buffer!.write(body);
    }
  }
  flush();
  return blocks;
}