fix(epub): ignores non-publication dates for published metadata (#2226)

This commit is contained in:
James Ward
2026-08-05 17:18:09 -04:00
committed by GitHub
parent be2ac14446
commit c077551926
2 changed files with 32 additions and 19 deletions
@@ -399,8 +399,15 @@ public class EpubMetadataExtractor implements FileMetadataExtractor {
}
}
case "date" -> {
String event = el.getAttributeNS(OPF_NS, "event");
// Epub3 publication date has no `event` specified.
// Epub2 publication date has an `event` of `publication`.
// This handles both to cover our bases.
boolean isPublishedDate = event.isBlank() || event.equalsIgnoreCase("publication");
LocalDate parsed = parseDate(text);
if (parsed != null) builderMeta.publishedDate(parsed);
if (parsed != null && isPublishedDate) {
builderMeta.publishedDate(parsed);
}
}
}
}
@@ -413,22 +420,6 @@ public class EpubMetadataExtractor implements FileMetadataExtractor {
else if ("subtitle".equals(type)) builderMeta.subtitle(value);
}
if (builderMeta.build().getPublishedDate() == null) {
for (int i = 0; i < children.getLength(); i++) {
if (!(children.item(i) instanceof Element el)) continue;
if (!"meta".equals(el.getLocalName())) continue;
String prop = el.getAttribute("property").trim().toLowerCase();
String content = el.hasAttribute("content") ? el.getAttribute("content").trim() : el.getTextContent().trim();
if ("dcterms:modified".equals(prop)) {
LocalDate parsed = parseDate(content);
if (parsed != null) {
builderMeta.publishedDate(parsed);
break;
}
}
}
}
for (Map.Entry<String, String> entry : creatorsById.entrySet()) {
String id = entry.getKey();
String value = entry.getValue();
@@ -307,14 +307,14 @@ class EpubMetadataExtractorTest {
}
@Test
void fallsBackToDctermsModifiedWhenNoDate() throws IOException {
void ignoresDctermsModifiedWhenNoDate() throws IOException {
String opf = wrapOpf("""
<dc:title>Book</dc:title>
<meta property="dcterms:modified">2020-08-01T12:00:00Z</meta>
""");
BookMetadata metadata = extractor.extractMetadata(createEpub(opf));
assertThat(metadata.getPublishedDate()).isEqualTo(LocalDate.of(2020, 8, 1));
assertThat(metadata.getPublishedDate()).isNull();
}
@Test
@@ -338,6 +338,28 @@ class EpubMetadataExtractorTest {
assertThat(metadata.getPublishedDate()).isNull();
}
@Test
void usesPublicationDate() throws IOException {
String opf = wrapOpf("""
<dc:title>Book</dc:title>
<dc:date opf:event="publication">2021-12-25T00:00:00</dc:date>
""");
BookMetadata metadata = extractor.extractMetadata(createEpub(opf));
assertThat(metadata.getPublishedDate()).isNotNull();
}
@Test
void ignoresModificationDate() throws IOException {
String opf = wrapOpf("""
<dc:title>Book</dc:title>
<dc:date opf:event="modification">2021-12-25T00:00:00</dc:date>
""");
BookMetadata metadata = extractor.extractMetadata(createEpub(opf));
assertThat(metadata.getPublishedDate()).isNull();
}
}
@Nested