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
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ All notable changes to this project will be documented in this file. Take a look

**Warning:** Features marked as *experimental* may change or be removed in a future release without notice. Use with caution.

<!--## [Unreleased]-->
## [Unreleased]

### Streamer

#### Fixed

* Fixed parsing the table of contents of an EPUB 3 using NCX instead of a Navigation Document.


## [2.2.0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,35 @@ class EpubParser(
?: emptyMap()

private suspend fun parseNavigationData(packageDocument: PackageDocument, fetcher: Fetcher): Map<String, List<Link>> =
if (packageDocument.epubVersion < 3.0) {
val ncxItem =
if (packageDocument.spine.toc != null) {
packageDocument.manifest.firstOrNull { it.id == packageDocument.spine.toc }
} else {
packageDocument.manifest.firstOrNull { MediaType.NCX.contains(it.mediaType) }
}
ncxItem?.let {
parseNavigationDocument(packageDocument, fetcher)
?: parseNcx(packageDocument, fetcher)
?: emptyMap()

private suspend fun parseNavigationDocument(packageDocument: PackageDocument, fetcher: Fetcher): Map<String, List<Link>>? =
packageDocument.manifest
.firstOrNull { it.properties.contains(Vocabularies.ITEM + "nav") }
?.let { navItem ->
val navPath = Href(navItem.href, baseHref = packageDocument.path).string
fetcher.readAsXmlOrNull(navPath)
?.let { NavigationDocumentParser.parse(it, navPath) }
}
?.takeUnless { it.isEmpty() }

private suspend fun parseNcx(packageDocument: PackageDocument, fetcher: Fetcher): Map<String, List<Link>>? {
val ncxItem =
if (packageDocument.spine.toc != null) {
packageDocument.manifest.firstOrNull { it.id == packageDocument.spine.toc }
} else {
packageDocument.manifest.firstOrNull { MediaType.NCX.contains(it.mediaType) }
}

return ncxItem
?.let {
val ncxPath = Href(ncxItem.href, baseHref = packageDocument.path).string
fetcher.readAsXmlOrNull(ncxPath)?.let { NcxParser.parse(it, ncxPath) }
}
} else {
val navItem = packageDocument.manifest.firstOrNull { it.properties.contains(Vocabularies.ITEM + "nav") }
navItem?.let {
val navPath = Href(navItem.href, baseHref = packageDocument.path).string
fetcher.readAsXmlOrNull(navPath)?.let { NavigationDocumentParser.parse(it, navPath) }
}
}.orEmpty()
?.takeUnless { it.isEmpty() }
}

private suspend fun parseDisplayOptions(fetcher: Fetcher): Map<String, String> {
val displayOptionsXml =
Expand Down