/
generator.go
141 lines (117 loc) · 2.8 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* benzipubor
* Copyright (C) 2018 bobo liu
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package epub_gen
import (
"archive/zip"
"log"
"os"
"strconv"
"sync"
)
type Gen struct {
imgList []string
tocNodes []toc
tocNum int
bi bookInfo
l *log.Logger
x int
Grey bool
quality int
th int
}
func (g *Gen) SetX(x int) {
g.x = inRange(x, 500, 1500)
}
func (g *Gen) SetQuality(quality int) {
g.quality = inRange(quality, 1, 100)
}
func (g *Gen) SetTh(th int) {
g.th = inRange(th, 1, 64)
}
func (g *Gen) AddTocNode(pic int, name string) {
g.tocNodes = append(g.tocNodes, toc{Pic: pic, Name: name, ID: g.tocNum})
g.tocNum += 1
}
func (g *Gen) AppendImgList(slice []string) int {
count := len(g.imgList)
g.imgList = append(g.imgList, slice...)
return count
}
func (g *Gen) SetTitle(t string) {
g.bi.Title = t
}
func (g *Gen) SetLogger(l log.Logger) {
l.SetPrefix("[EPUB]")
g.l = &l
}
func (g Gen) Do(dst string) {
f, err := os.Create(dst)
if err != nil {
g.l.Fatalln("Cannot open file to write:", dst, err)
}
defer f.Close()
w := zip.NewWriter(f)
defer w.Close()
op := newZipOp(w)
for k, v := range staticFiles {
sta, err := w.Create(k)
if err != nil {
g.l.Fatalln("Unknown error:", err)
}
sta.Write([]byte(v))
}
type imgProcessWorking struct {
id int
fn string
}
var wg sync.WaitGroup
workChan := make(chan imgProcessWorking, g.th)
go func() {
for img := range workChan {
// Pic
fw := op.Writer("image/i_" + strconv.Itoa(img.id) + ".jpg")
g.doZip(img.fn, fw)
fw.Flush()
// Page
fw = op.Writer("text/p_" + strconv.Itoa(img.id) + ".xhtml")
tpls["page"].Execute(fw, pageInfo{ID: img.id, Title: g.bi.Title})
fw.Flush()
wg.Done()
g.l.Println("Processed:", img.fn)
}
}()
for i, fn := range g.imgList {
wg.Add(1)
id := i + 1
g.bi.Objects = append(g.bi.Objects, id)
workChan <- imgProcessWorking{id: id, fn: fn}
}
wg.Wait()
g.bi.TocNodes = g.tocNodes
fw := op.Writer("content.opf")
tpls["opf"].Execute(fw, g.bi)
fw.Flush()
fw = op.Writer("toc.ncx")
tpls["toc"].Execute(fw, g.bi)
fw.Flush()
fw = op.Writer("toc.xhtml")
tpls["nav"].Execute(fw, g.bi)
fw.Flush()
op.Done()
op.Wait()
}