fill nav files to get toc

This commit is contained in:
Celogeek 2023-04-07 18:23:53 +02:00
parent 05a936947a
commit a24bf0cfc8
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
5 changed files with 90 additions and 12 deletions

View File

@ -1,6 +1,7 @@
package epub
import (
"encoding/xml"
"fmt"
"image/color"
"path/filepath"
@ -92,6 +93,7 @@ func (e *ePub) render(templateString string, data any) string {
func (e *ePub) getParts() ([]*epubPart, error) {
images, err := LoadImages(e.Input, e.ImageOptions)
if err != nil {
return nil, err
}
@ -139,6 +141,37 @@ func (e *ePub) getParts() ([]*epubPart, error) {
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 {
type zipContent struct {
Name string
@ -170,6 +203,11 @@ 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
}
content := []zipContent{
{"META-INF/container.xml", containerTmpl},
{"OEBPS/content.opf", e.render(contentTmpl, map[string]any{
@ -180,8 +218,15 @@ func (e *ePub) Write() error {
"Part": i + 1,
"Total": totalParts,
})},
{"OEBPS/toc.ncx", e.render(tocTmpl, map[string]any{"Info": e})},
{"OEBPS/nav.xhtml", e.render(navTmpl, map[string]any{"Info": e})},
{"OEBPS/toc.ncx", e.render(tocTmpl, map[string]any{
"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/part.xhtml", e.render(partTmpl, map[string]any{
"Info": e,

View File

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

View File

@ -2,18 +2,16 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>{{ .Info.Title }}</title>
<title>{{ .Title }}</title>
<meta charset="utf-8"/>
</head>
<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>
<li><a href="Text/part.xhtml">{{ .Info.Title }}</a></li>
</ol>
</nav>
<nav epub:type="page-list">
<ol>
<li><a href="Text/part.xhtml">{{ .Info.Title }}</a></li>
<li><a href="Text/part.xhtml">Title</a></li>
{{ .TOC }}
<li><a href="Text/{{ .Last.Id }}_p{{ .Last.Part}}.xhtml">Last page</a></li>
</ol>
</nav>
</body>

View File

@ -7,8 +7,8 @@
<meta name="dtb:maxPageNumber" content="0"/>
<meta name="generated" content="true"/>
</head>
<docTitle><text>{{ .Info.Title }}</text></docTitle>
<docTitle><text>{{ .Title }}</text></docTitle>
<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>
</ncx>

20
internal/epub/toc.go Normal file
View 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"`
}