Documentation
¶
Overview ¶
Example ¶
var err error var b ugarit.Book var target io.WriteCloser var gen ugarit.IndexGenerator var coverjpg *os.File var newId string newId = time.Now().Format("20060102150405") // The cover thumbnail is cached by iBooks and apparently, is referenced by // the epub filename. So, changing the filename when generating a new eBook // version (with a new cover) was the only way I found to make iBooks update // the book cover target, err = os.OpenFile("test-"+newId+".epub", os.O_CREATE|os.O_RDWR, 0600) if err != nil { fmt.Printf("Open error: %s\n", err) os.Exit(0) } b, err = epub30.New( target, // io.Writer where to save the epub contents []string{"My Amazing Book Title"}, // eBook title []string{"en"}, // eBook language []string{newId}, // eBook ID []epub30.Author{ epub30.Author{Data: "Me"}, epub30.Author{Data: "Myself"}, }, // eBook author(s) []string{"MY Dear Publisher"}, // eBook Publisher []epub30.Date{epub30.Date{Data: "2013-12-20"}}, // Date published // epub30.Signature{Href: "../META-INF/signatures.xml#AsYouLikeItSignature"}, epub30.Signature{}, []epub30.Metatag{ // Any metatags here epub30.Metatag{Property: "dcterms:dateCopyrighted", Data: "9999-01-01"}, }, "ltr", // PageProgression use "ltr" (left-to-right) or "rtl" (right-to-left) epub30.Versioner{}) // provide a versioner interface or use the package provided one // which just generate a version string using the current time // Start the book first providing the cover coverjpg, err = os.Open("cover.jpg") if err != nil { fmt.Printf("Open jpeg error: %s\n", err) os.Exit(0) } _, _, err = b.AddCover( "cover.jpg", // internal pathname where it will be stored "image/jpeg", // the mimetype coverjpg, // the contents nil) if err != nil { fmt.Printf("AddCover error: %s\n", err) os.Exit(0) } // Then add pages to your book _, _, _, err = b.AddPage( "p1.xhtml", // internal pathname where it will be stored "application/xhtml+xml", // the mimetype // the contents: // if provided it will be entirely read and closed // if not provided a new empty file will be created and an io.Writer will be returned strings.NewReader("<html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head><body>hello</body></html>"), "", // The optional page id, it will be generated if left empty &epub30.EPubOptions{ TOCTitle: "Summary", // TOC Title must be provided only once TOCItemTitle: "Hello Chapter", // Title for the page being added }) if err != nil { fmt.Printf("AddPage error: %s\n", err) os.Exit(0) } // Create a TOC compatible with epub3 gen, err = epub30.NewIndexGenerator() if err != nil { fmt.Printf("IndexGenerator 3.0 error: %s\n", err) os.Exit(0) } b.AddTOC(gen, "") // Create a TOC compatible with epub2 gen, err = epub20.NewIndexGenerator( "en", // Language newId, // Id "My Amazing Book Title", // Book title "Me", // Author b) // The book object if err != nil { fmt.Printf("IndexGenerator 2.01 error: %s\n", err) os.Exit(0) } b.AddTOC(gen, "") // You MUST close the eBook, some important operations are done upon closing b.Close()
Output: Hello
Index ¶
- Constants
- type Author
- type Book
- func (b *Book) AddCover(path string, mimetype string, src io.Reader, options interface{}) (string, io.Writer, error)
- func (b *Book) AddFile(path string, mimetype string, src io.Reader, id string, options interface{}) (string, io.Writer, error)
- func (b *Book) AddPage(path string, mimetype string, src io.Reader, id string, options interface{}) (string, io.Writer, ugarit.TOCRef, error)
- func (b *Book) AddTOC(gen ugarit.IndexGenerator, id string) (string, error)
- func (b *Book) Close() error
- func (b *Book) SpineAttr(key, val string)
- func (b *Book) SubSectionStyle(sty ugarit.SectionStyle)
- func (b *Book) TOCChild(n int) ugarit.TOC
- func (b *Book) TOCLen() int
- type Date
- type EPubOptions
- type Guide
- type Identifier
- type IndexGenerator
- func (gen IndexGenerator) AddItem(item *html.Node) error
- func (gen IndexGenerator) GetDocument() (io.Reader, error)
- func (gen IndexGenerator) GetId() string
- func (gen IndexGenerator) GetMimeType() string
- func (gen IndexGenerator) GetPathName() string
- func (gen IndexGenerator) GetPropertyValue() string
- type IndexOptions
- type Manifest
- type Metadata
- type Metatag
- type Package
- type Reference
- type Signature
- type Spine
- type SpineItem
- type Store
- type TOCContent
- type Versioner
Examples ¶
Constants ¶
const ( Prop_Mathml int // mathml Prop_RemoteResources // remote-resources Prop_Scripted // scripted Prop_Svg // svg )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Author ¶
type Author struct { Role string `xml:"role,attr,omitempty"` FileAs string `xml:"file-as,attr,omitempty"` Data string `xml:",chardata"` }
Author
type Book ¶
type Book struct { Package Package `xml:"package"` RootFolder string // contains filtered or unexported fields }
Book epub book
func New ¶
func New( target io.WriteCloser, title []string, language []string, identifier []string, creator []Author, publisher []string, date []Date, signature Signature, metatag []Metatag, pageProgression string, bookversion ugarit.Versioner) (*Book, error)
Create a blank EPub Book
Parameters:
@Target -- where to save the epub contents
@Title -- eBook title
@Language -- eBook language
@identifier -- eBook ID
@Creator -- eBook author(s)
@Publisher -- eBook Publisher(s)
@Date -- Date published
@signature -- Ignored for now :(
@metatag -- Any epub metatags go here
@PageProgression -- PageProgression use "ltr" (left-to-right) or "rtl" (right-to-left)
@bookversion -- provide a versioner interface or use the package provided one which just generate a version string using the current time
func (*Book) AddCover ¶
func (b *Book) AddCover(path string, mimetype string, src io.Reader, options interface{}) (string, io.Writer, error)
AddCover saves the Cover in the E-Book. Use it before saving any content to the E-Book. AddCover creates/truncates the file specified by path inside the epub file, registers as having the provided mimetype and makes it be the book cover. If src is nil, it returns a valid io.Writer. If src is not nil, it copies its contents to the file and returns a nil io.Writer. If path collides with a reserved path, it returns an error.
func (*Book) AddFile ¶
func (b *Book) AddFile(path string, mimetype string, src io.Reader, id string, options interface{}) (string, io.Writer, error)
AddFile stores the file in the E-Book. It does not include it in the TOC. AddFile creates/truncates the file specified by path, register as having the provided mimetype. If src is nil, it returns a valid io.Writer. If src is not nil, it copies its contents to the file and return a nil io.Writer. If path collides with a reserved path, it returns an error.
func (*Book) AddPage ¶
func (b *Book) AddPage(path string, mimetype string, src io.Reader, id string, options interface{}) (string, io.Writer, ugarit.TOCRef, error)
AddPage saves the page to the E-Book and adds an entry in the Spine pointing to it. It calls AddFile. So, if you use this method, you don't need to call AddFile to save it, otherwise it will be stored twice in the E-Book file. AddPage creates/truncates the file specified by path inside the epub file, register as having the provided mimetype and making it to figure in the book index. If src is nil, it returns a valid io.Writer. If src is not nil, it copies its content to the file and return a nil io.Writer. If path collides with a reserved path, it returns an error. If the page is to be added to the TOC, provide an EPubOptions object containing a TOCTitle and a TOCItemTitle; OR a TOCContent object. TOC support is still alpha code and will be improved in the future
func (*Book) AddTOC ¶
AddTOC saves the TOC file to the E-Book. Use it just before closing the E-Book. AddTOC creates/truncates the TOC file when the book is closed. If IndexGenerator != nil, the generated html nodes are be passed through it before saved in the file. If id!="" it sets the TOC id.
func (*Book) SubSectionStyle ¶
func (b *Book) SubSectionStyle(sty ugarit.SectionStyle)
SubSectionStyle sets the object to style the top level section of the TOC
type EPubOptions ¶
type Identifier ¶
type Identifier struct { Data string `xml:",chardata"` ID string `xml:"id,attr,omitempty"` Scheme string `xml:"scheme,attr,omitempty"` }
Identifier
type IndexGenerator ¶
type IndexGenerator struct {
// contains filtered or unexported fields
}
func NewIndexGenerator ¶
func NewIndexGenerator() (*IndexGenerator, error)
Creates an index generator object
func (IndexGenerator) AddItem ¶
func (gen IndexGenerator) AddItem(item *html.Node) error
AddItem adds a new TOC entry
func (IndexGenerator) GetDocument ¶
func (gen IndexGenerator) GetDocument() (io.Reader, error)
GetDocument returns the XHTML content of the TOC file
func (IndexGenerator) GetId ¶
func (gen IndexGenerator) GetId() string
GetId returns the ID to use in the TOC OPF entry and the Spine TOC reference.
func (IndexGenerator) GetMimeType ¶
func (gen IndexGenerator) GetMimeType() string
GetMimeType returns the mimetype of the TOC file.
func (IndexGenerator) GetPathName ¶
func (gen IndexGenerator) GetPathName() string
GetPathName returns the relative pathname of the TOC file
func (IndexGenerator) GetPropertyValue ¶
func (gen IndexGenerator) GetPropertyValue() string
GetPropertyValue returns the the value to set in the property attribute of the TOC OPF entry.
type IndexOptions ¶
type IndexOptions struct { IndexGenerator ugarit.IndexGenerator Id string Prop int }
type Manifest ¶
type Manifest struct { ID string `xml:"id,attr,omitempty"` Href string `xml:"href,attr"` MediaType string `xml:"media-type,attr"` // MediaFallback string `xml:"media-fallback,attr"` Properties string `xml:"properties,attr,omitempty"` MediaOverlay string `xml:"media-overlay,attr,omitempty"` }
Manifest manifest
type Metadata ¶
type Metadata struct { Xmlns string `xml:"xmlns:dc,attr"` Title []string `xml:"dc:title"` Language []string `xml:"dc:language"` Identifier []Identifier `xml:"dc:identifier"` Creator []Author `xml:"dc:creator"` Publisher []string `xml:"dc:publisher"` Date []Date `xml:"dc:date"` Signature *Signature `xml:"link,omitempty"` Metatag []Metatag `xml:"meta"` }
Metadata metadata
type Metatag ¶
type Metatag struct { Name string `xml:"name,attr,omitempty"` Langattr string `xml:"xml:lang,attr,omitempty"` Property string `xml:"property,attr,omitempty"` Content string `xml:"content,attr,omitempty"` Data string `xml:",chardata"` }
Metatag
type Package ¶
type Package struct { XMLName struct{} `xml:"package"` Version string `xml:"version,attr"` Langattr string `xml:"xml:lang,attr"` Xmlns string `xml:"xmlns,attr"` XmlnsDc string `xml:"xmlns:dc,attr"` XmlnsDcterms string `xml:"xmlns:dcterms,attr"` Prefix string `xml:"prefix,attr"` UID string `xml:"unique-identifier,attr"` Metadata Metadata `xml:"metadata"` Manifest []Manifest `xml:"manifest>item"` Spine Spine `xml:"spine"` Guide Guide `xml:"guide,omitempty"` }
Package content.opf
type Spine ¶
type Spine struct { ID string `xml:"id,attr,omitempty"` Toc string `xml:"toc,attr,omitempty"` PageProgression string `xml:"page-progression-direction,attr,omitempty"` Itemref []SpineItem `xml:"itemref"` }
Spine spine
type SpineItem ¶
type SpineItem struct { IDref string `xml:"idref,attr"` ID string `xml:"id,attr,omitempty"` Linear string `xml:"linear,attr,omitempty"` Properties string `xml:"properties,attr,omitempty"` }
SpineItem spine item
type TOCContent ¶
type TOCContent struct { Title string // contains filtered or unexported fields }
func (*TOCContent) ContentRef ¶
func (tc *TOCContent) ContentRef() string
ContentRef retrieves the path o the file pointed by the TOC entry
func (*TOCContent) ItemRef ¶
func (tc *TOCContent) ItemRef() string
ItemRef retrieves the reference ID of the TOC Item
func (*TOCContent) ItemTitle ¶
func (tc *TOCContent) ItemTitle() string
ItemTitle retrieves the item title (label) as it appears in the TOC
func (*TOCContent) SubSectionStyle ¶
func (tc *TOCContent) SubSectionStyle(sty ugarit.SectionStyle)
SubSectionStyle sets the object to style this TOC subsection
func (*TOCContent) TOCChild ¶
func (tc *TOCContent) TOCChild(n int) ugarit.TOC
TOCChild retrieves the Nth child of the TOC item
func (*TOCContent) TOCLen ¶
func (tc *TOCContent) TOCLen() int
TOCLen retrieves the item count in this TOC section