Skip to content

Commit ec46da7

Browse files
committed
Fix issue #29 - Minor FB2 to EPUB conversion errors
1 parent c8c8515 commit ec46da7

File tree

8 files changed

+263
-153
lines changed

8 files changed

+263
-153
lines changed

pkg/conv/epub2/add.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package epub2
22

33
import (
4+
"archive/zip"
45
"encoding/base64"
56
"strconv"
7+
"time"
68
)
79

810
// AddMetadataSubject adds subject to metadata
@@ -59,8 +61,12 @@ func (e *EPUB) AddBinary(id, contentType, base64Content string) error {
5961
if err != nil {
6062
return err
6163
}
62-
63-
f, err := e.zw.Create("OEBPS/" + id)
64+
header := &zip.FileHeader{
65+
Name: "OEBPS/" + id,
66+
Method: zip.Deflate,
67+
Modified: time.Now(),
68+
}
69+
f, err := e.zw.CreateHeader(header)
6470
if err != nil {
6571
return err
6672
}
@@ -74,44 +80,55 @@ func (e *EPUB) AddOPF() error {
7480
}
7581

7682
func (e *EPUB) AddTOC() error {
83+
if len(e.Toc) == 0 {
84+
e.Toc = append(e.Toc, TOC{
85+
Id: "root",
86+
Order: 1,
87+
Text: e.Title,
88+
Src: "chapter_1.xhtml",
89+
Depth: 1,
90+
})
91+
}
7792
toc := ""
7893
prevDepth := 1
94+
playOrder := 0
7995
for i, t := range e.Toc {
8096
// fmt.Println(t)
8197
switch {
8298
case i == 0 || t.Depth > prevDepth:
8399
prevDepth = t.Depth
84100
case t.Depth == prevDepth:
85-
toc += `
86-
</navPoint>
101+
toc += `</navPoint>
87102
`
88103
case t.Depth < prevDepth:
89-
toc += `
90-
</navPoint>
104+
toc += `</navPoint>
91105
</navPoint>
92106
`
93107
prevDepth = t.Depth
94108
}
109+
110+
playOrder++
95111
toc += `
96-
<navPoint playOrder="` + strconv.Itoa(t.Order) + `" id="` + t.Id + `">
97-
<navLabel>
98-
<text>` + t.Text + `</text>
99-
</navLabel>
100-
<content src="` + t.Src + `" />
112+
<navPoint playOrder="` + strconv.Itoa(playOrder) + `" id="` + t.Id + `">
113+
<navLabel>
114+
<text>` + t.Text + ` </text>
115+
</navLabel>
116+
<content src="` + t.Src + `"/>
101117
`
102118
}
103119

104120
for i := 1; i <= prevDepth; i++ {
105-
toc += `
106-
</navPoint>
121+
toc += `</navPoint>
107122
`
108123
}
109124

110125
data := struct {
126+
UUID string
111127
Lang string
112128
Title string
113129
Toc string
114130
}{
131+
UUID: e.UUID,
115132
Lang: e.Lang,
116133
Title: e.Title,
117134
Toc: toc,
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
33
<html xmlns="http://www.w3.org/1999/xhtml">
4-
<head>
5-
<link href="main.css" rel="stylesheet" type="text/css" />
6-
<title>{{.Title}}</title>
7-
</head>
8-
<body class="{{.Type}}">
9-
{{.Content}}
10-
</body>
4+
<head>
5+
<link href="main.css" rel="stylesheet" type="text/css" />
6+
<title>{{.Title}}</title>
7+
</head>
8+
<body class="{{.Type}}">
9+
{{.Content}}
10+
</body>
1111
</html>
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1" xml:lang="{{if .Lang}}{{.Lang}}{{else}}en-US{{end}}">
2-
<head>
3-
<meta name="dtb:uid" content="BookID" />
4-
</head>
5-
<docTitle>
6-
<text>{{.Title}}</text>
7-
</docTitle>
8-
<navMap>
9-
{{.Toc}}
10-
</navMap>
3+
4+
<head>
5+
<meta name="dtb:uid" content="urn:uuid:{{.UUID}}" />
6+
</head>
7+
8+
<docTitle>
9+
<text>{{.Title}}</text>
10+
</docTitle>
11+
12+
<navMap>
13+
{{.Toc}}
14+
</navMap>
15+
1116
</ncx>

pkg/conv/epub2/epub.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"archive/zip"
55
"embed"
66
"io"
7-
"io/fs"
87
"text/template"
8+
"time"
99

1010
"github.com/google/uuid"
1111
)
@@ -42,18 +42,49 @@ func New(wc io.WriteCloser) (*EPUB, error) {
4242
}
4343

4444
epub.zw = zip.NewWriter(wc)
45-
files, err := fs.Sub(assets, "assets/files")
46-
if err != nil {
47-
panic(err)
45+
for _, f := range []string{
46+
"mimetype",
47+
"META-INF/container.xml",
48+
"OEBPS/main.css",
49+
} {
50+
var header *zip.FileHeader
51+
if f == "mimetype" {
52+
header = &zip.FileHeader{
53+
Name: f,
54+
Method: zip.Store,
55+
}
56+
} else {
57+
header = &zip.FileHeader{
58+
Name: f,
59+
Method: zip.Deflate,
60+
Modified: time.Now(),
61+
}
62+
}
63+
dst, err := epub.zw.CreateHeader(header)
64+
if err != nil {
65+
panic(err)
66+
}
67+
content, err := assets.ReadFile("assets/files/" + f)
68+
if err != nil {
69+
panic(err)
70+
}
71+
_, err = dst.Write(content)
72+
if err != nil {
73+
panic(err)
74+
}
4875
}
49-
epub.zw.AddFS(files)
5076
epub.tmpl = template.Must(template.New("").ParseFS(assets, "assets/tmpl/*.tmpl"))
5177

5278
return epub, nil
5379
}
5480

5581
func (e *EPUB) execTemplate(file, name string, data any) error {
56-
w, err := e.zw.Create(file)
82+
header := &zip.FileHeader{
83+
Name: file,
84+
Method: zip.Deflate,
85+
Modified: time.Now(),
86+
}
87+
w, err := e.zw.CreateHeader(header)
5788
if err != nil {
5889
return err
5990
}

0 commit comments

Comments
 (0)