parsePage static method

Page? parsePage(
  1. String html, [
  2. ParserBuilder parser = Substitution.fromUntis
])

Implementation

static Page? parsePage(
  String html, [
  ParserBuilder parser = Substitution.fromUntis,
]) {
  final rawHtml = html
      .replaceAll('\n', ' ')
      .replaceAll('\r', ' ')
      .replaceAll(RegExp(r' +'), ' ');
  // TODO: try to find a way to have less in this try block
  try {
    // TODO: let's also rethink the parsing code in general
    var html = htmlParse(rawHtml).first.children[1].children; //body
    final pageTitle =
        html.searchFirst((e) => e.className.contains('mon_title'))!.innerHtml;
    html = html
        .searchFirst((e) => e.className.contains('mon_list'))!
        .children
        .first //for some reason <table>s like to contain <tbody>s
        .children;
    final subs = <Substitution>[];
    final p = parser(html[0].children.map(_str).toList());
    for (var i = 1; i < html.length; i++) {
      final e = html[i].children.map(_str).toList();
      // TODO: find a way for the `parser` to determine what the lesson is
      for (final lesson in _parseIntsFromString(e[1])) {
        subs.add(p(lesson, e));
      }
    }
    return Page(matchDay(pageTitle), subs, pageTitle);
  } catch (e) {
    return null;
  }
}