Skip to content

Modify the parsing of the guide node in EPUB 2.0 to return the same results as landmarks in EPUB 3.x. #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,20 @@ internal class ManifestAdapter(
}
.mapValues { listOf(PublicationCollection(links = it.value)) }

val guide = if (epubVersion < 3.0) {
mapOf("landmarks" to listOf(PublicationCollection(links = packageDocument.guide)))
} else {
emptyMap()
}

// Build Publication object
return Manifest(
metadata = metadata.metadata,
links = emptyList(),
readingOrder = readingOrder,
resources = resources,
tableOfContents = toc,
subcollections = subcollections
subcollections = subcollections + guide
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
package org.readium.r2.streamer.parser.epub

import org.readium.r2.shared.InternalReadiumApi
import org.readium.r2.shared.publication.Link
import org.readium.r2.shared.publication.Properties
import org.readium.r2.shared.publication.ReadingProgression
import org.readium.r2.shared.util.Url
import org.readium.r2.shared.util.fromEpubHref
Expand All @@ -21,6 +23,7 @@ internal data class PackageDocument(
val metadata: List<MetadataItem>,
val manifest: List<Item>,
val spine: Spine,
val guide: List<Link>,
) {

companion object {
Expand All @@ -34,6 +37,7 @@ internal data class PackageDocument(
?: return null
val spineElement = document.getFirst("spine", Namespaces.OPF)
?: return null
val guideElement = document.getFirst("guide", Namespaces.OPF)

return PackageDocument(
path = filePath,
Expand All @@ -42,7 +46,8 @@ internal data class PackageDocument(
metadata = metadata,
manifest = manifestElement.get("item", Namespaces.OPF)
.mapNotNull { Item.parse(it, filePath, prefixMap) },
spine = Spine.parse(spineElement, prefixMap, epubVersion)
spine = Spine.parse(spineElement, prefixMap, epubVersion),
guide = Guide.parse(guideElement, epubVersion),
)
}
}
Expand Down Expand Up @@ -106,6 +111,26 @@ internal data class Spine(
}
}

internal data class Guide(
val links: List<Link>
) {
companion object {
// Epub 3.0+ does not support the guide element
// https://idpf.org/epub/20/spec/OPF_2.0.1_draft.htm#TOC2.6
fun parse(element: ElementNode?, epubVersion: Double): List<Link> {
if (element == null || epubVersion >= 3.0) return emptyList()

return element.get("reference", Namespaces.OPF).mapNotNull { node ->
Link(
href = node.getAttr("href")?.let { Url.fromEpubHref(it) } ?: return@mapNotNull null,
title = node.getAttr("title"),
properties = Properties().add(mapOf("type" to (node.getAttr("type") ?: "")))
)
}
}
}
}

internal data class Itemref(
val idref: String,
val linear: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
package org.readium.r2.streamer.parser.epub

import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.entry
import org.junit.Test
import org.junit.runner.RunWith
import org.readium.r2.shared.InternalReadiumApi
import org.readium.r2.shared.publication.Href
import org.readium.r2.shared.publication.Link
import org.readium.r2.shared.publication.Manifest
import org.readium.r2.shared.publication.Properties
import org.readium.r2.shared.publication.PublicationCollection
import org.readium.r2.shared.publication.ReadingProgression
import org.readium.r2.shared.publication.epub.EpubLayout
import org.readium.r2.shared.publication.epub.contains
Expand Down Expand Up @@ -231,3 +234,35 @@ class LinkMiscTest {
parsePackageDocument("package/fallbacks-termination.opf")
}
}

@RunWith(RobolectricTestRunner::class)
class GuideTest {
private val guidePub = parsePackageDocument("package/guide-epub2.opf")

@Test
fun `Guide is rightly computed`() {
assertThat(guidePub.subcollections).containsExactly(
entry("landmarks",
listOf(
PublicationCollection(links = listOf(
Link(
href = Href("toc.html")!!,
title = "Table of Contents",
properties = Properties().add(mapOf("type" to "toc"))
),
Link(
href = Href("toc.html#figures")!!,
title = "List Of Illustrations",
properties = Properties().add(mapOf("type" to "loi"))
),
Link(
href = Href("beginpage.html")!!,
title = "Introduction",
properties = Properties().add(mapOf("type" to "text"))
),
))
)
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="pub-id" version="2.0">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:title>Alice's Adventures in Wonderland</dc:title>
<meta name="cover" content="cover" />
</metadata>
<manifest>
<item id="cover" href="cover.jpg" media-type="image/jpeg" />
<item id="titlepage" href="titlepage.xhtml"/>
<item id="beginpage" href="beginpage.xhtml"/>
</manifest>
<spine>
<itemref idref="titlepage"/>
<itemref idref="beginpage"/>
</spine>
<guide>
<reference type="toc" title="Table of Contents"
href="toc.html" />
<reference type="loi" title="List Of Illustrations"
href="toc.html#figures" />
<reference type="text" title="Introduction"
href="beginpage.html" />
</guide>
</package>