LyricsProvider: Improve parsing from HTML function
This commit is contained in:
@@ -40,26 +40,19 @@ QString LyricsProvider::ParseLyricsFromHTML(const QString &content, const QRegul
|
|||||||
QRegularExpressionMatch rematch = lyrics_start.match(content, start_idx);
|
QRegularExpressionMatch rematch = lyrics_start.match(content, start_idx);
|
||||||
if (!rematch.hasMatch()) break;
|
if (!rematch.hasMatch()) break;
|
||||||
|
|
||||||
const qint64 start_lyrics_tag_idx = rematch.capturedEnd();
|
const qint64 start_lyrics_idx = rematch.capturedEnd();
|
||||||
|
qint64 end_lyrics_idx = -1;
|
||||||
|
|
||||||
// Find the index of the end tag.
|
// Find the index of the end tag.
|
||||||
qint64 start_tag_idx = 0;
|
qint64 idx = start_lyrics_idx;
|
||||||
qint64 end_tag_idx = 0;
|
QRegularExpressionMatch rematch_start_tag;
|
||||||
qint64 end_tag_length = 0;
|
QRegularExpressionMatch rematch_end_tag;
|
||||||
qint64 idx = start_lyrics_tag_idx;
|
|
||||||
int tags = 1;
|
int tags = 1;
|
||||||
do {
|
do {
|
||||||
QRegularExpressionMatch rematch_start_tag = QRegularExpression(start_tag).match(content, idx);
|
rematch_start_tag = QRegularExpression(start_tag).match(content, idx);
|
||||||
start_tag_idx = rematch_start_tag.hasMatch() ? rematch_start_tag.capturedStart() : -1;
|
const qint64 start_tag_idx = rematch_start_tag.hasMatch() ? rematch_start_tag.capturedStart() : -1;
|
||||||
QRegularExpressionMatch rematch_end_tag = QRegularExpression(end_tag).match(content, idx);
|
rematch_end_tag = QRegularExpression(end_tag).match(content, idx);
|
||||||
if (rematch_end_tag.hasMatch()) {
|
const qint64 end_tag_idx = rematch_end_tag.hasMatch() ? rematch_end_tag.capturedStart() : -1;
|
||||||
end_tag_idx = rematch_end_tag.capturedStart();
|
|
||||||
end_tag_length = rematch_end_tag.capturedLength();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
end_tag_idx = -1;
|
|
||||||
end_tag_length = 0;
|
|
||||||
}
|
|
||||||
if (rematch_start_tag.hasMatch() && start_tag_idx <= end_tag_idx) {
|
if (rematch_start_tag.hasMatch() && start_tag_idx <= end_tag_idx) {
|
||||||
++tags;
|
++tags;
|
||||||
idx = start_tag_idx + rematch_start_tag.capturedLength();
|
idx = start_tag_idx + rematch_start_tag.capturedLength();
|
||||||
@@ -67,21 +60,26 @@ QString LyricsProvider::ParseLyricsFromHTML(const QString &content, const QRegul
|
|||||||
else if (rematch_end_tag.hasMatch()) {
|
else if (rematch_end_tag.hasMatch()) {
|
||||||
--tags;
|
--tags;
|
||||||
idx = end_tag_idx + rematch_end_tag.capturedLength();
|
idx = end_tag_idx + rematch_end_tag.capturedLength();
|
||||||
|
if (tags == 0) {
|
||||||
|
end_lyrics_idx = rematch_end_tag.capturedStart();
|
||||||
|
start_idx = rematch_end_tag.capturedEnd();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (tags > 0 || end_tag_idx >= start_tag_idx);
|
while (tags > 0 && (rematch_start_tag.hasMatch() || rematch_end_tag.hasMatch()));
|
||||||
|
|
||||||
start_idx = end_tag_idx + end_tag_length;
|
if (end_lyrics_idx != -1 && start_lyrics_idx < end_lyrics_idx) {
|
||||||
|
|
||||||
if (end_tag_idx > 0 || start_lyrics_tag_idx < end_tag_idx) {
|
|
||||||
if (!lyrics.isEmpty()) {
|
if (!lyrics.isEmpty()) {
|
||||||
lyrics.append("\n");
|
lyrics.append("\n");
|
||||||
}
|
}
|
||||||
lyrics.append(content.mid(start_lyrics_tag_idx, end_tag_idx - start_lyrics_tag_idx)
|
lyrics.append(content.mid(start_lyrics_idx, end_lyrics_idx - start_lyrics_idx)
|
||||||
.replace(QRegularExpression("<br[^>]+>"), "\n")
|
.replace(QRegularExpression("<br[^>]*>"), "\n")
|
||||||
.remove(QRegularExpression("<[^>]*>"))
|
.remove(QRegularExpression("<[^>]*>"))
|
||||||
.trimmed());
|
.trimmed());
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
start_idx = -1;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
while (start_idx > 0 && multiple);
|
while (start_idx > 0 && multiple);
|
||||||
@@ -93,4 +91,3 @@ QString LyricsProvider::ParseLyricsFromHTML(const QString &content, const QRegul
|
|||||||
return Utilities::DecodeHtmlEntities(lyrics);
|
return Utilities::DecodeHtmlEntities(lyrics);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user