Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion app/src/main/kotlin/com/metrolist/music/lyrics/LyricsUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,30 @@ object LyricsUtils {
Tokenizer()
}

private val HEX_ENTITY_REGEX = "&#x([0-9a-fA-F]+);".toRegex()
private val DEC_ENTITY_REGEX = "&#(\\d+);".toRegex()

private fun decodeHtmlEntities(text: String): String =
text
.replace(HEX_ENTITY_REGEX) { match ->
match.groupValues[1].toIntOrNull(16)
?.takeIf { it in 0..0x10FFFF }
?.let { String(Character.toChars(it)) }
?: match.value
}
.replace(DEC_ENTITY_REGEX) { match ->
match.groupValues[1].toIntOrNull()
?.takeIf { it in 0..0x10FFFF }
?.let { String(Character.toChars(it)) }
?: match.value
}
.replace("'", "'")
.replace(""", "\"")
.replace("&lt;", "<")
.replace("&gt;", ">")
.replace("&nbsp;", " ")
.replace("&amp;", "&")

fun parseLyrics(lyrics: String): List<LyricsEntry> {
// Unescape JSON string if needed
val unescapedLyrics = lyrics
Expand All @@ -291,8 +315,11 @@ object LyricsUtils {
.replace("\\n", "\n")
.replace("\\r", "\r")
.replace("\\t", "\t")

// Decode HTML entities (e.g. &#x27; -> ', &amp; -> &)
val decodedLyrics = decodeHtmlEntities(unescapedLyrics)

val lines = unescapedLyrics.lines()
val lines = decodedLyrics.lines()
.filter { it.isNotBlank() && !it.trim().startsWith("[offset:") }

// Check if this is rich sync format (contains <MM:SS.mm> patterns)
Expand Down
2 changes: 1 addition & 1 deletion kugou/src/main/kotlin/com/metrolist/kugou/KuGou.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ object KuGou {
Keyword(normalizeTitle(title), normalizeArtist(artist), album)

private fun String.normalize(): String =
replace("&apos;", "'").lines().filter { line -> line.matches(ACCEPTED_REGEX) }
lines().filter { line -> line.matches(ACCEPTED_REGEX) }
.let { lines ->
// Remove useless information such as singer, writer, composer, guitar, etc.
var headCutLine = 0
Expand Down