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"
"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{

View File

@ -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

View File

@ -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
}
}

View File

@ -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,