From 7a4821d43d7f4c330ced6eaa5b3636007e721f14 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 8 Apr 2023 13:16:16 +0200 Subject: [PATCH] display toc on dry mode --- internal/epub/core.go | 42 ++++++++++++++++++++++++------- internal/epub/image_processing.go | 18 ++++++++++++- internal/epub/toc.go | 16 +++++++++++- main.go | 5 +--- 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/internal/epub/core.go b/internal/epub/core.go index 7c3184a..c23a008 100644 --- a/internal/epub/core.go +++ b/internal/epub/core.go @@ -4,6 +4,7 @@ import ( "encoding/xml" "fmt" "image/color" + "os" "path/filepath" "regexp" "strings" @@ -11,6 +12,7 @@ import ( "time" "github.com/gofrs/uuid" + "gopkg.in/yaml.v3" ) type ImageOptions struct { @@ -38,6 +40,7 @@ type EpubOptions struct { Author string LimitMb int StripFirstDirectoryFromToc bool + Dry bool *ImageOptions } @@ -93,7 +96,7 @@ func (e *ePub) render(templateString string, data any) string { } func (e *ePub) getParts() ([]*epubPart, error) { - images, err := LoadImages(e.Input, e.ImageOptions) + images, err := LoadImages(e.Input, e.ImageOptions, e.Dry) if err != nil { return nil, err @@ -104,6 +107,15 @@ func (e *ePub) getParts() ([]*epubPart, error) { if e.HasCover { images = images[1:] } + + if e.Dry { + parts = append(parts, &epubPart{ + Cover: cover, + Images: images, + }) + return parts, nil + } + maxSize := uint64(e.LimitMb * 1024 * 1024) xhtmlSize := uint64(1024) @@ -142,7 +154,7 @@ func (e *ePub) getParts() ([]*epubPart, error) { return parts, nil } -func (e *ePub) getToc(title string, images []*Image) ([]byte, error) { +func (e *ePub) getToc(images []*Image) *TocChildren { paths := map[string]*TocPart{ ".": {}, } @@ -174,11 +186,8 @@ func (e *ePub) getToc(title string, images []*Image) ([]byte, error) { children = children.Tags[0].Children } - if children == nil { - return []byte{}, nil - } + return children - return xml.MarshalIndent(children.Tags, " ", " ") } func (e *ePub) Write() error { @@ -191,6 +200,16 @@ func (e *ePub) Write() error { if err != nil { return err } + + if e.Dry { + tocChildren := e.getToc(epubParts[0].Images) + fmt.Fprintf(os.Stderr, "TOC:\n- %s\n", e.Title) + if tocChildren != nil { + yaml.NewEncoder(os.Stderr).Encode(tocChildren) + } + return nil + } + totalParts := len(epubParts) bar := NewBar(totalParts, "Writing Part", 2, 2) @@ -212,9 +231,14 @@ func (e *ePub) Write() error { if totalParts > 1 { title = fmt.Sprintf("%s [%d/%d]", title, i+1, totalParts) } - toc, err := e.getToc(title, part.Images) - if err != nil { - return err + + tocChildren := e.getToc(part.Images) + toc := []byte{} + if tocChildren != nil { + toc, err = xml.MarshalIndent(tocChildren.Tags, " ", " ") + if err != nil { + return err + } } content := []zipContent{ diff --git a/internal/epub/image_processing.go b/internal/epub/image_processing.go index b723efb..678d427 100644 --- a/internal/epub/image_processing.go +++ b/internal/epub/image_processing.go @@ -92,7 +92,7 @@ BOTTOM: return imgArea } -func LoadImages(path string, options *ImageOptions) ([]*Image, error) { +func LoadImages(path string, options *ImageOptions, dry bool) ([]*Image, error) { images := make([]*Image, 0) fi, err := os.Stat(path) @@ -123,6 +123,22 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) { return nil, err } + if dry { + for img := range imageInput { + images = append(images, &Image{ + img.Id, + 0, + nil, + 0, + 0, + false, + false, // NeedSpace reajust during parts computation + img.Path, + }) + } + return images, nil + } + imageOutput := make(chan *Image) // processing diff --git a/internal/epub/toc.go b/internal/epub/toc.go index ddaeddb..8f9758b 100644 --- a/internal/epub/toc.go +++ b/internal/epub/toc.go @@ -1,6 +1,8 @@ package epub -import "encoding/xml" +import ( + "encoding/xml" +) type TocTitle struct { XMLName xml.Name `xml:"a"` @@ -13,8 +15,20 @@ type TocChildren struct { Tags []*TocPart } +func (t *TocChildren) MarshalYAML() (any, error) { + return t.Tags, nil +} + type TocPart struct { XMLName xml.Name `xml:"li"` Title TocTitle Children *TocChildren `xml:",omitempty"` } + +func (t *TocPart) MarshalYAML() (any, error) { + if t.Children == nil { + return t.Title.Value, nil + } else { + return map[string]any{t.Title.Value: t.Children}, nil + } +} diff --git a/main.go b/main.go index dfd0e8a..225dd86 100644 --- a/main.go +++ b/main.go @@ -90,10 +90,6 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s fmt.Fprintln(os.Stderr, cmd.Options) - if cmd.Options.Dry { - return - } - profile := cmd.Options.GetProfile() if err := epub.NewEpub(&epub.EpubOptions{ Input: cmd.Options.Input, @@ -102,6 +98,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s Title: cmd.Options.Title, Author: cmd.Options.Author, StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc, + Dry: cmd.Options.Dry, ImageOptions: &epub.ImageOptions{ ViewWidth: profile.Width, ViewHeight: profile.Height,