move epubimage outside epub

This commit is contained in:
Celogeek 2024-01-04 19:26:41 +01:00
parent db58244946
commit cd34e4b962
Signed by: celogeek
SSH Key Fingerprint: SHA256:DEDfxIK2nUWXbslbRkww3zsauDjhWHlTXar+ak4lDJ4
5 changed files with 35 additions and 35 deletions

View File

@ -15,8 +15,8 @@ import (
"text/template"
"time"
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image"
epubtemplates "github.com/celogeek/go-comic-converter/v2/pkg/epub/templates"
"github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
"github.com/celogeek/go-comic-converter/v2/pkg/epubimageprocessor"
"github.com/celogeek/go-comic-converter/v2/pkg/epuboptions"
"github.com/celogeek/go-comic-converter/v2/pkg/epubprogress"
@ -36,8 +36,8 @@ type ePub struct {
}
type epubPart struct {
Cover *epubimage.Image
Images []*epubimage.Image
Cover *epubimage.EPUBImage
Images []*epubimage.EPUBImage
Reader *zip.ReadCloser
}
@ -71,7 +71,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 *epubimage.Image, zipImg *zip.File) error {
func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.EPUBImage, zipImg *zip.File) error {
err := wz.WriteContent(
img.EPUBPagePath(),
[]byte(e.render(epubtemplates.Text, map[string]any{
@ -89,7 +89,7 @@ func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip
}
// write blank page
func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error {
func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.EPUBImage) error {
return wz.WriteContent(
img.EPUBSpacePath(),
[]byte(e.render(epubtemplates.Blank, map[string]any{
@ -100,7 +100,7 @@ func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error {
}
// write title image
func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part, totalParts int) error {
func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.EPUBImage, part, totalParts int) error {
title := "Cover"
text := ""
if totalParts > 1 {
@ -143,7 +143,7 @@ func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part,
}
// write title image
func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.Image, title string) error {
func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.EPUBImage, title string) error {
titleAlign := ""
if !e.Image.View.PortraitOnly {
if e.Image.Manga {
@ -240,7 +240,7 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.EPUBZipImageRe
baseSize := uint64(128*1024) + imgStorage.Size(cover.EPUBImgPath())*2
currentSize := baseSize
currentImages := make([]*epubimage.Image, 0)
currentImages := make([]*epubimage.EPUBImage, 0)
part := 1
for _, img := range images {
@ -252,7 +252,7 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.EPUBZipImageRe
})
part += 1
currentSize = baseSize
currentImages = make([]*epubimage.Image, 0)
currentImages = make([]*epubimage.EPUBImage, 0)
}
currentSize += imgSize
currentImages = append(currentImages, img)
@ -270,7 +270,7 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.EPUBZipImageRe
// create a tree from the directories.
//
// this is used to simulate the toc.
func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string {
func (e *ePub) getTree(images []*epubimage.EPUBImage, skip_files bool) string {
t := epubtree.New()
for _, img := range images {
if skip_files {
@ -350,7 +350,7 @@ func (e *ePub) Write() error {
fmt.Fprintf(os.Stderr, "TOC:\n - %s\n%s\n", e.Title, e.getTree(p.Images, true))
if e.DryVerbose {
if e.Image.HasCover {
fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*epubimage.Image{p.Cover}, false))
fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*epubimage.EPUBImage{p.Cover}, false))
}
fmt.Fprintf(os.Stderr, "Files:\n%s\n", e.getTree(p.Images, false))
}

View File

@ -4,7 +4,7 @@ import (
"fmt"
"github.com/beevik/etree"
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image"
"github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
"github.com/celogeek/go-comic-converter/v2/pkg/epuboptions"
)
@ -16,8 +16,8 @@ type ContentOptions struct {
Publisher string
UpdatedAt string
ImageOptions *epuboptions.Image
Cover *epubimage.Image
Images []*epubimage.Image
Cover *epubimage.EPUBImage
Images []*epubimage.EPUBImage
Current int
Total int
}
@ -140,7 +140,7 @@ func getMeta(o *ContentOptions) []tag {
func getManifest(o *ContentOptions) []tag {
var imageTags, pageTags, spaceTags []tag
addTag := func(img *epubimage.Image, withSpace bool) {
addTag := func(img *epubimage.EPUBImage, withSpace bool) {
imageTags = append(imageTags,
tag{"item", tagAttrs{"id": img.ImgKey(), "href": img.ImgPath(), "media-type": fmt.Sprintf("image/%s", o.ImageOptions.Format)}, ""},
)

View File

@ -5,11 +5,11 @@ import (
"strings"
"github.com/beevik/etree"
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image"
"github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
)
// create toc
func Toc(title string, hasTitle bool, stripFirstDirectoryFromToc bool, images []*epubimage.Image) string {
func Toc(title string, hasTitle bool, stripFirstDirectoryFromToc bool, images []*epubimage.EPUBImage) string {
doc := etree.NewDocument()
doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
doc.CreateDirective("DOCTYPE html")

View File

@ -9,7 +9,7 @@ import (
"strings"
)
type Image struct {
type EPUBImage struct {
Id int
Part int
Raw image.Image
@ -27,47 +27,47 @@ type Image struct {
}
// key name of the blank plage after the image
func (i *Image) SpaceKey() string {
func (i *EPUBImage) SpaceKey() string {
return fmt.Sprintf("space_%d", i.Id)
}
// path of the blank page
func (i *Image) SpacePath() string {
func (i *EPUBImage) SpacePath() string {
return fmt.Sprintf("Text/%s.xhtml", i.SpaceKey())
}
// path of the blank page into the EPUB
func (i *Image) EPUBSpacePath() string {
func (i *EPUBImage) EPUBSpacePath() string {
return fmt.Sprintf("OEBPS/%s", i.SpacePath())
}
// key for page
func (i *Image) PageKey() string {
func (i *EPUBImage) PageKey() string {
return fmt.Sprintf("page_%d_p%d", i.Id, i.Part)
}
// page path linked to the image
func (i *Image) PagePath() string {
func (i *EPUBImage) PagePath() string {
return fmt.Sprintf("Text/%s.xhtml", i.PageKey())
}
// page path into the EPUB
func (i *Image) EPUBPagePath() string {
func (i *EPUBImage) EPUBPagePath() string {
return fmt.Sprintf("OEBPS/%s", i.PagePath())
}
// key for image
func (i *Image) ImgKey() string {
func (i *EPUBImage) ImgKey() string {
return fmt.Sprintf("img_%d_p%d", i.Id, i.Part)
}
// image path
func (i *Image) ImgPath() string {
func (i *EPUBImage) ImgPath() string {
return fmt.Sprintf("Images/%s.%s", i.ImgKey(), i.Format)
}
// image path into the EPUB
func (i *Image) EPUBImgPath() string {
func (i *EPUBImage) EPUBImgPath() string {
return fmt.Sprintf("OEBPS/%s", i.ImgPath())
}
@ -75,7 +75,7 @@ func (i *Image) EPUBImgPath() string {
//
// center by default.
// align to left or right if it's part of the splitted double page.
func (i *Image) ImgStyle(viewWidth, viewHeight int, align string) string {
func (i *EPUBImage) ImgStyle(viewWidth, viewHeight int, align string) string {
relWidth, relHeight := i.RelSize(viewWidth, viewHeight)
marginW, marginH := float64(viewWidth-relWidth)/2, float64(viewHeight-relHeight)/2
@ -100,7 +100,7 @@ func (i *Image) ImgStyle(viewWidth, viewHeight int, align string) string {
return strings.Join(style, "; ")
}
func (i *Image) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) {
func (i *EPUBImage) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) {
w, h := viewWidth, viewHeight
srcw, srch := i.Width, i.Height

View File

@ -10,8 +10,8 @@ import (
"os"
"sync"
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image"
epubimagefilters "github.com/celogeek/go-comic-converter/v2/pkg/epub/imagefilters"
"github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
"github.com/celogeek/go-comic-converter/v2/pkg/epuboptions"
"github.com/celogeek/go-comic-converter/v2/pkg/epubprogress"
"github.com/celogeek/go-comic-converter/v2/pkg/epubzip"
@ -27,8 +27,8 @@ func New(o *epuboptions.EPUBOptions) *EPUBImageProcessor {
}
// extract and convert images
func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
images = make([]*epubimage.Image, 0)
func (e *EPUBImageProcessor) Load() (images []*epubimage.EPUBImage, err error) {
images = make([]*epubimage.EPUBImage, 0)
imageCount, imageInput, err := e.load()
if err != nil {
return nil, err
@ -37,7 +37,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
// dry run, skip convertion
if e.Dry {
for img := range imageInput {
images = append(images, &epubimage.Image{
images = append(images, &epubimage.EPUBImage{
Id: img.Id,
Path: img.Path,
Name: img.Name,
@ -48,7 +48,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
return images, nil
}
imageOutput := make(chan *epubimage.Image)
imageOutput := make(chan *epubimage.EPUBImage)
// processing
bar := epubprogress.New(epubprogress.Options{
@ -85,7 +85,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
raw = dst
}
img := &epubimage.Image{
img := &epubimage.EPUBImage{
Id: input.Id,
Part: part,
Raw: raw,