extractHomeworkArticle function

List<Content> extractHomeworkArticle(
  1. Bs4Element element
)

Implementation

List<Content> extractHomeworkArticle(Bs4Element element) {
  List<Content> contents = [];
  if (element.children.isEmpty) {
    return contents;
  }
  List<Bs4Element> children = element.children;
  for (var child in children) {
    var restText = editedText(child.text, child.children);
    var newChildren = extractHomeworkArticle(child);
    switch (child.name) {
      case "h1":
        if (restText.isNotEmpty) {
          contents.add(Header(restText, newChildren));
        } else {
          contents.addAll(newChildren);
        }
        break;
      case "img":
        String? maybeSrc = child.getAttrValue("src");
        if (maybeSrc != null) {
          contents.add(Image(maybeSrc, newChildren));
        }
        break;
      case "a":
        String? maybeHref = child.getAttrValue("href");
        if (maybeHref != null) {
          contents.add(Link(child.text, maybeHref, newChildren));
        }
        break;
      default:
        if (restText.isNotEmpty) {
          contents.add(Paragraph(child.text, newChildren));
        } else {
          contents.addAll(newChildren);
        }
    }
  }
  return contents;
}