From a2f4bb35cdb6dd005c23e67c6eda7ee14de4da6a Mon Sep 17 00:00:00 2001 From: Koitharu Date: Fri, 13 Oct 2023 13:34:07 +0300 Subject: [PATCH] [*chan] Fix title parsing --- .../parsers/site/ru/multichan/ChanParser.kt | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/ru/multichan/ChanParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/ru/multichan/ChanParser.kt index 1323e27b..39fd9297 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/ru/multichan/ChanParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/ru/multichan/ChanParser.kt @@ -56,12 +56,13 @@ internal abstract class ChanParser( val a = row.selectFirst("div.manga_row1")?.selectFirst("h2")?.selectFirst("a") ?: return@mapNotNull null val href = a.attrAsRelativeUrl("href") + val title = a.text().parseTitle() Manga( id = generateUid(href), url = href, publicUrl = href.toAbsoluteUrl(a.host ?: domain), - altTitle = a.attr("title"), - title = a.text().substringAfterLast('(').substringBeforeLast(')'), + altTitle = title.second, + title = title.first, author = row.getElementsByAttributeValueStarting( "href", "/mangaka", @@ -169,12 +170,13 @@ internal abstract class ChanParser( val info = div.selectFirst(".related_info") ?: return@mapNotNull null val a = info.selectFirst("a") ?: return@mapNotNull null val href = a.attrAsRelativeUrl("href") + val title = a.text().parseTitle() Manga( id = generateUid(href), url = href, publicUrl = href.toAbsoluteUrl(a.host ?: domain), - altTitle = a.attr("title"), - title = a.text().substringAfterLast('(').substringBeforeLast(')'), + altTitle = title.second, + title = title.first, author = info.getElementsByAttributeValueStarting( "href", "/mangaka", @@ -206,4 +208,20 @@ internal abstract class ChanParser( } private fun String.toTagName() = replace('_', ' ').toTitleCase() + + private fun String.parseTitle(): Pair { + var depth = 0 + for (i in indices.reversed()) { + val c = this[i] + if (c == '(') { + depth-- + if (depth == 0 && (i + 2) < lastIndex && i > 0) { + return substring(i + 1, lastIndex).trim() to substring(0, i).trim() + } + } else if (c == ')') { + depth++ + } + } + return this to null + } }