parseHtml function
Implementation
@visibleForTesting
void parseHtml(Subtitle subtitle) {
//https://regex101.com/r/LtkFNE/4
final RegExp detectAll = RegExp(
r'((<(b|i|u|(font color="((#([0-9a-fA-F]+))|((rgb|rgba)\(((\d{1,3}),(\d{1,3}),(\d{1,3})|(\d{1,3}),(\d{1,3}),(\d{1,3}),(0?\.[1-9]{1,2}|1))\))|([a-z]+))"))>)+)([^<|>|\/]+)((<\/(b|i|u|font)>)+)+|([^<|>|\/]+)');
final RegExp detectFont = RegExp(r'(<font color=")');
final RegExp detectI = RegExp(r'(<i>)');
final RegExp detectB = RegExp(r'(<b>)');
final RegExp detectU = RegExp(r'(<u>)');
for (String line in subtitle.rawLines) {
int index = subtitle.rawLines.indexOf(line);
Iterable<Match> allMatches = detectAll.allMatches(line);
for (Match match in allMatches) {
String? firstMatch = match.group(1);
// not coded text
if (match.group(23) != null) {
subtitle.parsedLines[index].subLines
.add(SubLine(rawString: match.group(23)));
continue;
}
//Html-coded text
else {
SubLine subLineWithCode = SubLine();
if (detectI.hasMatch(firstMatch!)) {
subLineWithCode.htmlCode.i = true;
}
if (detectB.hasMatch(firstMatch)) {
subLineWithCode.htmlCode.b = true;
}
if (detectU.hasMatch(firstMatch)) {
subLineWithCode.htmlCode.u = true;
}
//font color
if (detectFont.hasMatch(firstMatch)) {
//hexColor
if (match.group(7) != null) {
subLineWithCode.htmlCode.fontColor = Color.hex(match.group(7)!);
}
//rgb or rgba
if (match.group(8) != null) {
if (match.group(9) == 'rgb') {
subLineWithCode.htmlCode.fontColor = Color.createRgba(
int.parse(match.group(11)!),
int.parse(match.group(12)!),
int.parse(match.group(13)!));
}
if (match.group(9) == 'rgba') {
subLineWithCode.htmlCode.fontColor = Color.createRgba(
int.parse(match.group(14)!),
int.parse(match.group(15)!),
int.parse(match.group(16)!),
num.parse(match.group(17)!));
}
}
// if color word names
if (match.group(18) != null) {
subLineWithCode.htmlCode.fontColor = colorMap.entries
.firstWhere((MapEntry entry) => entry.key == match.group(18))
.value;
}
}
subLineWithCode.rawString = match.group(19);
subtitle.parsedLines[index].subLines.add(subLineWithCode);
}
}
}
}