github.com/readium/readium-lcp-server@v0.0.0-20240509124024-799e77a0bbd6/epub/opf/opf.go (about)

     1  // Copyright 2020 Readium Foundation. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license
     3  // that can be found in the LICENSE file exposed on Github (readium) in the project repository.
     4  
     5  package opf
     6  
     7  import (
     8  	"encoding/xml"
     9  	"io"
    10  
    11  	"golang.org/x/net/html/charset"
    12  )
    13  
    14  // Package is the main opf structure
    15  type Package struct {
    16  	BasePath string   `xml:"-"`
    17  	Metadata Metadata `xml:"http://www.idpf.org/2007/opf metadata"`
    18  	Manifest Manifest `xml:"http://www.idpf.org/2007/opf manifest"`
    19  }
    20  
    21  // Metadata is the package metadata structure
    22  type Metadata struct {
    23  	Identifier  string   `json:"identifier" xml:"http://purl.org/dc/elements/1.1/ identifier"`
    24  	Title       []string `json:"title" xml:"http://purl.org/dc/elements/1.1/ title"`
    25  	Description string   `json:"description" xml:"http://purl.org/dc/elements/1.1/ description"`
    26  	Date        string   `json:"date" xml:"http://purl.org/dc/elements/1.1/ date"`
    27  	Author      []string `json:"author" xml:"http://purl.org/dc/elements/1.1/ creator"`
    28  	Contributor []string `json:"contributor" xml:"http://purl.org/dc/elements/1.1/ contributor"`
    29  	Publisher   []string `json:"publisher" xml:"http://purl.org/dc/elements/1.1/ publisher"`
    30  	Language    []string `json:"language" xml:"http://purl.org/dc/elements/1.1/ language"`
    31  	Subject     []string `json:"subject" xml:"http://purl.org/dc/elements/1.1/ subject"`
    32  	Metas       []Meta   `xml:"http://www.idpf.org/2007/opf meta"`
    33  }
    34  
    35  // Meta is the metadata item structure
    36  type Meta struct {
    37  	Name     string `xml:"name,attr"` // EPUB 2
    38  	Content  string `xml:"content,attr"`
    39  	Property string `xml:"property,attr"` // EPUB 3
    40  	Refines  string `xml:"refines,attr"`
    41  	Text     string `xml:",chardata"`
    42  }
    43  
    44  // Manifest is the package manifest structure
    45  type Manifest struct {
    46  	Items []Item `xml:"http://www.idpf.org/2007/opf item"`
    47  }
    48  
    49  // Item is the manifest item structure
    50  type Item struct {
    51  	ID         string `xml:"id,attr"`
    52  	Href       string `xml:"href,attr"`
    53  	MediaType  string `xml:"media-type,attr"`
    54  	Properties string `xml:"properties,attr"`
    55  }
    56  
    57  // ItemWithPath looks for the manifest item corresponding to a given path
    58  func (m Manifest) ItemWithPath(path string) (Item, bool) {
    59  	for _, i := range m.Items {
    60  		if i.Href == path { // FIXME(JPB) Canonicalize the path
    61  			return i, true
    62  		}
    63  	}
    64  	return Item{}, false
    65  }
    66  
    67  // Parse parses the opf xml struct and returns a Package object
    68  func Parse(r io.Reader) (Package, error) {
    69  	var p Package
    70  	xd := xml.NewDecoder(r)
    71  	// deal with non utf-8 xml files
    72  	xd.CharsetReader = charset.NewReaderLabel
    73  	err := xd.Decode(&p)
    74  	return p, err
    75  }