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 {
Id int
Path string
Name string
Title string
Data []byte
Width int
@ -37,6 +35,7 @@ type EPub struct {
Quality int
Images []Images
FirstImageTitle string
Error error
}
@ -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)})
}

View File

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

View File

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

View File

@ -8,7 +8,7 @@
</head>
<body style="">
<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 id="PV">
<div id="PV-TL">
@ -25,16 +25,16 @@
</div>
</div>
<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 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 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 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>
</body>
</html>

View File

@ -9,6 +9,6 @@
</head>
<docTitle><text>{{ .Title }}</text></docTitle>
<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>
</ncx>