🛠️ Fix empty lines pass .isNotBlank() & head > title is not filtered out

* Fixed issue, where some kind of invisible symbol passes .isNotBlank() check, resulting in empty lines
* Fixed issue with "head > title" is present and is not filtered out

Resolves: #122
This commit is contained in:
Acclorite 2024-12-16 15:02:53 +02:00
parent 9bac48c5cb
commit 85712cf566
2 changed files with 34 additions and 1 deletions

View file

@ -3,6 +3,7 @@ package ua.acclorite.book_story.data.parser
import kotlinx.coroutines.yield
import org.jsoup.nodes.Document
import ua.acclorite.book_story.presentation.core.util.clearMarkdown
import ua.acclorite.book_story.presentation.core.util.containsVisibleText
import javax.inject.Inject
class DocumentParser @Inject constructor() {
@ -26,6 +27,9 @@ class DocumentParser @Inject constructor() {
element.append("\n")
}
// Remove <head>'s title
select("title").remove()
// Markdown
select("hr").append("\n---\n")
select("b").append("**").prepend("**")
@ -52,7 +56,7 @@ class DocumentParser @Inject constructor() {
Regex("""_\s*(.*?)\s*_"""), "_$1_"
).trim()
if (formattedLine.clearMarkdown().isNotBlank()) {
if (formattedLine.clearMarkdown().containsVisibleText()) {
lines.add(formattedLine)
}
}

View file

@ -29,4 +29,33 @@ fun String.clearMarkdown(): String {
fun String.clearAllMarkdown(): String {
return replace(Regex("(_+)|(\\*+)|(#+)"), "").trim()
}
fun String.containsVisibleText(): Boolean {
return any { it.isVisibleCharacter() }
}
fun Char.isVisibleCharacter(): Boolean {
return when (this.category) {
CharCategory.UPPERCASE_LETTER,
CharCategory.LOWERCASE_LETTER,
CharCategory.TITLECASE_LETTER,
CharCategory.MODIFIER_LETTER,
CharCategory.OTHER_LETTER,
CharCategory.DECIMAL_DIGIT_NUMBER,
CharCategory.LETTER_NUMBER,
CharCategory.OTHER_NUMBER,
CharCategory.MATH_SYMBOL,
CharCategory.CURRENCY_SYMBOL,
CharCategory.OTHER_SYMBOL,
CharCategory.INITIAL_QUOTE_PUNCTUATION,
CharCategory.FINAL_QUOTE_PUNCTUATION,
CharCategory.CONNECTOR_PUNCTUATION,
CharCategory.DASH_PUNCTUATION,
CharCategory.START_PUNCTUATION,
CharCategory.END_PUNCTUATION,
CharCategory.OTHER_PUNCTUATION -> true
else -> false
}
}