chapter method

Future<UEpubChapter> chapter(
  1. int index
)

Implementation

Future<UEpubChapter> chapter(int index) async {
  if (index < 0 || index >= spine.length) return UEpubChapter(spineIndex: index, href: "", blocks: const <UEpubBlock>[], title: "", text: "");
  final UEpubSpineItem item = spine[index];
  final String html = UDocText.decodeBytes(await resource(item.href));
  final UEpubNode? document = UEpubXml.parse(html);
  if (document == null) return UEpubChapter(spineIndex: index, href: item.href, blocks: const <UEpubBlock>[], title: "", text: "");
  final UEpubStyleSheet sheet = UEpubStyleSheet();
  for (final UEpubNode link in document.findAll("link")) {
    final String rel = (link.attributes["rel"] ?? "").toLowerCase();
    final String href = link.attributes["href"] ?? "";
    if (!rel.contains("stylesheet") || href.isEmpty) continue;
    sheet.parse(UDocText.decodeBytes(await resource(_relative(item.href, href))));
  }
  for (final UEpubNode style in document.findAll("style")) {
    sheet.parse(style.textContent);
  }
  final UEpubBlockBuilder builder = UEpubBlockBuilder(sheet: sheet, baseHref: item.href, book: this);
  UEpubNode body = document;
  for (final UEpubNode node in document.findAll("body")) {
    body = node;
    break;
  }
  builder.walk(body);
  return UEpubChapter(spineIndex: index, href: item.href, blocks: builder.blocks, title: builder.title, text: builder.plainText);
}