runParserAndExecuteOptional method
Helper function to run parser and execute optional methods
Implementation
Future<Data?> runParserAndExecuteOptional({
required Parser parser,
required Data parentData,
required Uri? proxyUrl,
required bool debug,
}) async {
Data? parsed = await runParser(
parser: parser,
parentData: parentData,
proxyUrl: proxyUrl,
debug: debug,
);
if (parsed != null) {
Object data = parsed.obj;
if (parser.optional != null) {
/// Select nth result
Object? nth = nthOptional(parser, data, debug);
if (nth != null) {
data = nth;
}
/// Split by string
Object? splitBy = splitByOptional(parser, data, debug);
if (splitBy != null) {
data = splitBy;
}
/// Apply methods
Object? applyMethod = applyOptional(parser, data, debug);
if (applyMethod != null) {
data = applyMethod;
}
/// Replace
Object? replace = replaceOptional(parser, data, debug);
if (replace != null) {
data = replace;
}
/// Regex replace
Object? regexReplace = regexReplaceOptional(parser, data, debug);
if (regexReplace != null) {
data = regexReplace;
}
/// Regex match
Object? regexMatch = regexMatchOptional(parser, data, debug);
if (regexMatch != null) {
data = regexMatch;
}
/// Crop Start
Object? cropStart = cropStartOptional(parser, data, debug);
if (cropStart != null) {
data = cropStart;
}
/// Crop End
Object? cropEnd = cropEndOptional(parser, data, debug);
if (cropEnd != null) {
data = cropEnd;
}
/// Prepend
Object? prepend = prependOptional(parser, data, debug);
if (prepend != null) {
data = prepend;
}
/// Append
Object? append = appendOptional(parser, data, debug);
if (append != null) {
data = append;
}
/// Match with list and convert to boolean
Object? match = matchOptional(parser, data, debug);
if (match != null) {
data = match;
}
}
/// If custom cleaner is defined in Parser, clean the data.
if (parser.cleaner != null) {
printLog("Cleaning ${parser.id}...", debug, color: LogColor.green);
Function cleaner = parser.cleaner!;
Object? cleaned = cleaner(Data(parsed.url, data), debug);
if (cleaned != null) {
printLog(
"Cleaning success for ${parser.id}.",
debug,
color: LogColor.green,
);
extractedData.addAll({parser.id: cleaned});
data = cleaned;
} else {
printLog(
"Cleaning failed for ${parser.id}!!",
debug,
color: LogColor.red,
);
}
} else {
extractedData.addAll({parser.id: data});
}
return Data(parsed.url, data);
} else {
return null;
}
}