parse method

Future<void> parse()

Parsing the book

Implementation

Future<void> parse() async {
  String res = await file.readAsString();

  /// parse [images]
  final Iterable<RegExpMatch> _images = RegExp(r'<binary[\s\S]+?>([\s\S]+?)<\/binary>').allMatches(res);
  images = [];
  for (var image in _images) {
    images.add(FB2Image(image.group(0)!));
  }

  /// replacing the fb2 tag <image ...> with html tag <img ...>
  res = res.replaceAllMapped(RegExp(r'<image([\s\S]+?)\/>'), (match) {
    String name = RegExp(r'="#([\s\S]+?)"').firstMatch(match.group(1)!)?.group(1) as String;
    FB2Image? currentImage;
    for (var image in images) {
      if (image.name == name) currentImage = image;
    }
    if (currentImage == null) return match.group(0)!;
    return '<img src="data:image/png;base64, ${currentImage.bytes}"/>';
  });

  /// replacing the fb2 tag <empty-line/> with html tag <br>
  res = res.replaceAllMapped(RegExp(r'<empty-line.?>'), (_) {
    return '<br>';
  });

  /// remove the tag <a l:href ...>
  res = res.replaceAllMapped(RegExp(r'<a ([a-zA-Z\:]*)href([\s\S]+?)>([\s\S]+?)<\/a>'), (match) {
    return '${match.group(3)}';
  });

  /// parse [description]
  final String description = RegExp(r"<description>([\s\S]+)<\/description>").firstMatch(res)?.group(1) as String;
  this.description = FB2Description(description);

  /// parse [body]
  final String body = RegExp(r"<body>([\s\S]+)<\/body>").firstMatch(res)?.group(1) as String;
  this.body = FB2Body(body);
}