/
converter.go
347 lines (286 loc) · 8.33 KB
/
converter.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Package kepub provides functions for converting EPUBs to KEPUBs.
package kepub
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"golang.org/x/net/html"
"golang.org/x/sync/errgroup"
)
// Verbose controls whether this package writes verbose output (it is silent
// otherwise).
var Verbose = false // TODO: implement
// Converter converts EPUB2/EPUB3 books to KEPUB.
type Converter struct {
// extra css
extraCSS []string
extraCSSClass []string
// smart punctuation
smartypants bool
// find/replace in raw html output (note: inefficient, but more efficient
// than working with strings)
find [][]byte
replace [][]byte
// addSpans is for use by the kobotest command and should note be used elsewhere.
addSpans func(*html.Node)
}
// NewConverter creates a new Converter. By default, no options are applied.
func NewConverter() *Converter {
return NewConverterWithOptions()
}
// NewConverterWithOptions is like NewConverter, with options.
func NewConverterWithOptions(opts ...ConverterOption) *Converter {
c := new(Converter)
for _, f := range opts {
f(c)
}
c.addSpans = transform2koboSpans
return c
}
// Convert converts the dir as the root of an EPUB.
func (c *Converter) Convert(dir string) error {
if _, err := FindOPF(dir); err != nil {
// sanity check to help guard against mistakes (i.e. we don't want to mess up someone's files if they make a mistake with the path)
return fmt.Errorf("not an epub: %s", dir)
}
if err := c.transformAllContentParallel(dir); err != nil {
return fmt.Errorf("transform content: %w", err)
}
opfPath, err := FindOPF(dir)
if err != nil {
return fmt.Errorf("transform opf: find: %w", err)
}
opfFile, err := os.OpenFile(opfPath, os.O_RDWR, 0)
if err != nil {
return fmt.Errorf("transform opf: open: %w", err)
}
defer opfFile.Close()
if err := c.TransformOPFFile(opfFile); err != nil {
return fmt.Errorf("transform opf: %w", err)
}
if err := c.transformEPUB(dir); err != nil {
return fmt.Errorf("transform epub: %w", err)
}
return nil
}
// ConvertEPUB converts an EPUB file.
func (c *Converter) ConvertEPUB(epub, kepub string) error {
td, err := ioutil.TempDir("", "kepubify")
if err != nil {
return fmt.Errorf("create temp dir: %w", err)
}
defer os.RemoveAll(td)
dir := filepath.Join(td, "unpacked")
if err := UnpackEPUB(epub, dir); err != nil {
return fmt.Errorf("unpack epub: %w", err)
}
if err := c.Convert(dir); err != nil {
return err
}
if err := PackEPUB(dir, kepub); err != nil {
return fmt.Errorf("pack kepub: %w", err)
}
return nil
}
func (c *Converter) transformAllContentParallel(dir string) error {
g, ctx := errgroup.WithContext(context.Background())
contentFiles := make(chan string)
g.Go(func() error {
defer close(contentFiles)
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
switch strings.ToLower(filepath.Ext(path)) {
case ".html", ".xhtml", ".htm", ".xhtm":
select {
case contentFiles <- path:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
})
})
for i := 0; i < runtime.NumCPU(); i++ {
g.Go(func() error {
for fn := range contentFiles {
if f, err := os.OpenFile(fn, os.O_RDWR, 0); err != nil {
return fmt.Errorf("open content file %#v: %w", fn, err)
} else if err := c.TransformContentDocFile(f); err != nil {
f.Close()
return fmt.Errorf("transform content file %#v: %w", fn, err)
} else {
f.Close()
}
select {
case <-ctx.Done():
return ctx.Err()
default:
continue
}
}
return nil
})
}
return g.Wait()
}
// TransformContentDoc transforms an HTML4/HTML5/XHTML1.1 content document for
// a KEPUB.
func (c *Converter) TransformContentDoc(w io.Writer, r io.Reader) error {
doc, err := c.transform1(r)
if err != nil {
return fmt.Errorf("parse: %w", err)
}
if err := c.transform2(doc); err != nil {
return fmt.Errorf("transform: %w", err)
}
if err := c.transform3(w, doc); err != nil {
return fmt.Errorf("render: %w", err)
}
return nil
}
// TransformContentDocFile is like TransformContentDoc, but works inplace.
func (c *Converter) TransformContentDocFile(rws interface {
io.ReadWriteSeeker
Truncate(size int64) error
}) error {
if _, err := rws.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("parse: seek to file start: %w", err)
}
doc, err := c.transform1(rws)
if err != nil {
return fmt.Errorf("parse: %w", err)
}
if err := c.transform2(doc); err != nil {
return fmt.Errorf("transform: %w", err)
}
if _, err := rws.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("render: seek to file start: %w", err)
}
if err := rws.Truncate(0); err != nil {
return fmt.Errorf("render: truncate: %w", err)
}
if err := c.transform3(rws, doc); err != nil {
return fmt.Errorf("render: %w", err)
}
return nil
}
// TransformOPF transforms the OPF document for a KEPUB.
func (c *Converter) TransformOPF(w io.Writer, r io.Reader) error {
doc, err := c.transformOPF1(r)
if err != nil {
return fmt.Errorf("parse: %w", err)
}
if err := c.transformOPF2(doc); err != nil {
return fmt.Errorf("transform: %w", err)
}
if err := c.transformOPF3(w, doc); err != nil {
return fmt.Errorf("render: %w", err)
}
return nil
}
// TransformOPFFile is like TransformOPF, but works inplace.
func (c *Converter) TransformOPFFile(rws interface {
io.ReadWriteSeeker
Truncate(size int64) error
}) error {
if _, err := rws.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("parse: seek to file start: %w", err)
}
doc, err := c.transformOPF1(rws)
if err != nil {
return fmt.Errorf("parse: %w", err)
}
if err := c.transformOPF2(doc); err != nil {
return fmt.Errorf("transform: %w", err)
}
if _, err := rws.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("render: seek to file start: %w", err)
}
if err := rws.Truncate(0); err != nil {
return fmt.Errorf("render: truncate: %w", err)
}
if err := c.transformOPF3(rws, doc); err != nil {
return fmt.Errorf("render: %w", err)
}
return nil
}
// --- Options --- //
// ConverterOption configures a Converter.
type ConverterOption func(*Converter)
// ConverterOptionSmartypants enables smart punctuation.
func ConverterOptionSmartypants() ConverterOption {
return func(c *Converter) {
c.smartypants = true
}
}
// ConverterOptionFindReplace replaces a raw string in the transformed HTML (
// note that this reduces efficiency by requiring the HTML to be encoded into
// a temporary buffer before being written).
func ConverterOptionFindReplace(find, replace string) ConverterOption {
return func(c *Converter) {
c.find = append(c.find, []byte(find))
c.replace = append(c.replace, []byte(replace))
}
}
// ConverterOptionAddCSS adds CSS code to a book.
func ConverterOptionAddCSS(css string) ConverterOption {
return converterOptionAddCSS("kepubify-extracss", css)
}
// ConverterOptionHyphenate enables or disables hyphenation. If not set, no
// specific state is enforced by kepubify.
func ConverterOptionHyphenate(enabled bool) ConverterOption {
if enabled {
return converterOptionAddCSS("kepubify-hyphenate", cssHyphenate)
}
return converterOptionAddCSS("kepubify-nohyphenate", cssNoHyphenate)
}
// ConverterOptionFullScreenFixes applies fullscreen fixes for firmwares older
// than 4.19.11911.
func ConverterOptionFullScreenFixes() ConverterOption {
return converterOptionAddCSS("kepubify-fullscreenfixes", cssFullScreenFixes)
}
func converterOptionAddCSS(class, css string) ConverterOption {
return func(c *Converter) {
c.extraCSS = append(c.extraCSS, css)
c.extraCSSClass = append(c.extraCSSClass, class)
}
}
const cssHyphenate = `* {
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
-webkit-hyphenate-after: 3;
-webkit-hyphenate-before: 3;
-webkit-hyphenate-lines: 2;
hyphenate-after: 3;
hyphenate-before: 3;
hyphenate-lines: 2;
}
h1, h2, h3, h4, h5, h6, td {
-moz-hyphens: none !important;
-webkit-hyphens: none !important;
hyphens: none !important;
}`
const cssNoHyphenate = `* {
-moz-hyphens: none !important;
-webkit-hyphens: none !important;
hyphens: none !important;
}`
const cssFullScreenFixes = `body {
margin: 0 !important;
padding: 0 !important;
}
body>div {
padding-left: 0.2em !important;
padding-right: 0.2em !important;
}`