/
kepub.go
299 lines (257 loc) · 8.12 KB
/
kepub.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
package kepub
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/beevik/etree"
zglob "github.com/mattn/go-zglob"
"golang.org/x/net/html"
)
const HyphenateCSS = `* {
-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 NoHyphenateCSS = `* {
-moz-hyphens: none !important;
-webkit-hyphens: none !important;
hyphens: none !important;
}`
const FullScreenFixesCSS = `body {
margin: 0 !important;
padding: 0 !important;
}
body>div {
padding-left: 0.2em !important;
padding-right: 0.2em !important;
}`
type Converter struct {
PostDoc func(doc *goquery.Document) error // post-processing the parsed document
PostHTML func(h string) (string, error) // post-processing the resulting html string (after PostDoc)
ExtraCSS string // extra css to add
NoHyphenate bool // force disable hyphenation
Hyphenate bool // force enable hyphenation
InlineStyles bool // inline all stylesheets
FullScreenFixes bool // add css to fix FullScreenReading (probably not needed since 4.11.11911)
FindReplace map[string]string // find and replace on the html of all content files
Verbose bool // verbose output to stdout during conversion
}
func (c *Converter) Convert(src, dest string) error {
td, err := ioutil.TempDir("", "kepubify")
if err != nil {
return fmt.Errorf("could not create temp dir: %s", err)
}
defer os.RemoveAll(td)
if err := UnpackEPUB(src, td, true); err != nil {
return err
}
var contentFiles []string
for _, glob := range []string{"**/*.html", "**/*.xhtml", "**/*.htm"} {
if files, err := zglob.Glob(filepath.Join(td, glob)); err != nil {
return fmt.Errorf("could not search for content files: %v", err)
} else {
contentFiles = append(contentFiles, files...)
}
}
runtime.GOMAXPROCS(runtime.NumCPU() + 1)
var wg sync.WaitGroup
err = nil
var errOnce sync.Once // this is a slightly less efficient method for handling errors, but it is way cleaner
errfn := func(ferr error) {
errOnce.Do(func() {
err = ferr
})
}
for _, f := range contentFiles {
wg.Add(1)
go func(f string) {
defer wg.Done()
if buf, err := ioutil.ReadFile(f); err != nil {
errfn(err)
return
} else if html, err := c.ProcessHTML(string(buf), f); err != nil {
errfn(err)
return
} else if err := ioutil.WriteFile(f, []byte(html), 0644); err != nil {
errfn(err)
return
}
time.Sleep(time.Millisecond * 5)
}(f)
}
if wg.Wait(); err != nil {
return err
}
rsk, err := os.Open(filepath.Join(td, "META-INF", "container.xml"))
if err != nil {
return fmt.Errorf("error opening container.xml: %v", err)
}
defer rsk.Close()
container := etree.NewDocument()
if _, err = container.ReadFrom(rsk); err != nil {
return fmt.Errorf("error parsing container.xml: %v", err)
}
var rootfile string
for _, e := range container.FindElements("//rootfiles/rootfile[@full-path]") {
rootfile = e.SelectAttrValue("full-path", "")
}
if rootfile == "" {
return fmt.Errorf("error parsing container.xml")
}
if buf, err := ioutil.ReadFile(filepath.Join(td, rootfile)); err != nil {
return fmt.Errorf("error parsing content.opf: %v", err)
} else if opf, err := c.ProcessOPF(string(buf)); err != nil {
return fmt.Errorf("error cleaning content.opf: %v", err)
} else if err := ioutil.WriteFile(filepath.Join(td, rootfile), []byte(opf), 0644); err != nil {
return fmt.Errorf("error writing new content.opf: %v", err)
}
if err := c.CleanFiles(td); err != nil {
return fmt.Errorf("error cleaning extra files: %v", err)
}
if err := PackEPUB(td, dest, true); err != nil {
return fmt.Errorf("error creating new epub: %v", err)
}
return nil
}
// ProcessHTML processes a html file. filename is the full path to the file and is required if inlining styles.
func (c *Converter) ProcessHTML(html, filename string) (string, error) {
html = fixInvalidSelfClosingTags(html)
var doc *goquery.Document
var err error
if doc, err = goquery.NewDocumentFromReader(strings.NewReader(html)); err != nil {
return html, err
} else {
for _, fn := range []func(*goquery.Document) error{addDivs, addSpans, addKoboStyles, cleanHTML} {
if err = fn(doc); err != nil {
return html, err
}
}
if c.InlineStyles {
if filename == "" {
return html, errors.New("filename required for inlining styles")
}
doc.Find("link[rel='stylesheet'][href$='.css']").Each(func(_ int, s *goquery.Selection) {
if buf, err := ioutil.ReadFile(filepath.Join(filepath.Dir(filename), s.AttrOr("href", ""))); err == nil {
s.ReplaceWithNodes(styleNode(string(buf), "kepubify-inlinestyle"))
}
})
}
if c.FullScreenFixes {
doc.Find("body").AppendNodes(styleNode(FullScreenFixesCSS, "kepubify-fullscreenfixes"))
}
if c.Hyphenate {
doc.Find("body").AppendNodes(styleNode(HyphenateCSS, "kepubify-hyphenate"))
}
if c.NoHyphenate {
doc.Find("body").AppendNodes(styleNode(NoHyphenateCSS, "kepubify-nohyphenate"))
}
if c.ExtraCSS != "" {
doc.Find("body").AppendNodes(styleNode(c.ExtraCSS, "kepubify-extracss"))
}
if c.PostDoc != nil {
if err = c.PostDoc(doc); err != nil {
return html, err
}
}
if html, err = doc.Html(); err != nil {
return html, err
}
}
html = smartenPunctuation(html)
html = strings.Replace(html, "�", "", -1) // Remove unicode replacement chars
html = strings.Replace(html, `<!-- ?xml version="1.0" encoding="utf-8"? -->`, `<?xml version="1.0" encoding="utf-8"?>`, 1) // Fix commented xml tag
html = strings.Replace(html, `<!--?xml version="1.0" encoding="utf-8"?-->`, `<?xml version="1.0" encoding="utf-8"?>`, 1) // Fix commented xml tag
html = strings.Replace(html, "\u00a0", " ", -1) // Fix nbsps removed
if c.FindReplace != nil {
for find, replace := range c.FindReplace {
html = strings.Replace(html, find, replace, -1)
}
}
if c.PostHTML != nil {
if html, err = c.PostHTML(html); err != nil {
return html, err
}
}
return html, nil
}
func (c *Converter) ProcessOPF(opf string) (string, error) {
doc := etree.NewDocument()
err := doc.ReadFromString(opf)
if err != nil {
return "", err
}
// Add properties="cover-image" to cover file item entry to enable the kobo
// to find the cover image.
for _, meta := range doc.FindElements("//meta[@name='cover']") {
coverID := meta.SelectAttrValue("content", "")
if coverID == "" {
coverID = "cover"
}
for _, item := range doc.FindElements("//[@id='" + coverID + "']") {
item.CreateAttr("properties", "cover-image")
}
}
for _, meta := range doc.FindElements("//meta[@name='calibre:timestamp']") {
meta.Parent().RemoveChild(meta)
}
for _, contributor := range doc.FindElements("//dc:contributor[@role='bkp']") {
contributor.Parent().RemoveChild(contributor)
}
doc.Indent(4)
if opf, err = doc.WriteToString(); err != nil {
return opf, err
}
return opf, nil
}
func (c *Converter) CleanFiles(epubDir string) error {
for _, file := range []string{
"META-INF/calibre_bookmarks.txt",
"META-INF/iTunesMetadata.plist",
"META-INF/iTunesArtwork.plist",
"META-INF/.DS_STORE",
"META-INF/thumbs.db",
".DS_STORE",
"thumbs.db",
"iTunesMetadata.plist",
"iTunesArtwork.plist",
} {
os.Remove(filepath.Join(epubDir, file))
}
return nil
}
func styleNode(cssstr, class string) *html.Node {
style := &html.Node{
Type: html.ElementNode,
Data: "style",
Attr: []html.Attribute{{
Key: "type",
Val: "text/css",
}, {
Key: "class",
Val: class,
}},
}
style.AppendChild(&html.Node{
Type: html.TextNode,
Data: cssstr,
})
return style
}