Merge branch 'toc'

This commit is contained in:
Celogeek 2023-04-08 16:23:15 +02:00
commit c1b67a250e
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
8 changed files with 213 additions and 71 deletions

View File

@ -96,6 +96,7 @@ func (c *Converter) InitParse() {
c.AddBoolParam(&c.Options.HasCover, "hascover", c.Options.HasCover, "Has cover. Indicate if your comic have a cover. The first page will be used as a cover and include after the title.") c.AddBoolParam(&c.Options.HasCover, "hascover", c.Options.HasCover, "Has cover. Indicate if your comic have a cover. The first page will be used as a cover and include after the title.")
c.AddBoolParam(&c.Options.AddPanelView, "addpanelview", c.Options.AddPanelView, "Add an embeded panel view. On kindle you may not need this option as it is handled by the kindle.") c.AddBoolParam(&c.Options.AddPanelView, "addpanelview", c.Options.AddPanelView, "Add an embeded panel view. On kindle you may not need this option as it is handled by the kindle.")
c.AddIntParam(&c.Options.LimitMb, "limitmb", c.Options.LimitMb, "Limit size of the ePub: Default nolimit (0), Minimum 20") c.AddIntParam(&c.Options.LimitMb, "limitmb", c.Options.LimitMb, "Limit size of the ePub: Default nolimit (0), Minimum 20")
c.AddBoolParam(&c.Options.StripFirstDirectoryFromToc, "strip", c.Options.StripFirstDirectoryFromToc, "Strip first directory from the TOC if only 1")
c.AddSection("Default config") c.AddSection("Default config")
c.AddBoolParam(&c.Options.Show, "show", false, "Show your default parameters") c.AddBoolParam(&c.Options.Show, "show", false, "Show your default parameters")

View File

@ -32,6 +32,7 @@ type Options struct {
HasCover bool `yaml:"has_cover"` HasCover bool `yaml:"has_cover"`
AddPanelView bool `yaml:"add_panel_view"` AddPanelView bool `yaml:"add_panel_view"`
LimitMb int `yaml:"limit_mb"` LimitMb int `yaml:"limit_mb"`
StripFirstDirectoryFromToc bool `yaml:"strip_first_directory_from_toc"`
// Default Config // Default Config
Show bool `yaml:"-"` Show bool `yaml:"-"`
@ -60,6 +61,7 @@ func New() *Options {
HasCover: true, HasCover: true,
AddPanelView: false, AddPanelView: false,
LimitMb: 0, LimitMb: 0,
StripFirstDirectoryFromToc: false,
profiles: profiles.New(), profiles: profiles.New(),
} }
} }
@ -132,12 +134,13 @@ func (o *Options) ShowDefault() string {
Brightness : %d Brightness : %d
Contrast : %d Contrast : %d
AutoRotate : %v AutoRotate : %v
AutoSplitDoublePage: %v AutoSplitDoublePage : %v
NoBlankPage : %v NoBlankPage : %v
Manga : %v Manga : %v
HasCover : %v HasCover : %v
AddPanelView : %v AddPanelView : %v
LimitMb : %s`, LimitMb : %s
StripFirstDirectoryFromToc: %v`,
profileDesc, profileDesc,
o.Quality, o.Quality,
o.Crop, o.Crop,
@ -150,6 +153,7 @@ func (o *Options) ShowDefault() string {
o.HasCover, o.HasCover,
o.AddPanelView, o.AddPanelView,
limitmb, limitmb,
o.StripFirstDirectoryFromToc,
) )
} }

View File

@ -1,8 +1,10 @@
package epub package epub
import ( import (
"encoding/xml"
"fmt" "fmt"
"image/color" "image/color"
"os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -10,6 +12,7 @@ import (
"time" "time"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"gopkg.in/yaml.v3"
) )
type ImageOptions struct { type ImageOptions struct {
@ -36,6 +39,8 @@ type EpubOptions struct {
Title string Title string
Author string Author string
LimitMb int LimitMb int
StripFirstDirectoryFromToc bool
Dry bool
*ImageOptions *ImageOptions
} }
@ -91,7 +96,8 @@ 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
} }
@ -101,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)
@ -139,6 +154,42 @@ func (e *ePub) getParts() ([]*epubPart, error) {
return parts, nil return parts, nil
} }
func (e *ePub) getToc(images []*Image) *TocChildren {
paths := map[string]*TocPart{
".": {},
}
for _, img := range images {
currentPath := "."
for _, path := range strings.Split(img.Path, string(filepath.Separator)) {
parentPath := currentPath
currentPath = filepath.Join(currentPath, path)
if _, ok := paths[currentPath]; ok {
continue
}
part := &TocPart{
Title: TocTitle{
Value: path,
Link: fmt.Sprintf("Text/%d_p%d.xhtml", img.Id, img.Part),
},
}
paths[currentPath] = part
if paths[parentPath].Children == nil {
paths[parentPath].Children = &TocChildren{}
}
paths[parentPath].Children.Tags = append(paths[parentPath].Children.Tags, part)
}
}
children := paths["."].Children
if children != nil && e.StripFirstDirectoryFromToc && len(children.Tags) == 1 {
children = children.Tags[0].Children
}
return children
}
func (e *ePub) Write() error { func (e *ePub) Write() error {
type zipContent struct { type zipContent struct {
Name string Name string
@ -149,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)
@ -170,6 +231,16 @@ 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)
} }
tocChildren := e.getToc(part.Images)
toc := []byte{}
if tocChildren != nil {
toc, err = xml.MarshalIndent(tocChildren.Tags, " ", " ")
if err != nil {
return err
}
}
content := []zipContent{ content := []zipContent{
{"META-INF/container.xml", containerTmpl}, {"META-INF/container.xml", containerTmpl},
{"OEBPS/content.opf", e.render(contentTmpl, map[string]any{ {"OEBPS/content.opf", e.render(contentTmpl, map[string]any{
@ -180,8 +251,14 @@ func (e *ePub) Write() error {
"Part": i + 1, "Part": i + 1,
"Total": totalParts, "Total": totalParts,
})}, })},
{"OEBPS/toc.ncx", e.render(tocTmpl, map[string]any{"Info": e})}, {"OEBPS/toc.ncx", e.render(tocTmpl, map[string]any{
{"OEBPS/nav.xhtml", e.render(navTmpl, map[string]any{"Info": e})}, "Info": e,
"Title": title,
})},
{"OEBPS/nav.xhtml", e.render(navTmpl, map[string]any{
"Title": title,
"TOC": string(toc),
})},
{"OEBPS/Text/style.css", styleTmpl}, {"OEBPS/Text/style.css", styleTmpl},
{"OEBPS/Text/part.xhtml", e.render(partTmpl, map[string]any{ {"OEBPS/Text/part.xhtml", e.render(partTmpl, map[string]any{
"Info": e, "Info": e,

View File

@ -31,11 +31,13 @@ type Image struct {
Height int Height int
IsCover bool IsCover bool
NeedSpace bool NeedSpace bool
Path string
} }
type imageTask struct { type imageTask struct {
Id int Id int
Reader io.ReadCloser Reader io.ReadCloser
Path string
Filename string Filename string
} }
@ -90,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)
@ -121,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
@ -162,6 +180,7 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) {
dst.Bounds().Dy(), dst.Bounds().Dy(),
img.Id == 0, img.Id == 0,
false, false,
img.Path,
} }
// Auto split double page // Auto split double page
@ -185,6 +204,7 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) {
dst.Bounds().Dy(), dst.Bounds().Dy(),
false, false,
false, // NeedSpace reajust during parts computation false, // NeedSpace reajust during parts computation
img.Path,
} }
} }
} }
@ -235,6 +255,7 @@ func isSupportedImage(path string) bool {
func loadDir(input string) (int, chan *imageTask, error) { func loadDir(input string) (int, chan *imageTask, error) {
images := make([]string, 0) images := make([]string, 0)
input = filepath.Clean(input)
err := filepath.WalkDir(input, func(path string, d fs.DirEntry, err error) error { err := filepath.WalkDir(input, func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
return err return err
@ -263,9 +284,16 @@ func loadDir(input string) (int, chan *imageTask, error) {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
p := filepath.Dir(img)
if p == input {
p = ""
} else {
p = p[len(input)+1:]
}
output <- &imageTask{ output <- &imageTask{
Id: i, Id: i,
Reader: f, Reader: f,
Path: p,
Filename: img, Filename: img,
} }
} }
@ -306,6 +334,7 @@ func loadCbz(input string) (int, chan *imageTask, error) {
output <- &imageTask{ output <- &imageTask{
Id: i, Id: i,
Reader: f, Reader: f,
Path: filepath.Dir(filepath.Clean(img.Name)),
Filename: img.Name, Filename: img.Name,
} }
} }
@ -374,6 +403,7 @@ func loadCbr(input string) (int, chan *imageTask, error) {
output <- &imageTask{ output <- &imageTask{
Id: idx, Id: idx,
Reader: io.NopCloser(b), Reader: io.NopCloser(b),
Path: filepath.Dir(filepath.Clean(f.Name)),
Filename: f.Name, Filename: f.Name,
} }
} }
@ -409,6 +439,7 @@ func loadPdf(input string) (int, chan *imageTask, error) {
output <- &imageTask{ output <- &imageTask{
Id: i, Id: i,
Reader: io.NopCloser(b), Reader: io.NopCloser(b),
Path: "/",
Filename: fmt.Sprintf("page %d", i+1), Filename: fmt.Sprintf("page %d", i+1),
} }
} }

View File

@ -2,18 +2,15 @@
<!DOCTYPE html> <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head> <head>
<title>{{ .Info.Title }}</title> <title>{{ .Title }}</title>
<meta charset="utf-8"/> <meta charset="utf-8"/>
</head> </head>
<body> <body>
<nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc"> <nav epub:type="toc" id="toc">
<h1>Table of content</h1>
<ol> <ol>
<li><a href="Text/part.xhtml">{{ .Info.Title }}</a></li> <li><a href="Text/part.xhtml">{{ .Title }}</a></li>
</ol> {{ .TOC }}
</nav>
<nav epub:type="page-list">
<ol>
<li><a href="Text/part.xhtml">{{ .Info.Title }}</a></li>
</ol> </ol>
</nav> </nav>
</body> </body>

View File

@ -7,8 +7,8 @@
<meta name="dtb:maxPageNumber" content="0"/> <meta name="dtb:maxPageNumber" content="0"/>
<meta name="generated" content="true"/> <meta name="generated" content="true"/>
</head> </head>
<docTitle><text>{{ .Info.Title }}</text></docTitle> <docTitle><text>{{ .Title }}</text></docTitle>
<navMap> <navMap>
<navPoint id="Text"><navLabel><text>{{ .Info.Title }}</text></navLabel><content src="Text/part.xhtml"/></navPoint> <navPoint id="Text"><navLabel><text>{{ .Title }}</text></navLabel><content src="Text/part.xhtml"/></navPoint>
</navMap> </navMap>
</ncx> </ncx>

34
internal/epub/toc.go Normal file
View File

@ -0,0 +1,34 @@
package epub
import (
"encoding/xml"
)
type TocTitle struct {
XMLName xml.Name `xml:"a"`
Value string `xml:",innerxml"`
Link string `xml:"href,attr"`
}
type TocChildren struct {
XMLName xml.Name `xml:"ol"`
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

@ -36,7 +36,7 @@ func main() {
} }
latest_version := v.Versions[0] latest_version := v.Versions[0]
fmt.Printf(`go-comic-converter fmt.Fprintf(os.Stderr, `go-comic-converter
Path : %s Path : %s
Sum : %s Sum : %s
Version : %s Version : %s
@ -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,
@ -101,6 +97,8 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
LimitMb: cmd.Options.LimitMb, LimitMb: cmd.Options.LimitMb,
Title: cmd.Options.Title, Title: cmd.Options.Title,
Author: cmd.Options.Author, Author: cmd.Options.Author,
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,