fix(metadata): extract epub titles per spec with regards to ordering (#1637)
This commit is contained in:
+6
-2
@@ -206,6 +206,7 @@ public class EpubMetadataExtractor implements FileMetadataExtractor {
|
||||
|
||||
Map<String, String> titlesById = new HashMap<>();
|
||||
Map<String, String> titleTypeById = new HashMap<>();
|
||||
boolean hasTitle = false;
|
||||
|
||||
for (int i = 0; i < children.getLength(); i++) {
|
||||
if (!(children.item(i) instanceof Element el)) continue;
|
||||
@@ -218,7 +219,10 @@ public class EpubMetadataExtractor implements FileMetadataExtractor {
|
||||
String id = el.getAttribute("id");
|
||||
if (StringUtils.isNotBlank(id)) {
|
||||
titlesById.put(id, text);
|
||||
} else {
|
||||
}
|
||||
|
||||
if (!hasTitle) {
|
||||
hasTitle = true;
|
||||
builderMeta.title(text);
|
||||
}
|
||||
}
|
||||
@@ -404,7 +408,7 @@ public class EpubMetadataExtractor implements FileMetadataExtractor {
|
||||
for (Map.Entry<String, String> entry : titlesById.entrySet()) {
|
||||
String id = entry.getKey();
|
||||
String value = entry.getValue();
|
||||
String type = titleTypeById.getOrDefault(id, "main");
|
||||
String type = titleTypeById.get(id);
|
||||
if ("main".equals(type)) builderMeta.title(value);
|
||||
else if ("subtitle".equals(type)) builderMeta.subtitle(value);
|
||||
}
|
||||
|
||||
+54
@@ -186,6 +186,60 @@ class EpubMetadataExtractorTest {
|
||||
assertThat(metadata.getSubtitle()).isEqualTo("The Subtitle");
|
||||
}
|
||||
|
||||
@Test
|
||||
void distinguishesMainTitleWithoutRefines() throws IOException {
|
||||
String opf = wrapOpf("""
|
||||
<dc:title id="maintitle">Main Title</dc:title>
|
||||
<dc:title id="subtitle">The Subtitle</dc:title>
|
||||
""");
|
||||
BookMetadata metadata = extractor.extractMetadata(createEpub(opf));
|
||||
|
||||
assertThat(metadata.getTitle()).isEqualTo("Main Title");
|
||||
assertThat(metadata.getSubtitle()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void distinguishesMainTitleWithoutIDsOrRefines() throws IOException {
|
||||
String opf = wrapOpf("""
|
||||
<dc:title>Main Title</dc:title>
|
||||
<dc:title>The Subtitle</dc:title>
|
||||
""");
|
||||
BookMetadata metadata = extractor.extractMetadata(createEpub(opf));
|
||||
|
||||
assertThat(metadata.getTitle()).isEqualTo("Main Title");
|
||||
assertThat(metadata.getSubtitle()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void distinguishesRefinedOverrideFallbackBehavior() throws IOException {
|
||||
String opf = wrapOpf("""
|
||||
<dc:title>Something else</dc:title>
|
||||
<dc:title id="t1">Another something else</dc:title>
|
||||
<dc:title id="t2">Main Title</dc:title>
|
||||
<dc:title id="t3">Another title</dc:title>
|
||||
<dc:title id="t4">The Subtitle</dc:title>
|
||||
<dc:title>Even more titles</dc:title>
|
||||
<meta refines="#t2" property="title-type">main</meta>
|
||||
<meta refines="#t4" property="title-type">subtitle</meta>
|
||||
""");
|
||||
BookMetadata metadata = extractor.extractMetadata(createEpub(opf));
|
||||
|
||||
assertThat(metadata.getTitle()).isEqualTo("Main Title");
|
||||
assertThat(metadata.getSubtitle()).isEqualTo("The Subtitle");
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankTitlesFallbackToFilename() throws IOException {
|
||||
String opf = wrapOpf("""
|
||||
<dc:title> </dc:title>
|
||||
<dc:title>The Subtitle</dc:title>
|
||||
""");
|
||||
BookMetadata metadata = extractor.extractMetadata(createEpub(opf));
|
||||
|
||||
assertThat(metadata.getTitle()).isEqualTo("test");
|
||||
assertThat(metadata.getSubtitle()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void titleWithoutIdSetDirectly() throws IOException {
|
||||
String opf = wrapOpf("<dc:title>Direct Title</dc:title>");
|
||||
|
||||
Reference in New Issue
Block a user