diff --git a/internal/epub/epub.go b/internal/epub/epub.go index 431017c..23d7690 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -70,7 +70,7 @@ func (e *ePub) render(templateString string, data map[string]any) string { // write image to the zip func (e *ePub) writeImage(wz *epubzip.EpubZip, img *epubimageprocessor.LoadedImage) error { err := wz.WriteContent( - fmt.Sprintf("OEBPS/%s", img.Image.TextPath()), + fmt.Sprintf("OEBPS/%s", img.Image.PagePath()), []byte(e.render(epubtemplates.Text, map[string]any{ "Title": fmt.Sprintf("Image %d Part %d", img.Image.Id, img.Image.Part), "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height), @@ -289,14 +289,14 @@ func (e *ePub) Write() error { } } - for i, img := range part.LoadedImages { + lastImage := part.LoadedImages[len(part.LoadedImages)-1] + for _, img := range part.LoadedImages { if err := e.writeImage(wz, img); err != nil { return err } // Double Page or Last Image that is not a double page - if img.Image.DoublePage || - (img.Image.Part == 0 && i+1 == len(part.LoadedImages)) { + if img.Image.DoublePage || (img.Image.Part == 0 && img == lastImage) { if err := e.writeBlank(wz, img.Image); err != nil { return err } diff --git a/internal/epub/image/epub_image.go b/internal/epub/image/epub_image.go index 8770a42..74b4b41 100644 --- a/internal/epub/image/epub_image.go +++ b/internal/epub/image/epub_image.go @@ -20,29 +20,34 @@ type Image struct { Name string } -// key name of the image -func (i *Image) Key(prefix string) string { - return fmt.Sprintf("%s_%d_p%d", prefix, i.Id, i.Part) -} - // key name of the blank plage after the image -func (i *Image) SpaceKey(prefix string) string { - return fmt.Sprintf("%s_%d_sp", prefix, i.Id) +func (i *Image) SpaceKey() string { + return fmt.Sprintf("space_%d", i.Id) } // path of the blank page func (i *Image) SpacePath() string { - return fmt.Sprintf("Text/%d_sp.xhtml", i.Id) + return fmt.Sprintf("Text/%s.xhtml", i.SpaceKey()) } -// text path linked to the image -func (i *Image) TextPath() string { - return fmt.Sprintf("Text/%d_p%d.xhtml", i.Id, i.Part) +// key for page +func (i *Image) PageKey() string { + return fmt.Sprintf("page_%d_p%d", i.Id, i.Part) +} + +// page path linked to the image +func (i *Image) PagePath() string { + return fmt.Sprintf("Text/%s.xhtml", i.PageKey()) +} + +// key for image +func (i *Image) ImgKey() string { + return fmt.Sprintf("img_%d_p%d", i.Id, i.Part) } // image path func (i *Image) ImgPath() string { - return fmt.Sprintf("Images/%d_p%d.jpg", i.Id, i.Part) + return fmt.Sprintf("Images/%s.jpg", i.ImgKey()) } // style to apply to the image. diff --git a/internal/epub/templates/epub_templates_content.go b/internal/epub/templates/epub_templates_content.go index 329ffad..63f6bc1 100644 --- a/internal/epub/templates/epub_templates_content.go +++ b/internal/epub/templates/epub_templates_content.go @@ -109,7 +109,7 @@ func getMeta(o *ContentOptions) []tag { } if o.Cover != nil { - metas = append(metas, tag{"meta", tagAttrs{"name": "cover", "content": o.Cover.Key("img")}, ""}) + metas = append(metas, tag{"meta", tagAttrs{"name": "cover", "content": o.Cover.ImgKey()}, ""}) } if o.Total > 1 { @@ -124,15 +124,21 @@ func getMeta(o *ContentOptions) []tag { } func getManifest(o *ContentOptions) []tag { - itag := func(img *epubimage.Image) tag { - return tag{"item", tagAttrs{"id": img.Key("img"), "href": img.ImgPath(), "media-type": "image/jpeg"}, ""} - } - htag := func(img *epubimage.Image) tag { - return tag{"item", tagAttrs{"id": img.Key("page"), "href": img.TextPath(), "media-type": "application/xhtml+xml"}, ""} - } - stag := func(img *epubimage.Image) tag { - return tag{"item", tagAttrs{"id": img.SpaceKey("page"), "href": img.SpacePath(), "media-type": "application/xhtml+xml"}, ""} + var imageTags, pageTags, spaceTags []tag + addTag := func(img *epubimage.Image, withSpace bool) { + imageTags = append(imageTags, + tag{"item", tagAttrs{"id": img.ImgKey(), "href": img.ImgPath(), "media-type": "image/jpeg"}, ""}, + ) + pageTags = append(pageTags, + tag{"item", tagAttrs{"id": img.PageKey(), "href": img.PagePath(), "media-type": "application/xhtml+xml"}, ""}, + ) + if withSpace { + spaceTags = append(spaceTags, + tag{"item", tagAttrs{"id": img.SpaceKey(), "href": img.SpacePath(), "media-type": "application/xhtml+xml"}, ""}, + ) + } } + items := []tag{ {"item", tagAttrs{"id": "toc", "href": "toc.xhtml", "properties": "nav", "media-type": "application/xhtml+xml"}, ""}, {"item", tagAttrs{"id": "css", "href": "Text/style.css", "media-type": "text/css"}, ""}, @@ -141,20 +147,18 @@ func getManifest(o *ContentOptions) []tag { } if o.ImageOptions.HasCover || o.Current > 1 { - items = append(items, itag(o.Cover), htag(o.Cover)) + addTag(o.Cover, false) } - for _, img := range o.Images { - if img.Part == 1 { - items = append(items, stag(img)) - } - items = append(items, itag(img), htag(img)) - } lastImage := o.Images[len(o.Images)-1] - if lastImage.Part == 0 { - items = append(items, stag(lastImage)) + for _, img := range o.Images { + addTag(img, img.DoublePage || (img.Part == 0 && img == lastImage)) } + items = append(items, imageTags...) + items = append(items, pageTags...) + items = append(items, spaceTags...) + return items } @@ -179,23 +183,23 @@ func getSpine(o *ContentOptions) []tag { {"itemref", tagAttrs{"idref": "page_title", "properties": getSpread(true)}, ""}, } for _, img := range o.Images { - spine = append(spine, tag{ - "itemref", - tagAttrs{"idref": img.Key("page"), "properties": getSpread(img.DoublePage && o.ImageOptions.NoBlankPage)}, - "", - }) - if img.DoublePage && isOnTheRight && !o.ImageOptions.NoBlankPage { + if img.DoublePage && isOnTheRight { spine = append(spine, tag{ "itemref", - tagAttrs{"idref": img.SpaceKey("page"), "properties": getSpread(false)}, + tagAttrs{"idref": img.SpaceKey(), "properties": getSpread(false) + " layout-blank"}, "", }) } + spine = append(spine, tag{ + "itemref", + tagAttrs{"idref": img.PageKey(), "properties": getSpread(img.DoublePage)}, + "", + }) } if o.ImageOptions.Manga == isOnTheRight { spine = append(spine, tag{ "itemref", - tagAttrs{"idref": o.Images[len(o.Images)-1].SpaceKey("page"), "properties": getSpread(false)}, + tagAttrs{"idref": o.Images[len(o.Images)-1].SpaceKey(), "properties": getSpread(false)}, "", }) } @@ -207,8 +211,8 @@ func getSpine(o *ContentOptions) []tag { func getGuide(o *ContentOptions) []tag { guide := []tag{} if o.Cover != nil { - guide = append(guide, tag{"reference", tagAttrs{"type": "cover", "title": "cover", "href": o.Cover.TextPath()}, ""}) + guide = append(guide, tag{"reference", tagAttrs{"type": "cover", "title": "cover", "href": o.Cover.PagePath()}, ""}) } - guide = append(guide, tag{"reference", tagAttrs{"type": "text", "title": "content", "href": o.Images[0].TextPath()}, ""}) + guide = append(guide, tag{"reference", tagAttrs{"type": "text", "title": "content", "href": o.Images[0].PagePath()}, ""}) return guide } diff --git a/internal/epub/templates/epub_templates_toc.go b/internal/epub/templates/epub_templates_toc.go index be3d612..d7ea700 100644 --- a/internal/epub/templates/epub_templates_toc.go +++ b/internal/epub/templates/epub_templates_toc.go @@ -37,7 +37,7 @@ func Toc(title string, stripFirstDirectoryFromToc bool, images []*epubimage.Imag } t := paths[parentPath].CreateElement("li") link := t.CreateElement("a") - link.CreateAttr("href", img.TextPath()) + link.CreateAttr("href", img.PagePath()) link.CreateText(path) paths[currentPath] = t.CreateElement("ol") }