26 expose the internal epub and epub options package (#44)

* make epub and epub options public

* export only interface
This commit is contained in:
Celogeek 2025-01-05 16:57:23 +01:00 committed by GitHub
parent b1a398e155
commit c34a255768
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 22 additions and 18 deletions

View File

@ -9,8 +9,8 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epuboptions"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/utils" "github.com/celogeek/go-comic-converter/v3/internal/pkg/utils"
"github.com/celogeek/go-comic-converter/v3/pkg/epuboptions"
) )
type Options struct { type Options struct {

View File

@ -11,10 +11,10 @@ import (
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimage" "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimage"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimagefilters" "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimagefilters"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epuboptions"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epubprogress" "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubprogress"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epubzip" "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubzip"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/utils" "github.com/celogeek/go-comic-converter/v3/internal/pkg/utils"
"github.com/celogeek/go-comic-converter/v3/pkg/epuboptions"
) )
type EPUBImageProcessor struct { type EPUBImageProcessor struct {

View File

@ -4,8 +4,8 @@ import (
"github.com/beevik/etree" "github.com/beevik/etree"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimage" "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimage"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epuboptions"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/utils" "github.com/celogeek/go-comic-converter/v3/internal/pkg/utils"
"github.com/celogeek/go-comic-converter/v3/pkg/epuboptions"
) )
type Content struct { type Content struct {

View File

@ -15,8 +15,8 @@ import (
"github.com/tcnksm/go-latest" "github.com/tcnksm/go-latest"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/converter" "github.com/celogeek/go-comic-converter/v3/internal/pkg/converter"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epub"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/utils" "github.com/celogeek/go-comic-converter/v3/internal/pkg/utils"
"github.com/celogeek/go-comic-converter/v3/pkg/epub"
) )
func main() { func main() {

View File

@ -16,15 +16,19 @@ import (
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimage" "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimage"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimageprocessor" "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimageprocessor"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epuboptions"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epubprogress" "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubprogress"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epubtemplates" "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubtemplates"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epubtree" "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubtree"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/epubzip" "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubzip"
"github.com/celogeek/go-comic-converter/v3/internal/pkg/utils" "github.com/celogeek/go-comic-converter/v3/internal/pkg/utils"
"github.com/celogeek/go-comic-converter/v3/pkg/epuboptions"
) )
type EPUB struct { type EPUB interface {
Write() error
}
type epub struct {
epuboptions.EPUBOptions epuboptions.EPUBOptions
UID string UID string
Publisher string Publisher string
@ -48,7 +52,7 @@ func New(options epuboptions.EPUBOptions) EPUB {
"zoom": func(s int, z float32) int { return int(float32(s) * z) }, "zoom": func(s int, z float32) int { return int(float32(s) * z) },
}) })
return EPUB{ return epub{
EPUBOptions: options, EPUBOptions: options,
UID: uid.String(), UID: uid.String(),
Publisher: "GO Comic Converter", Publisher: "GO Comic Converter",
@ -59,7 +63,7 @@ func New(options epuboptions.EPUBOptions) EPUB {
} }
// render templates // render templates
func (e EPUB) render(templateString string, data map[string]any) string { func (e epub) render(templateString string, data map[string]any) string {
var result strings.Builder var result strings.Builder
tmpl := template.Must(e.templateProcessor.Parse(templateString)) tmpl := template.Must(e.templateProcessor.Parse(templateString))
if err := tmpl.Execute(&result, data); err != nil { if err := tmpl.Execute(&result, data); err != nil {
@ -69,7 +73,7 @@ func (e EPUB) render(templateString string, data map[string]any) string {
} }
// write image to the zip // write image to the zip
func (e EPUB) writeImage(wz epubzip.EPUBZip, img epubimage.EPUBImage, zipImg *zip.File) error { func (e epub) writeImage(wz epubzip.EPUBZip, img epubimage.EPUBImage, zipImg *zip.File) error {
err := wz.WriteContent( err := wz.WriteContent(
img.EPUBPagePath(), img.EPUBPagePath(),
[]byte(e.render(epubtemplates.Text, map[string]any{ []byte(e.render(epubtemplates.Text, map[string]any{
@ -87,7 +91,7 @@ func (e EPUB) writeImage(wz epubzip.EPUBZip, img epubimage.EPUBImage, zipImg *zi
} }
// write blank page // write blank page
func (e EPUB) writeBlank(wz epubzip.EPUBZip, img epubimage.EPUBImage) error { func (e epub) writeBlank(wz epubzip.EPUBZip, img epubimage.EPUBImage) error {
return wz.WriteContent( return wz.WriteContent(
img.EPUBSpacePath(), img.EPUBSpacePath(),
[]byte(e.render(epubtemplates.Blank, map[string]any{ []byte(e.render(epubtemplates.Blank, map[string]any{
@ -98,7 +102,7 @@ func (e EPUB) writeBlank(wz epubzip.EPUBZip, img epubimage.EPUBImage) error {
} }
// write title image // write title image
func (e EPUB) writeCoverImage(wz epubzip.EPUBZip, img epubimage.EPUBImage, part, totalParts int) error { func (e epub) writeCoverImage(wz epubzip.EPUBZip, img epubimage.EPUBImage, part, totalParts int) error {
title := "Cover" title := "Cover"
text := "" text := ""
if totalParts > 1 { if totalParts > 1 {
@ -141,7 +145,7 @@ func (e EPUB) writeCoverImage(wz epubzip.EPUBZip, img epubimage.EPUBImage, part,
} }
// write title image // write title image
func (e EPUB) writeTitleImage(wz epubzip.EPUBZip, img epubimage.EPUBImage, title string) error { func (e epub) writeTitleImage(wz epubzip.EPUBZip, img epubimage.EPUBImage, title string) error {
titleAlign := "" titleAlign := ""
if !e.Image.View.PortraitOnly { if !e.Image.View.PortraitOnly {
if e.Image.Manga { if e.Image.Manga {
@ -197,7 +201,7 @@ func (e EPUB) writeTitleImage(wz epubzip.EPUBZip, img epubimage.EPUBImage, title
} }
// extract image and split it into part // extract image and split it into part
func (e EPUB) getParts() (parts []epubPart, imgStorage epubzip.StorageImageReader, err error) { func (e epub) getParts() (parts []epubPart, imgStorage epubzip.StorageImageReader, err error) {
images, err := e.imageProcessor.Load() images, err := e.imageProcessor.Load()
if err != nil { if err != nil {
@ -268,7 +272,7 @@ func (e EPUB) getParts() (parts []epubPart, imgStorage epubzip.StorageImageReade
// create a tree from the directories. // create a tree from the directories.
// //
// this is used to simulate the toc. // this is used to simulate the toc.
func (e EPUB) getTree(images []epubimage.EPUBImage, skipFiles bool) string { func (e epub) getTree(images []epubimage.EPUBImage, skipFiles bool) string {
t := epubtree.New() t := epubtree.New()
for _, img := range images { for _, img := range images {
if skipFiles { if skipFiles {
@ -285,7 +289,7 @@ func (e EPUB) getTree(images []epubimage.EPUBImage, skipFiles bool) string {
return c.WriteString("") return c.WriteString("")
} }
func (e EPUB) computeAspectRatio(epubParts []epubPart) float64 { func (e epub) computeAspectRatio(epubParts []epubPart) float64 {
var ( var (
bestAspectRatio float64 bestAspectRatio float64
bestAspectRatioCount int bestAspectRatioCount int
@ -312,7 +316,7 @@ func (e EPUB) computeAspectRatio(epubParts []epubPart) float64 {
return bestAspectRatio return bestAspectRatio
} }
func (e EPUB) computeViewPort(epubParts []epubPart) (int, int) { func (e epub) computeViewPort(epubParts []epubPart) (int, int) {
if e.Image.View.AspectRatio == -1 { if e.Image.View.AspectRatio == -1 {
//keep device size //keep device size
return e.Image.View.Width, e.Image.View.Height return e.Image.View.Width, e.Image.View.Height
@ -332,7 +336,7 @@ func (e EPUB) computeViewPort(epubParts []epubPart) (int, int) {
} }
} }
func (e EPUB) writePart(path string, currentPart, totalParts int, part epubPart, imgStorage epubzip.StorageImageReader) error { func (e epub) writePart(path string, currentPart, totalParts int, part epubPart, imgStorage epubzip.StorageImageReader) error {
hasTitlePage := e.TitlePage == 1 || (e.TitlePage == 2 && totalParts > 1) hasTitlePage := e.TitlePage == 1 || (e.TitlePage == 2 && totalParts > 1)
wz, err := epubzip.New(path) wz, err := epubzip.New(path)
@ -413,7 +417,7 @@ func (e EPUB) writePart(path string, currentPart, totalParts int, part epubPart,
} }
// create the zip // create the zip
func (e EPUB) Write() error { func (e epub) Write() error {
epubParts, imgStorage, err := e.getParts() epubParts, imgStorage, err := e.getParts()
if err != nil { if err != nil {
return err return err