feat(scanner): Calibre-aligned OPF metadata extraction
Release / build-and-push (push) Successful in 3m5s
Release / build-and-push (push) Successful in 3m5s
Adopt Calibre's reading conventions for the Dublin Core metadata that
parseOPFContent now pulls from the structured OPF parse:
- Titles: EPUB3 title-type selection (prefer 'main', join a distinct
subtitle with ': ' exactly as Calibre stores it). There is no separate
subtitle column by design - Calibre-sidecar books arrive pre-joined,
so a column would stay empty for most libraries and force every client
to reimplement concatenation.
- Genre: first dc:subject, mirroring the existing processGenresAndTags
behavior of the Calibre-sidecar path; the embedded path never
populated Genre before. Subjects stay one-element-one-tag - Library
of Congress headings legitimately contain commas ("Holmes, Sherlock
(Fictitious character) -- Fiction") and must not be split.
- Identifiers: urn:isbn:/urn:asin: prefixed values parse in addition to
opf:scheme attributes, and the scheme-less fallback now requires an
ISBN-shaped value (10/13 digits, optional separators/trailing X) so
URIs like the Gutenberg identifiers cannot masquerade as ISBNs -
observed live on 'A Study in Scarlet'.
- Series: EPUB3 belongs-to-collection with collection-type=series and
group-position refines, ahead of the classic calibre:series metas.
- Audiobookshelf metadata.json sidecars join their subtitle field into
the title the same way.
Tests cover title-type main+subtitle joining, belongs-to-collection
series with fractional group-position, urn:isbn extraction, genre/tag
parity, and comma preservation inside subject headings.
This commit is contained in:
@@ -3,6 +3,7 @@ package services
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -77,6 +78,112 @@ func parseOPFXML(content []byte) (*opfDocument, error) {
|
||||
return &doc, nil
|
||||
}
|
||||
|
||||
// refinesFor maps an element id to its EPUB3 refining metas
|
||||
// (those whose refines attribute starts with '#').
|
||||
func (d *opfDocument) refinesFor(id string) []opfMeta {
|
||||
var out []opfMeta
|
||||
if id == "" {
|
||||
return out
|
||||
}
|
||||
for _, m := range d.Metadata.Metas {
|
||||
if strings.HasPrefix(m.Refines, "#") && m.Refines[1:] == id {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// refinesProperty returns the value of the first refining meta carrying the
|
||||
// given property (e.g. "title-type", "collection-type", "group-position").
|
||||
func refinesProperty(metas []opfMeta, property string) (string, bool) {
|
||||
for _, m := range metas {
|
||||
if strings.EqualFold(m.Property, property) {
|
||||
if v := strings.TrimSpace(m.Value); v != "" {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// selectTitle ports Calibre's read_title: prefer the dc:title refined as
|
||||
// title-type "main"; fall back to the first non-empty title. A distinct
|
||||
// subtitle (title-type containing "subtitle"/"sub-title") is joined onto the
|
||||
// main title with ": ", exactly as Calibre stores it.
|
||||
func (d *opfDocument) selectTitle() string {
|
||||
var first, main, subtitle string
|
||||
for _, t := range d.Metadata.Titles {
|
||||
v := strings.TrimSpace(t.Value)
|
||||
if v == "" {
|
||||
continue
|
||||
}
|
||||
if first == "" {
|
||||
first = v
|
||||
}
|
||||
tt, ok := refinesProperty(d.refinesFor(t.ID), "title-type")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
switch strings.ToLower(tt) {
|
||||
case "main":
|
||||
if main == "" {
|
||||
main = v
|
||||
}
|
||||
default:
|
||||
l := strings.ToLower(tt)
|
||||
if strings.Contains(l, "subtitle") || strings.Contains(l, "sub-title") {
|
||||
if subtitle == "" {
|
||||
subtitle = v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
title := main
|
||||
if title == "" {
|
||||
title = first
|
||||
}
|
||||
if subtitle != "" && subtitle != title {
|
||||
title = title + ": " + subtitle
|
||||
}
|
||||
return title
|
||||
}
|
||||
|
||||
// readSeries ports Calibre's read_series: EPUB3 belongs-to-collection (with a
|
||||
// collection-type=series refine and group-position index) first, then the
|
||||
// classic calibre:series / calibre:series_index metas.
|
||||
func (d *opfDocument) readSeries() (series string, index float64) {
|
||||
for _, m := range d.Metadata.Metas {
|
||||
if !strings.EqualFold(m.Property, "belongs-to-collection") {
|
||||
continue
|
||||
}
|
||||
name := strings.TrimSpace(m.Value)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
refines := d.refinesFor(m.ID)
|
||||
if ct, ok := refinesProperty(refines, "collection-type"); !ok || !strings.EqualFold(ct, "series") {
|
||||
continue
|
||||
}
|
||||
if gp, ok := refinesProperty(refines, "group-position"); ok {
|
||||
if v, err := strconv.ParseFloat(strings.TrimSpace(gp), 64); err == nil {
|
||||
index = v
|
||||
}
|
||||
}
|
||||
return name, index
|
||||
}
|
||||
for _, m := range d.Metadata.Metas {
|
||||
switch m.Name {
|
||||
case "calibre:series":
|
||||
series = m.Content
|
||||
case "calibre:series_index":
|
||||
if v, err := strconv.ParseFloat(strings.TrimSpace(m.Content), 64); err == nil {
|
||||
index = v
|
||||
}
|
||||
}
|
||||
}
|
||||
return series, index
|
||||
}
|
||||
|
||||
// itemByID returns manifest items with id, href and media-type, keyed by id.
|
||||
func (d *opfDocument) itemByID() map[string]opfItem {
|
||||
m := make(map[string]opfItem, len(d.Manifest.Items))
|
||||
@@ -166,6 +273,27 @@ func (d *opfDocument) coverPageHref() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// isISBNLike reports whether a bare identifier value is shaped like an ISBN
|
||||
// (digits, optional hyphens/spaces, optional trailing X; 10 or 13
|
||||
// significant characters). Guards the scheme-less dc:identifier fallback
|
||||
// against URLs and UUIDs sharing the same slot.
|
||||
func isISBNLike(v string) bool {
|
||||
digits := 0
|
||||
for i, r := range v {
|
||||
switch {
|
||||
case r >= '0' && r <= '9':
|
||||
digits++
|
||||
case r == '-' || r == ' ':
|
||||
// separator
|
||||
case (r == 'X' || r == 'x') && i == len(v)-1:
|
||||
digits++ // ISBN-10 check character
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return digits == 10 || digits == 13
|
||||
}
|
||||
|
||||
// findImageReferenceInPage extracts the first raster image reference from a
|
||||
// cover (X)HTML page: <img src="..."> or SVG <image xlink:href="...">.
|
||||
// Token-based parsing keeps it tolerant of mixed namespaces and fragments.
|
||||
|
||||
Reference in New Issue
Block a user