stringBetweenParser function

Data? stringBetweenParser(
  1. Parser parser,
  2. Data parentData,
  3. bool debug
)

Implementation

Data? stringBetweenParser(
  Parser parser,
  Data parentData,
  bool debug,
) {
  printLog("----------------------------------", debug, color: LogColor.yellow);
  printLog(
    "ID: ${parser.id} Parser: String Between",
    debug,
    color: LogColor.cyan,
  );
  List<Element>? element = getElementObject(parentData);
  if (element == null || element.isEmpty) {
    printLog(
      "StringBetween Parser: Element not found!",
      debug,
      color: LogColor.red,
    );
    return null;
  }
  Element document;
  if (element.length == 1) {
    document = element[0];
  } else {
    throw UnimplementedError("Multiple elements not supported");
  }

  String source = document.outerHtml;

  String tmp;
  String start = parser.optional?.start ?? "";
  String end = parser.optional?.end ?? "";
  final startIndex = source.indexOf(start);
  if (startIndex == -1) {
    printLog(
      "StringBetween Parser: No data found!",
      debug,
      color: LogColor.orange,
    );
    return null;
  }
  final endIndex = source.indexOf(end, startIndex + start.length);
  try {
    tmp = source.substring(startIndex + start.length, endIndex);
  } catch (e) {
    printLog(
      "StringBetween Parser: No data found!",
      debug,
      color: LogColor.orange,
    );
    return null;
  }
  return Data(parentData.url, tmp.trim());
}