display toc on dry mode

This commit is contained in:
Celogeek 2023-04-08 13:16:16 +02:00
parent aeb5034a43
commit 7a4821d43d
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
4 changed files with 66 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"image/color" "image/color"
"os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -11,6 +12,7 @@ import (
"time" "time"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"gopkg.in/yaml.v3"
) )
type ImageOptions struct { type ImageOptions struct {
@ -38,6 +40,7 @@ type EpubOptions struct {
Author string Author string
LimitMb int LimitMb int
StripFirstDirectoryFromToc bool StripFirstDirectoryFromToc bool
Dry bool
*ImageOptions *ImageOptions
} }
@ -93,7 +96,7 @@ func (e *ePub) render(templateString string, data any) string {
} }
func (e *ePub) getParts() ([]*epubPart, error) { 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 { if err != nil {
return nil, err return nil, err
@ -104,6 +107,15 @@ func (e *ePub) getParts() ([]*epubPart, error) {
if e.HasCover { if e.HasCover {
images = images[1:] images = images[1:]
} }
if e.Dry {
parts = append(parts, &epubPart{
Cover: cover,
Images: images,
})
return parts, nil
}
maxSize := uint64(e.LimitMb * 1024 * 1024) maxSize := uint64(e.LimitMb * 1024 * 1024)
xhtmlSize := uint64(1024) xhtmlSize := uint64(1024)
@ -142,7 +154,7 @@ func (e *ePub) getParts() ([]*epubPart, error) {
return parts, nil return parts, nil
} }
func (e *ePub) getToc(title string, images []*Image) ([]byte, error) { func (e *ePub) getToc(images []*Image) *TocChildren {
paths := map[string]*TocPart{ paths := map[string]*TocPart{
".": {}, ".": {},
} }
@ -174,11 +186,8 @@ func (e *ePub) getToc(title string, images []*Image) ([]byte, error) {
children = children.Tags[0].Children children = children.Tags[0].Children
} }
if children == nil { return children
return []byte{}, nil
}
return xml.MarshalIndent(children.Tags, " ", " ")
} }
func (e *ePub) Write() error { func (e *ePub) Write() error {
@ -191,6 +200,16 @@ func (e *ePub) Write() error {
if err != nil { if err != nil {
return err 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) totalParts := len(epubParts)
bar := NewBar(totalParts, "Writing Part", 2, 2) bar := NewBar(totalParts, "Writing Part", 2, 2)
@ -212,10 +231,15 @@ func (e *ePub) Write() error {
if totalParts > 1 { if totalParts > 1 {
title = fmt.Sprintf("%s [%d/%d]", title, i+1, totalParts) title = fmt.Sprintf("%s [%d/%d]", title, i+1, totalParts)
} }
toc, err := e.getToc(title, part.Images)
tocChildren := e.getToc(part.Images)
toc := []byte{}
if tocChildren != nil {
toc, err = xml.MarshalIndent(tocChildren.Tags, " ", " ")
if err != nil { if err != nil {
return err return err
} }
}
content := []zipContent{ content := []zipContent{
{"META-INF/container.xml", containerTmpl}, {"META-INF/container.xml", containerTmpl},

View File

@ -92,7 +92,7 @@ BOTTOM:
return imgArea return imgArea
} }
func LoadImages(path string, options *ImageOptions) ([]*Image, error) { func LoadImages(path string, options *ImageOptions, dry bool) ([]*Image, error) {
images := make([]*Image, 0) images := make([]*Image, 0)
fi, err := os.Stat(path) fi, err := os.Stat(path)
@ -123,6 +123,22 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) {
return nil, err 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) imageOutput := make(chan *Image)
// processing // processing

View File

@ -1,6 +1,8 @@
package epub package epub
import "encoding/xml" import (
"encoding/xml"
)
type TocTitle struct { type TocTitle struct {
XMLName xml.Name `xml:"a"` XMLName xml.Name `xml:"a"`
@ -13,8 +15,20 @@ type TocChildren struct {
Tags []*TocPart Tags []*TocPart
} }
func (t *TocChildren) MarshalYAML() (any, error) {
return t.Tags, nil
}
type TocPart struct { type TocPart struct {
XMLName xml.Name `xml:"li"` XMLName xml.Name `xml:"li"`
Title TocTitle Title TocTitle
Children *TocChildren `xml:",omitempty"` 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
}
}

View File

@ -90,10 +90,6 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
fmt.Fprintln(os.Stderr, cmd.Options) fmt.Fprintln(os.Stderr, cmd.Options)
if cmd.Options.Dry {
return
}
profile := cmd.Options.GetProfile() profile := cmd.Options.GetProfile()
if err := epub.NewEpub(&epub.EpubOptions{ if err := epub.NewEpub(&epub.EpubOptions{
Input: cmd.Options.Input, Input: cmd.Options.Input,
@ -102,6 +98,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
Title: cmd.Options.Title, Title: cmd.Options.Title,
Author: cmd.Options.Author, Author: cmd.Options.Author,
StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc, StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc,
Dry: cmd.Options.Dry,
ImageOptions: &epub.ImageOptions{ ImageOptions: &epub.ImageOptions{
ViewWidth: profile.Width, ViewWidth: profile.Width,
ViewHeight: profile.Height, ViewHeight: profile.Height,