Documentation ¶
Overview ¶
Package epub generates valid EPUB 3.0 files with additional EPUB 2.0 table of contents (as seen here: https://github.com/bmaupin/epub-samples) for maximum compatibility.
Basic usage:
// Create a new EPUB e := epub.NewEpub("My title") // Set the author e.SetAuthor("Hingle McCringleberry") // Add a section section1Body := `<h1>Section 1</h1> <p>This is a paragraph.</p>` e.AddSection(section1Body, "Section 1", "", "") // Write the EPUB err = e.Write("My EPUB.epub") if err != nil { // handle error }
Index ¶
- Constants
- func Use(s FSType)
- type Epub
- func (e *Epub) AddAudio(source string, audioFilename string) (string, error)
- func (e *Epub) AddCSS(source string, internalFilename string) (string, error)
- func (e *Epub) AddFont(source string, internalFilename string) (string, error)
- func (e *Epub) AddImage(source string, imageFilename string) (string, error)
- func (e *Epub) AddSection(body string, sectionTitle string, internalFilename string, ...) (string, error)
- func (e *Epub) AddSubSection(parentFilename string, body string, sectionTitle string, ...) (string, error)
- func (e *Epub) AddVideo(source string, videoFilename string) (string, error)
- func (e *Epub) Author() string
- func (e *Epub) Description() string
- func (e *Epub) Identifier() string
- func (e *Epub) Lang() string
- func (e *Epub) Ppd() string
- func (e *Epub) SetAuthor(author string)
- func (e *Epub) SetCover(internalImagePath string, internalCSSPath string)
- func (e *Epub) SetDescription(desc string)
- func (e *Epub) SetIdentifier(identifier string)
- func (e *Epub) SetLang(lang string)
- func (e *Epub) SetPpd(direction string)
- func (e *Epub) SetTitle(title string)
- func (e *Epub) Title() string
- func (e *Epub) Write(destFilePath string) error
- func (e *Epub) WriteTo(dst io.Writer) (int64, error)
- type FSType
- type FileRetrievalError
- type FilenameAlreadyUsedError
- type ParentDoesNotExistError
- type UnableToCreateEpubError
Examples ¶
Constants ¶
const ( CSSFolderName = "css" FontFolderName = "fonts" ImageFolderName = "images" VideoFolderName = "videos" AudioFolderName = "audios" )
Folder names used for resources inside the EPUB
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Epub ¶
Epub implements an EPUB file.
func (*Epub) AddAudio ¶ added in v1.1.0
AddAudio adds an audio to the EPUB and returns a relative path to the audio file that can be used in EPUB sections in the format: ../AudioFolderName/internalFilename
The audio source should either be a URL, a path to a local file, or an embedded data URL; in any case, the audio file will be retrieved and stored in the EPUB.
The internal filename will be used when storing the audio file in the EPUB and must be unique among all audio files. If the same filename is used more than once, FilenameAlreadyUsedError will be returned. The internal filename is optional; if no filename is provided, one will be generated.
func (*Epub) AddCSS ¶
AddCSS adds a CSS file to the EPUB and returns a relative path to the CSS file that can be used in EPUB sections in the format: ../CSSFolderName/internalFilename
The CSS source should either be a URL, a path to a local file, or an embedded data URL; in any case, the CSS file will be retrieved and stored in the EPUB.
The internal filename will be used when storing the CSS file in the EPUB and must be unique among all CSS files. If the same filename is used more than once, FilenameAlreadyUsedError will be returned. The internal filename is optional; if no filename is provided, one will be generated.
Example ¶
e := epub.NewEpub("My title") // Add CSS css1Path, err := e.AddCSS("testdata/cover.css", "epub.css") if err != nil { log.Fatal(err) } // The filename is optional css2Path, err := e.AddCSS("testdata/cover.css", "") if err != nil { log.Fatal(err) } // Use the CSS in a section sectionBody := ` <h1>Section 1</h1> <p>This is a paragraph.</p>` e.AddSection(sectionBody, "Section 1", "", css1Path) fmt.Println(css1Path) fmt.Println(css2Path)
Output: ../css/epub.css ../css/cover.css
func (*Epub) AddFont ¶
AddFont adds a font file to the EPUB and returns a relative path to the font file that can be used in EPUB sections in the format: ../FontFolderName/internalFilename
The font source should either be a URL, a path to a local file, or an embedded data URL; in any case, the font file will be retrieved and stored in the EPUB.
The internal filename will be used when storing the font file in the EPUB and must be unique among all font files. If the same filename is used more than once, FilenameAlreadyUsedError will be returned. The internal filename is optional; if no filename is provided, one will be generated.
Example ¶
e := epub.NewEpub("My title") // Add a font from a local file font1Path, err := e.AddFont("testdata/redacted-script-regular.ttf", "font.ttf") if err != nil { log.Fatal(err) } // The filename is optional font2Path, err := e.AddFont("testdata/redacted-script-regular.ttf", "") if err != nil { log.Fatal(err) } fmt.Println(font1Path) fmt.Println(font2Path)
Output: ../fonts/font.ttf ../fonts/redacted-script-regular.ttf
func (*Epub) AddImage ¶
AddImage adds an image to the EPUB and returns a relative path to the image file that can be used in EPUB sections in the format: ../ImageFolderName/internalFilename
The image source should either be a URL, a path to a local file, or an embedded data URL; in any case, the image file will be retrieved and stored in the EPUB.
The internal filename will be used when storing the image file in the EPUB and must be unique among all image files. If the same filename is used more than once, FilenameAlreadyUsedError will be returned. The internal filename is optional; if no filename is provided, one will be generated.
Example ¶
e := epub.NewEpub("My title") // Add an image from a local file img1Path, err := e.AddImage("testdata/gophercolor16x16.png", "go-gopher.png") if err != nil { log.Fatal(err) } // Add an image from a URL. The filename is optional img2Path, err := e.AddImage("https://golang.org/doc/gopher/gophercolor16x16.png", "") if err != nil { log.Fatal(err) } fmt.Println(img1Path) fmt.Println(img2Path)
Output: ../images/go-gopher.png ../images/gophercolor16x16.png
func (*Epub) AddSection ¶
func (e *Epub) AddSection(body string, sectionTitle string, internalFilename string, internalCSSPath string) (string, error)
AddSection adds a new section (chapter, etc) to the EPUB and returns a relative path to the section that can be used from another section (for links).
The body must be valid XHTML that will go between the <body> tags of the section XHTML file. The content will not be validated.
The title will be used for the table of contents. The section will be shown in the table of contents in the same order it was added to the EPUB. The title is optional; if no title is provided, the section will not be added to the table of contents.
The internal filename will be used when storing the section file in the EPUB and must be unique among all section files. If the same filename is used more than once, FilenameAlreadyUsedError will be returned. The internal filename is optional; if no filename is provided, one will be generated.
The internal path to an already-added CSS file (as returned by AddCSS) to be used for the section is optional.
Example ¶
e := epub.NewEpub("My title") // Add a section. The CSS path is optional section1Body := ` <h1>Section 1</h1> <p>This is a paragraph.</p>` section1Path, err := e.AddSection(section1Body, "Section 1", "firstsection.xhtml", "") if err != nil { log.Fatal(err) } // Link to the first section section2Body := fmt.Sprintf(` <h1>Section 2</h1> <a href="%s">Link to section 1</a>`, section1Path) // The title and filename are also optional section2Path, err := e.AddSection(section2Body, "", "", "") if err != nil { log.Fatal(err) } fmt.Println(section1Path) fmt.Println(section2Path)
Output: firstsection.xhtml section0001.xhtml
func (*Epub) AddSubSection ¶ added in v1.0.0
func (e *Epub) AddSubSection(parentFilename string, body string, sectionTitle string, internalFilename string, internalCSSPath string) (string, error)
AddSubSection adds a nested section (chapter, etc) to an existing section. The method returns a relative path to the section that can be used from another section (for links).
The parent filename must be a valid filename from another section already added.
The body must be valid XHTML that will go between the <body> tags of the section XHTML file. The content will not be validated.
The title will be used for the table of contents. The section will be shown as a nested entry of the parent section in the table of contents. The title is optional; if no title is provided, the section will not be added to the table of contents.
The internal filename will be used when storing the section file in the EPUB and must be unique among all section files. If the same filename is used more than once, FilenameAlreadyUsedError will be returned. The internal filename is optional; if no filename is provided, one will be generated.
The internal path to an already-added CSS file (as returned by AddCSS) to be used for the section is optional.
Example ¶
e := epub.NewEpub("My title") // Add a section. The CSS path is optional section1Body := ` <h1>Section 1</h1> <p>This is a paragraph.</p>` section1Path, err := e.AddSection(section1Body, "Section 1", "firstsection.xhtml", "") if err != nil { log.Fatal(err) } // Link to the first section section2Body := fmt.Sprintf(` <h1>Section 2</h1> <a href="%s">Link to section 1</a>`, section1Path) // The title and filename are also optional section2Path, err := e.AddSubSection(section1Path, section2Body, "", "", "") if err != nil { log.Fatal(err) } fmt.Println(section1Path) fmt.Println(section2Path)
Output: firstsection.xhtml section0001.xhtml
func (*Epub) AddVideo ¶ added in v0.11.0
AddVideo adds an video to the EPUB and returns a relative path to the video file that can be used in EPUB sections in the format: ../VideoFolderName/internalFilename
The video source should either be a URL, a path to a local file, or an embedded data URL; in any case, the video file will be retrieved and stored in the EPUB.
The internal filename will be used when storing the video file in the EPUB and must be unique among all video files. If the same filename is used more than once, FilenameAlreadyUsedError will be returned. The internal filename is optional; if no filename is provided, one will be generated.
func (*Epub) Description ¶ added in v0.5.1
Description returns the description of the EPUB.
func (*Epub) Identifier ¶ added in v0.2.0
Identifier returns the unique identifier of the EPUB.
func (*Epub) SetCover ¶
SetCover sets the cover page for the EPUB using the provided image source and optional CSS.
The internal path to an already-added image file (as returned by AddImage) is required.
The internal path to an already-added CSS file (as returned by AddCSS) to be used for the cover is optional. If the CSS path isn't provided, default CSS will be used.
Example ¶
e := epub.NewEpub("My title") // Set the cover. The CSS file is optional coverImagePath, _ := e.AddImage("testdata/gophercolor16x16.png", "cover.png") e.SetCover(coverImagePath, "") // Update the cover using custom CSS coverCSSPath, _ := e.AddCSS("testdata/cover.css", "") e.SetCover(coverImagePath, coverCSSPath)
Output:
func (*Epub) SetDescription ¶ added in v0.5.1
SetDescription sets the description of the EPUB.
func (*Epub) SetIdentifier ¶ added in v0.2.0
SetIdentifier sets the unique identifier of the EPUB, such as a UUID, DOI, ISBN or ISSN. If no identifier is set, a UUID will be automatically generated.
Example ¶
e := epub.NewEpub("My title") // Set the identifier to a UUID e.SetIdentifier("urn:uuid:a1b0d67e-2e81-4df5-9e67-a64cbe366809") // Set the identifier to an ISBN e.SetIdentifier("urn:isbn:9780101010101")
Output:
type FileRetrievalError ¶ added in v0.4.0
type FileRetrievalError struct { Source string // The source of the file whose retrieval failed Err error // The underlying error that was thrown }
FileRetrievalError is thrown by AddCSS, AddFont, AddImage, or Write if there was a problem retrieving the source file that was provided.
func (*FileRetrievalError) Error ¶ added in v0.4.0
func (e *FileRetrievalError) Error() string
type FilenameAlreadyUsedError ¶ added in v0.4.0
type FilenameAlreadyUsedError struct {
Filename string // Filename that caused the error
}
FilenameAlreadyUsedError is thrown by AddCSS, AddFont, AddImage, or AddSection if the same filename is used more than once.
func (*FilenameAlreadyUsedError) Error ¶ added in v0.4.0
func (e *FilenameAlreadyUsedError) Error() string
type ParentDoesNotExistError ¶ added in v1.0.0
type ParentDoesNotExistError struct {
Filename string // Filename that caused the error
}
ParentDoesNotExistError is thrown by AddSubSection if the parent with the previously defined internal filename does not exist.
func (*ParentDoesNotExistError) Error ¶ added in v1.0.0
func (e *ParentDoesNotExistError) Error() string
type UnableToCreateEpubError ¶ added in v0.4.0
type UnableToCreateEpubError struct { Path string // The path that was given to Write to create the EPUB Err error // The underlying error that was thrown }
UnableToCreateEpubError is thrown by Write if it cannot create the destination EPUB file
func (*UnableToCreateEpubError) Error ¶ added in v0.4.0
func (e *UnableToCreateEpubError) Error() string