Skip to content

Commit

Permalink
fix: Add handling for opf prefixed manifest tags in epub parser (#197)
Browse files Browse the repository at this point in the history
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam authored Aug 8, 2024
1 parent 8436eeb commit b308fd6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
11 changes: 0 additions & 11 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ dependencies {

// Android core components.
implementation 'androidx.core:core-ktx:1.13.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.8.3'
implementation 'androidx.activity:activity-compose:1.9.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.8.3"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.8.4'
implementation 'androidx.activity:activity-compose:1.9.1'
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.8.4"
implementation "androidx.navigation:navigation-compose:2.7.7"
// Jetpack compose.
implementation "androidx.compose.ui:ui"
Expand Down
23 changes: 15 additions & 8 deletions app/src/main/java/com/starry/myne/epub/EpubParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,21 @@ class EpubParser {
document.metadata.selectFirstChildTag("dc:language")?.textContent ?: "en"

val metadataCoverId = document.metadata.selectChildTag("meta")
.ifEmpty { document.metadata.selectChildTag("opf:meta") }
.find { it.getAttributeValue("name") == "cover" }?.getAttributeValue("content")

val hrefRootPath = File(document.opfFilePath).parentFile ?: File("")

val manifestItems = document.manifest.selectChildTag("item").map {
EpubManifestItem(
id = it.getAttribute("id"),
absPath = it.getAttribute("href").decodedURL.hrefAbsolutePath(hrefRootPath),
mediaType = it.getAttribute("media-type"),
properties = it.getAttribute("properties")
)
}.associateBy { it.id }
val manifestItems = document.manifest.selectChildTag("item")
.ifEmpty { document.manifest.selectChildTag("opf:item") }
.map {
EpubManifestItem(
id = it.getAttribute("id"),
absPath = it.getAttribute("href").decodedURL.hrefAbsolutePath(hrefRootPath),
mediaType = it.getAttribute("media-type"),
properties = it.getAttribute("properties")
)
}.associateBy { it.id }

// Find the table of contents (toc.ncx) file.
val tocFileItem = manifestItems.values.firstOrNull {
Expand Down Expand Up @@ -223,10 +226,13 @@ class EpubParser {
val document = parseXMLFile(opfFile.data)
?: throw EpubParserException(".opf file failed to parse data")
val metadata = document.selectFirstTag("metadata")
?: document.selectFirstTag("opf:metadata")
?: throw EpubParserException(".opf file metadata section missing")
val manifest = document.selectFirstTag("manifest")
?: document.selectFirstTag("opf:manifest")
?: throw EpubParserException(".opf file manifest section missing")
val spine = document.selectFirstTag("spine")
?: document.selectFirstTag("opf:spine")
?: throw EpubParserException(".opf file spine section missing")

return EpubDocument(metadata, manifest, spine, opfFilePath)
Expand Down Expand Up @@ -313,6 +319,7 @@ class EpubParser {
var chapterIndex = 0
val chapterExtensions = listOf("xhtml", "xml", "html", "htm").map { ".$it" }
return spine.selectChildTag("itemref")
.ifEmpty { spine.selectChildTag("opf:itemref") }
.mapNotNull { manifestItems[it.getAttribute("idref")] }
.filter { item ->
chapterExtensions.any {
Expand Down

0 comments on commit b308fd6

Please sign in to comment.