From 188260df33a653d1e2fc72163ad39acb18483fac Mon Sep 17 00:00:00 2001
From: celogeek <65178+celogeek@users.noreply.github.com>
Date: Mon, 26 Dec 2022 19:20:53 +0100
Subject: [PATCH] rely on id only
---
internal/epub/core.go | 37 ++++++++++++------------
internal/epub/templates/content.opf.tmpl | 6 ++--
internal/epub/templates/nav.xhtml.tmpl | 4 +--
internal/epub/templates/text.xhtml.tmpl | 10 +++----
internal/epub/templates/toc.ncx.tmpl | 2 +-
5 files changed, 30 insertions(+), 29 deletions(-)
diff --git a/internal/epub/core.go b/internal/epub/core.go
index 1d68eb2..70e42fc 100644
--- a/internal/epub/core.go
+++ b/internal/epub/core.go
@@ -16,8 +16,6 @@ import (
type Images struct {
Id int
- Path string
- Name string
Title string
Data []byte
Width int
@@ -36,8 +34,9 @@ type EPub struct {
ViewHeight int
Quality int
- Images []Images
- Error error
+ Images []Images
+ FirstImageTitle string
+ Error error
}
func NewEpub(path string) *EPub {
@@ -107,6 +106,7 @@ func (e *EPub) Render(templateString string, data any) string {
}
func (e *EPub) LoadDir(dirname string) *EPub {
+ images := make([]string, 0)
err := filepath.WalkDir(dirname, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
@@ -118,27 +118,28 @@ func (e *EPub) LoadDir(dirname string) *EPub {
if strings.ToLower(ext) != ".jpg" {
return nil
}
- name := filepath.Base(path)
- title := name[0 : len(name)-len(ext)]
- e.Images = append(e.Images, Images{
- Path: path,
- Name: name,
- Title: title,
- })
+ images = append(images, path)
return nil
})
if err != nil {
e.Error = err
return e
}
- sort.SliceStable(e.Images, func(i, j int) bool {
- return strings.Compare(e.Images[i].Path, e.Images[j].Path) < 0
- })
-
- for i := range e.Images {
- e.Images[i].Id = i
+ if len(images) == 0 {
+ e.Error = fmt.Errorf("no images found")
+ return e
}
+ sort.Strings(images)
+
+ titleFormat := fmt.Sprintf("%%0%dd", len(fmt.Sprint(len(images)-1)))
+ for i := range images {
+ e.Images = append(e.Images, Images{
+ Id: i,
+ Title: fmt.Sprintf(titleFormat, i),
+ })
+ }
+ e.FirstImageTitle = e.Images[0].Title
return e
}
@@ -161,7 +162,7 @@ func (e *EPub) Write() error {
{"OEBPS/Text/style.css", TEMPLATE_STYLE},
}
for _, img := range e.Images {
- filename := fmt.Sprintf("OEBPS/Text/%d.xhtml", img.Id)
+ filename := fmt.Sprintf("OEBPS/Text/%s.xhtml", img.Title)
zipContent = append(zipContent, []string{filename, e.Render(TEMPLATE_TEXT, img)})
}
diff --git a/internal/epub/templates/content.opf.tmpl b/internal/epub/templates/content.opf.tmpl
index a340630..b3812db 100644
--- a/internal/epub/templates/content.opf.tmpl
+++ b/internal/epub/templates/content.opf.tmpl
@@ -24,10 +24,10 @@
{{ range .Images }}
{{ if eq .Id 0 }}
-
+
{{ end }}
-
-
+
+
{{ end }}
diff --git a/internal/epub/templates/nav.xhtml.tmpl b/internal/epub/templates/nav.xhtml.tmpl
index 1a0bec8..9942198 100644
--- a/internal/epub/templates/nav.xhtml.tmpl
+++ b/internal/epub/templates/nav.xhtml.tmpl
@@ -8,12 +8,12 @@
diff --git a/internal/epub/templates/text.xhtml.tmpl b/internal/epub/templates/text.xhtml.tmpl
index ae54f8a..c57f008 100644
--- a/internal/epub/templates/text.xhtml.tmpl
+++ b/internal/epub/templates/text.xhtml.tmpl
@@ -8,7 +8,7 @@
-

+
-

+
-

+
-

+
-

+