int _processFile()

in tool/extract.dart [32:87]


int _processFile(File file) {
  print(file.path);

  // Look for ```dart sections.
  String source = file.readAsStringSync();
  List<String> lines = source.split('\n');

  int index = 1;
  int count = 0;

  String lastComment;

  while (index < lines.length) {
    final trimmed = lines[index].trim();
    // Look for ```dart sections.
    if ((trimmed.startsWith('```dart') ||
            trimmed.startsWith('```run-dartpad')) &&
        lastComment?.trim() != 'skip') {
      int startIndex = index + 1;
      index++;
      while (index < lines.length && !lines[index].trim().startsWith('```')) {
        index++;
      }
      final snippet = maxUnindent(lines.sublist(startIndex, index));
      _extractSnippet(file, ++count, startIndex, snippet,
          includeSource: lastComment);
    } else if (lines[index].trim().startsWith('<!--')) {
      // Look for <!-- comment sections.
      int startIndex = index;
      while (!lines[index].trim().endsWith('-->')) {
        index++;
        if (index >= lines.length) {
          throw StateError(
              'Line ${startIndex + 1} in $file has an unterminated '
              'comment - failed to find "-->" before EOF.');
        }
      }

      lastComment = lines.sublist(startIndex, index + 1).join('\n').trim();
      lastComment = lastComment.substring(4);
      if (lines[startIndex].trim() == '<!--') {
        // remove the first \n
        lastComment = lastComment.substring(1);
      }
      lastComment = lastComment.substring(0, lastComment.length - 3);
    } else if (lines[index].contains('<?code-')) {
      lastComment = 'skip';
    } else {
      lastComment = null;
    }

    index++;
  }

  return count;
}