mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 00:02:37 +02:00
fill nav files to get toc
This commit is contained in:
parent
05a936947a
commit
a24bf0cfc8
@ -1,6 +1,7 @@
|
|||||||
package epub
|
package epub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/color"
|
"image/color"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -92,6 +93,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)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -139,6 +141,37 @@ func (e *ePub) getParts() ([]*epubPart, error) {
|
|||||||
return parts, nil
|
return parts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *ePub) getToc(title string, images []*Image) ([]byte, error) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if paths["."].Children == nil {
|
||||||
|
return []byte{}, nil
|
||||||
|
}
|
||||||
|
return xml.MarshalIndent(paths["."].Children.Tags, " ", " ")
|
||||||
|
}
|
||||||
|
|
||||||
func (e *ePub) Write() error {
|
func (e *ePub) Write() error {
|
||||||
type zipContent struct {
|
type zipContent struct {
|
||||||
Name string
|
Name string
|
||||||
@ -170,6 +203,11 @@ 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)
|
||||||
|
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 +218,15 @@ 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),
|
||||||
|
"Last": part.Images[len(part.Images)-1],
|
||||||
|
})},
|
||||||
{"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,
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,6 +164,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 +188,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 +239,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 +268,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 +318,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 +387,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 +423,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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,18 +2,16 @@
|
|||||||
<!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>
|
<li><a href="Text/{{ .Last.Id }}_p{{ .Last.Part}}.xhtml">Last page</a></li>
|
||||||
<nav epub:type="page-list">
|
|
||||||
<ol>
|
|
||||||
<li><a href="Text/part.xhtml">{{ .Info.Title }}</a></li>
|
|
||||||
</ol>
|
</ol>
|
||||||
</nav>
|
</nav>
|
||||||
</body>
|
</body>
|
||||||
|
@ -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>
|
20
internal/epub/toc.go
Normal file
20
internal/epub/toc.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
type TocPart struct {
|
||||||
|
XMLName xml.Name `xml:"li"`
|
||||||
|
Title TocTitle
|
||||||
|
Children *TocChildren `xml:",omitempty"`
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user