Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Make API for setting the cover more intuitive
Browse files Browse the repository at this point in the history
  • Loading branch information
bmaupin committed Jun 6, 2016
1 parent 7696ec6 commit db28736
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 34 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

**Features:**

- [Clean, documented API](https://godoc.org/github.com/bmaupin/go-epub)
- [Documented API](https://godoc.org/github.com/bmaupin/go-epub)
- Creates valid EPUB 3.0 files
- Adds an additional EPUB 2.0 table of contents ([as seen here](https://github.com/bmaupin/epub-samples)) for maximum compatibility

**Wishlist:**

- Add functionality to read EPUB files
68 changes: 48 additions & 20 deletions epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ img {
height: 100%;
max-width: 100%;
}`
defaultCoverFilename = "cover.xhtml"
defaultEpubLang = "en"
imageFileFormat = "image%04d%s"
sectionFileFormat = "section%04d.xhtml"
urnUUIDPrefix = "urn:uuid:"
defaultCoverCSSFilename = "cover.css"
defaultCoverImgFormat = "cover%s"
defaultCoverXhtmlFilename = "cover.xhtml"
defaultEpubLang = "en"
imageFileFormat = "image%04d%s"
sectionFileFormat = "section%04d.xhtml"
urnUUIDPrefix = "urn:uuid:"
)

// Epub implements an EPUB file.
Expand All @@ -80,8 +82,9 @@ type Epub struct {
}

type epubCover struct {
xhtmlFilename string
cssFilename string
imageFilename string
xhtmlFilename string
}

type epubSection struct {
Expand Down Expand Up @@ -228,38 +231,66 @@ func (e *Epub) SetAuthor(author string) {
e.pkg.setAuthor(author)
}

// SetCover sets the cover page for the EPUB using the provided path to image
// file and optional path to CSS file.
// SetCover sets the cover page for the EPUB using the provided image source and
// optional CSS.
//
// The image source should either be a URL or a path to a local file; in either
// case, the image will be retrieved and stored in the EPUB.
//
// If the path to the CSS file isn't provided, a default cover CSS file will be
// created and used.
func (e *Epub) SetCover(imagePath string, cssPath string) {
// The CSS content is the exact content that will be stored in the CSS file. It
// will not be validated. If the CSS content isn't provided, default content
// will be used.
func (e *Epub) SetCover(imageSource string, cssFileContent string) {
var err error

// If a cover already exists, remove it from the EPUB sections
// If a cover already exists
if e.cover.xhtmlFilename != "" {
// Remove the xhtml file
for i, section := range e.sections {
if section.filename == e.cover.xhtmlFilename {
e.sections = append(e.sections[:i], e.sections[i+1:]...)
break
}
}

// Remove the image
delete(e.images, e.cover.imageFilename)

// Remove the CSS
delete(e.css, e.cover.cssFilename)
}

defaultImageFilename := fmt.Sprintf(defaultCoverImgFormat, filepath.Ext(imageSource))
imagePath, err := e.AddImage(imageSource, defaultImageFilename)
// If that doesn't work, generate a filename
if err != nil {
imagePath, err = e.AddImage(imageSource, "")
if err != nil {
// This shouldn't cause an error since we're not specifying a filename
panic(fmt.Sprintf("Error adding default cover image file: %s", err))
}
}
e.cover.imageFilename = filepath.Base(imagePath)

// Create a default cover stylesheet if one isn't provided
if cssPath == "" {
cssPath, err = e.AddCSS(defaultCoverCSSContent, "")
// Use default cover stylesheet if one isn't provided
if cssFileContent == "" {
cssFileContent = defaultCoverCSSContent
}
cssPath, err := e.AddCSS(cssFileContent, defaultCoverCSSFilename)
// If that doesn't work, generate a filename
if err != nil {
cssPath, err = e.AddCSS(cssFileContent, "")
if err != nil {
// This shouldn't cause an error since we're not specifying a filename
panic(fmt.Sprintf("Error adding default cover CSS file: %s", err))
}
}
e.cover.cssFilename = filepath.Base(cssPath)

coverBody := fmt.Sprintf(defaultCoverBody, imagePath)

// Title won't be used since the cover won't be added to the TOC
// First try to use the default cover filename
coverPath, err := e.AddSection("", coverBody, defaultCoverFilename, cssPath)
coverPath, err := e.AddSection("", coverBody, defaultCoverXhtmlFilename, cssPath)
// If that doesn't work, generate a filename
if err != nil {
coverPath, err = e.AddSection("", coverBody, "", cssPath)
Expand All @@ -268,10 +299,7 @@ func (e *Epub) SetCover(imagePath string, cssPath string) {
panic(fmt.Sprintf("Error adding default cover XHTML file: %s", err))
}
}

// Set the cover field to the base filename
e.cover.xhtmlFilename = filepath.Base(coverPath)
e.cover.imageFilename = filepath.Base(imagePath)
}

// SetLang sets the language of the EPUB.
Expand Down
16 changes: 7 additions & 9 deletions epub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ img {
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>%s</title>
<link rel="stylesheet" type="text/css" href="%s"></link>
<link rel="stylesheet" type="text/css" href="../css/cover.css"></link>
</head>
<body>
<img src="%s" alt="Cover Image" />
<img src="../img/cover.png" alt="Cover Image" />
</body>
</html>`
testCSSLinkTemplate = `<link rel="stylesheet" type="text/css" href="%s"></link>`
Expand Down Expand Up @@ -474,18 +474,16 @@ func TestEpubUUID(t *testing.T) {

func TestSetCover(t *testing.T) {
e := NewEpub(testEpubTitle)
testImagePath, _ := e.AddImage(testImageFromFileSource, testImageFromFileFilename)
testCSSPath, _ := e.AddCSS(testCoverCSSContent, testCoverCSSFilename)
e.SetCover(testImagePath, testCSSPath)
e.SetCover(testImageFromFileSource, testCoverCSSContent)

tempDir := writeAndExtractEpub(t, e, testEpubFilename)

contents, err := ioutil.ReadFile(filepath.Join(tempDir, contentFolderName, xhtmlFolderName, defaultCoverFilename))
contents, err := ioutil.ReadFile(filepath.Join(tempDir, contentFolderName, xhtmlFolderName, defaultCoverXhtmlFilename))
if err != nil {
t.Errorf("Unexpected error reading cover XHTML file: %s", err)
}

testCoverContents := fmt.Sprintf(testCoverContentTemplate, testEpubTitle, testCSSPath, testImagePath)
testCoverContents := fmt.Sprintf(testCoverContentTemplate, testEpubTitle)
if trimAllSpace(string(contents)) != trimAllSpace(testCoverContents) {
t.Errorf(
"Cover file contents don't match\n"+
Expand All @@ -503,12 +501,12 @@ func TestEpubValidity(t *testing.T) {
testCSSPath, _ := e.AddCSS(testCoverCSSContent, testCoverCSSFilename)
e.AddCSS(testCoverCSSContent, "")
e.AddSection(testSectionTitle, testSectionBody, testSectionFilename, testCSSPath)
testImagePath, _ := e.AddImage(testImageFromFileSource, testImageFromFileFilename)
e.AddImage(testImageFromFileSource, testImageFromFileFilename)
e.AddImage(testImageFromURLSource, "")
e.AddSection(testSectionTitle, testSectionBody, testSectionFilename, "")
e.AddSection(testSectionTitle, testSectionBody, "", "")
e.SetAuthor(testEpubAuthor)
e.SetCover(testImagePath, testCSSPath)
e.SetCover(testImageFromFileSource, testCoverCSSContent)
e.SetLang(testEpubLang)
e.SetTitle(testEpubAuthor)
e.SetUUID(testEpubUUID)
Expand Down

0 comments on commit db28736

Please sign in to comment.