rely on id only

This commit is contained in:
Celogeek 2022-12-26 19:20:53 +01:00
parent 1250fa5ea7
commit 188260df33
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
5 changed files with 30 additions and 29 deletions

View File

@ -16,8 +16,6 @@ import (
type Images struct { type Images struct {
Id int Id int
Path string
Name string
Title string Title string
Data []byte Data []byte
Width int Width int
@ -36,8 +34,9 @@ type EPub struct {
ViewHeight int ViewHeight int
Quality int Quality int
Images []Images Images []Images
Error error FirstImageTitle string
Error error
} }
func NewEpub(path string) *EPub { 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 { func (e *EPub) LoadDir(dirname string) *EPub {
images := make([]string, 0)
err := filepath.WalkDir(dirname, func(path string, d fs.DirEntry, err error) error { err := filepath.WalkDir(dirname, func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
return err return err
@ -118,27 +118,28 @@ func (e *EPub) LoadDir(dirname string) *EPub {
if strings.ToLower(ext) != ".jpg" { if strings.ToLower(ext) != ".jpg" {
return nil return nil
} }
name := filepath.Base(path)
title := name[0 : len(name)-len(ext)]
e.Images = append(e.Images, Images{ images = append(images, path)
Path: path,
Name: name,
Title: title,
})
return nil return nil
}) })
if err != nil { if err != nil {
e.Error = err e.Error = err
return e return e
} }
sort.SliceStable(e.Images, func(i, j int) bool { if len(images) == 0 {
return strings.Compare(e.Images[i].Path, e.Images[j].Path) < 0 e.Error = fmt.Errorf("no images found")
}) return e
for i := range e.Images {
e.Images[i].Id = i
} }
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 return e
} }
@ -161,7 +162,7 @@ func (e *EPub) Write() error {
{"OEBPS/Text/style.css", TEMPLATE_STYLE}, {"OEBPS/Text/style.css", TEMPLATE_STYLE},
} }
for _, img := range e.Images { 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)}) zipContent = append(zipContent, []string{filename, e.Render(TEMPLATE_TEXT, img)})
} }

View File

@ -24,10 +24,10 @@
<item id="nav" href="nav.xhtml" properties="nav" media-type="application/xhtml+xml"/> <item id="nav" href="nav.xhtml" properties="nav" media-type="application/xhtml+xml"/>
{{ range .Images }} {{ range .Images }}
{{ if eq .Id 0 }} {{ if eq .Id 0 }}
<item id="cover" href="Images/{{ .Id }}.jpg" media-type="image/jpeg" properties="cover-image"/> <item id="cover" href="Images/{{ .Title }}.jpg" media-type="image/jpeg" properties="cover-image"/>
{{ end }} {{ end }}
<item id="page_{{ .Id }}" href="Text/{{ .Id }}.xhtml" media-type="application/xhtml+xml"/> <item id="page_{{ .Id }}" href="Text/{{ .Title }}.xhtml" media-type="application/xhtml+xml"/>
<item id="img_{{ .Id }}" href="Images/{{ .Id }}.jpg" media-type="image/jpeg"/> <item id="img_{{ .Id }}" href="Images/{{ .Title }}.jpg" media-type="image/jpeg"/>
{{ end }} {{ end }}
</manifest> </manifest>
<spine page-progression-direction="ltr" toc="ncx"> <spine page-progression-direction="ltr" toc="ncx">

View File

@ -8,12 +8,12 @@
<body> <body>
<nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc"> <nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc">
<ol> <ol>
<li><a href="Text/0.xhtml">{{ .Title }}</a></li> <li><a href="Text/{{ .FirstImageTitle }}.xhtml">{{ .Title }}</a></li>
</ol> </ol>
</nav> </nav>
<nav epub:type="page-list"> <nav epub:type="page-list">
<ol> <ol>
<li><a href="Text/0.xhtml">{{ .Title }}</a></li> <li><a href="Text/{{ .FirstImageTitle }}.xhtml">{{ .Title }}</a></li>
</ol> </ol>
</nav> </nav>
</body> </body>

View File

@ -8,7 +8,7 @@
</head> </head>
<body style=""> <body style="">
<div style="text-align:center;top:0.0%;"> <div style="text-align:center;top:0.0%;">
<img width="{{ .Width }}" height="{{ .Height }}" src="../Images/{{ .Id }}.jpg"/> <img width="{{ .Width }}" height="{{ .Height }}" src="../Images/{{ .Title }}.jpg"/>
</div> </div>
<div id="PV"> <div id="PV">
<div id="PV-TL"> <div id="PV-TL">
@ -25,16 +25,16 @@
</div> </div>
</div> </div>
<div class="PV-P" id="PV-TL-P" style=""> <div class="PV-P" id="PV-TL-P" style="">
<img style="position:absolute;left:0;top:0;" src="../Images/{{ .Id }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/> <img style="position:absolute;left:0;top:0;" src="../Images/{{ .Title }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/>
</div> </div>
<div class="PV-P" id="PV-TR-P" style=""> <div class="PV-P" id="PV-TR-P" style="">
<img style="position:absolute;right:0;top:0;" src="../Images/{{ .Id }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/> <img style="position:absolute;right:0;top:0;" src="../Images/{{ .Title }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/>
</div> </div>
<div class="PV-P" id="PV-BL-P" style=""> <div class="PV-P" id="PV-BL-P" style="">
<img style="position:absolute;left:0;bottom:0;" src="../Images/{{ .Id }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/> <img style="position:absolute;left:0;bottom:0;" src="../Images/{{ .Title }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/>
</div> </div>
<div class="PV-P" id="PV-BR-P" style=""> <div class="PV-P" id="PV-BR-P" style="">
<img style="position:absolute;right:0;bottom:0;" src="../Images/{{ .Id }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/> <img style="position:absolute;right:0;bottom:0;" src="../Images/{{ .Title }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/>
</div> </div>
</body> </body>
</html> </html>

View File

@ -9,6 +9,6 @@
</head> </head>
<docTitle><text>{{ .Title }}</text></docTitle> <docTitle><text>{{ .Title }}</text></docTitle>
<navMap> <navMap>
<navPoint id="Text"><navLabel><text>{{ .Title }}</text></navLabel><content src="Text/0.xhtml"/></navPoint> <navPoint id="Text"><navLabel><text>{{ .Title }}</text></navLabel><content src="Text/{{ .FirstImageTitle }}.xhtml"/></navPoint>
</navMap> </navMap>
</ncx> </ncx>