diff --git a/app/src/main/java/ua/acclorite/book_story/data/parser/epub/EpubTextParser.kt b/app/src/main/java/ua/acclorite/book_story/data/parser/epub/EpubTextParser.kt index a39ddc78..f5b7f32d 100644 --- a/app/src/main/java/ua/acclorite/book_story/data/parser/epub/EpubTextParser.kt +++ b/app/src/main/java/ua/acclorite/book_story/data/parser/epub/EpubTextParser.kt @@ -4,6 +4,8 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import nl.siegmann.epublib.epub.EpubReader import org.jsoup.Jsoup +import org.jsoup.nodes.Document.OutputSettings +import org.jsoup.safety.Safelist import ua.acclorite.book_story.R import ua.acclorite.book_story.data.parser.TextParser import ua.acclorite.book_story.domain.model.StringWithId @@ -16,6 +18,7 @@ import java.io.InputStreamReader import java.nio.charset.Charset import javax.inject.Inject + class EpubTextParser @Inject constructor() : TextParser { override suspend fun parse(file: File): Resource> { @@ -44,9 +47,8 @@ class EpubTextParser @Inject constructor() : TextParser { inputStream.close() } - while (withContext(Dispatchers.IO) { - reader.readLine() - }.also { line = it } != null) { + while (withContext(Dispatchers.IO) { reader.readLine() } + .also { line = it } != null) { unformattedText.append(line).append("\n") } @@ -57,11 +59,28 @@ class EpubTextParser @Inject constructor() : TextParser { val stringWithIds = mutableListOf() - Jsoup.parse(unformattedText.toString()).wholeText().split("\n").forEach { - if (it.isNotBlank()) { - stringWithIds.add(StringWithId(it.trim())) + val parsedText = Jsoup.parse(unformattedText.toString()) + parsedText.outputSettings(OutputSettings().prettyPrint(false)) + parsedText.select("br").append("\n") + parsedText.select("p").prepend("\n") + parsedText.select("em").append(" ").prepend("") + + val formattedText = Jsoup.clean( + parsedText.html(), + "", + Safelist.none(), + OutputSettings().prettyPrint(false) + ) + + formattedText + .replace(" ", "") + .replace("\u00a0", "") + .split("\n") + .forEach { + if (it.isNotBlank()) { + stringWithIds.add(StringWithId(it.trim())) + } } - } if (stringWithIds.isEmpty()) { return Resource.Error(UIText.StringResource(R.string.error_file_empty)) diff --git a/app/src/main/java/ua/acclorite/book_story/data/parser/pdf/PdfTextParser.kt b/app/src/main/java/ua/acclorite/book_story/data/parser/pdf/PdfTextParser.kt index e9b1496e..a391cbee 100644 --- a/app/src/main/java/ua/acclorite/book_story/data/parser/pdf/PdfTextParser.kt +++ b/app/src/main/java/ua/acclorite/book_story/data/parser/pdf/PdfTextParser.kt @@ -46,12 +46,62 @@ class PdfTextParser @Inject constructor(private val application: Application) : val lines = mutableListOf() unformattedLines.forEachIndexed { index, string -> - if (string.trim().first().isLowerCase() && index > 0) { - lines[lines.lastIndex] += " ${string.trim()}" - } else { - lines.add(string.trim()) + try { + val line = string.trim() + + if (index == 0) { + lines.add(line) + return@forEachIndexed + } + + if ( + line.all { + if (it == ' ') { + true + } else if (it.isUpperCase() || it.isDigit()) { + true + } else { + false + } + } + ) { + return@forEachIndexed + } + + if (line.all { it.isDigit() }) { + return@forEachIndexed + } + + if (line.first().isLowerCase()) { + val currentLine = lines[lines.lastIndex] + + if (currentLine.last() == '-') { + if (currentLine[currentLine.lastIndex - 1].isLowerCase()) { + lines[lines.lastIndex] = currentLine.dropLast(1) + line + return@forEachIndexed + } + } + + lines[lines.lastIndex] += " $line" + return@forEachIndexed + } + + if (line.first().isUpperCase() || line.first().isDigit()) { + lines.add(line) + return@forEachIndexed + } + + if (line.first().isLetter()) { + lines[lines.lastIndex] += " $line" + return@forEachIndexed + } + + } catch (e: Exception) { + e.printStackTrace() + return@forEachIndexed } } + lines.forEach { line -> stringWithIds.add(StringWithId(line.trim())) } @@ -62,6 +112,7 @@ class PdfTextParser @Inject constructor(private val application: Application) : return Resource.Success(stringWithIds) } catch (e: Exception) { + e.printStackTrace() return Resource.Error( UIText.StringResource( R.string.error_query,