parseSessions function

List<Map<String, String>> parseSessions(
  1. String? content
)

Parses a TTL string containing session data into a list of maps. Returns a list of sessions, where each session is a map with 'start', 'end', 'type', 'silenceDuration', 'title', and 'description' keys.

Implementation

List<Map<String, String>> parseSessions(String? content) {
  if (content == null || content.isEmpty) {
    return [];
  }

  final List<Map<String, String>> sessions = [];

  // RegExp to match a session block.
  // It looks for a block starting with :session_ and ending with a literal dot
  // that is followed by whitespace or end of string.
  // The dot in timestamps (e.g., .000Z) is NOT followed by whitespace, so this distinguishes the terminator.
  final RegExp sessionBlockRegExp =
      RegExp(r':session_\d+.*?\.(?:\s+|$)', dotAll: true);

  // RegExp to extract properties within a block
  final RegExp startRegExp = RegExp(r':start "(.*?)"\^\^xsd:dateTime');
  final RegExp endRegExp = RegExp(r':end "(.*?)"\^\^xsd:dateTime');
  final RegExp typeRegExp = RegExp(r':type "(.*?)"');
  final RegExp durationRegExp = RegExp(r':silenceDuration (\d+)');
  final RegExp titleRegExp = RegExp(r':title "(.*?)"');
  final RegExp descriptionRegExp = RegExp(r':description "(.*?)"');

  final matches = sessionBlockRegExp.allMatches(content);

  for (final match in matches) {
    final block = match.group(0)!;
    final startMatch = startRegExp.firstMatch(block);
    final endMatch = endRegExp.firstMatch(block);
    final typeMatch = typeRegExp.firstMatch(block);
    final durationMatch = durationRegExp.firstMatch(block);
    final titleMatch = titleRegExp.firstMatch(block);
    final descriptionMatch = descriptionRegExp.firstMatch(block);

    if (startMatch != null && endMatch != null) {
      sessions.add({
        'start': startMatch.group(1)!,
        'end': endMatch.group(1)!,
        'type': typeMatch?.group(1) ?? 'bell',
        'silenceDuration': durationMatch?.group(1) ?? '1200',
        'title': titleMatch?.group(1) ?? '',
        'description': descriptionMatch?.group(1) ?? '',
      });
    }
  }

  // Sort by start time descending (newest first)
  sessions.sort((a, b) => b['start']!.compareTo(a['start']!));

  return sessions;
}