-
Notifications
You must be signed in to change notification settings - Fork 2
/
epubmaker.rb
423 lines (348 loc) · 13.4 KB
/
epubmaker.rb
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
require 'fileutils'
require_relative '../header.rb'
require_relative '../metadata.rb'
# ---------------------- VARIABLES
local_log_hash, @log_hash = Bkmkr::Paths.setLocalLoghash
# the directory where the epub will be created initially
epub_dir = Bkmkr::Paths.project_tmp_dir
# the path for the epub zip tool
zipepub_py = File.join(Bkmkr::Paths.core_dir, "epubmaker", "zipepub.py")
# the path for the adjusted epub html file
epub_tmp_html = File.join(Bkmkr::Paths.project_tmp_dir, "epub_tmp.xhtml")
# the path for strip-tocnodes.js
strip_tocnodes_js = File.join(Bkmkr::Paths.core_dir, "epubmaker", "strip-tocnodes.js")
# the path for strip-tocnodes.js
cover_placeholder_js = File.join(Bkmkr::Paths.core_dir, "epubmaker", "cover_placeholder.js")
# the path for strip-halftitle.js
strip_halftitle_xsl = File.join(Bkmkr::Paths.core_dir, "epubmaker", "strip-halftitle.xsl")
# the path for the primary epub conversion xsl from HTMLBook
epub_xsl = File.join(Bkmkr::Paths.scripts_dir, "HTMLBook", "htmlbook-xsl", "epub.xsl")
# the path for the temporary epub file
tmp_epub = File.join(Bkmkr::Paths.project_tmp_dir, "tmp.epub")
# the path for the temp OEBPS dir
oebps_dir = File.join(Bkmkr::Paths.project_tmp_dir, "OEBPS")
# the path for the temp META-INF dir
metainf_dir = File.join(Bkmkr::Paths.project_tmp_dir, "META-INF")
# cover html file within the epub
cover_html = File.join(oebps_dir, "cover.xhtml")
# ncx file within the epub
toc_ncx = File.join(oebps_dir, "toc.ncx")
# opf file within the epub
content_opf = File.join(oebps_dir, "content.opf")
# the path for the converted epub cover file
cover_jpg = File.join(oebps_dir, "cover.jpg")
# the path to the holding dir for epub image conversion
epub_img_dir = File.join(Bkmkr::Paths.project_tmp_dir, "epubimg")
# final epub filename
csfilename = "#{Metadata.eisbn}_EPUB"
# epub css file
epub_css = File.join(Metadata.final_dir, "layout", "epub.css")
# final image directory
img_dir = File.join(Metadata.final_dir, "images")
# final converted epub
final_epub = File.join(Bkmkr::Paths.project_tmp_dir, "#{csfilename}.epub")
# final archive dir
final_dir = Metadata.final_dir
# second epub conversion
tmp_epub2 = File.join(Bkmkr::Paths.project_tmp_dir, "#{csfilename}.epub")
# ---------------------- METHODS
def readConfigJson(logkey='')
data_hash = Mcmlln::Tools.readjson(Metadata.configfile)
return data_hash
rescue => logstring
return {}
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
# Delete any old conversion stuff
## wrapping a Mcmlln::Tools method in a new method for this script; to return a result for json_logfile
def deleteOld(dir, logkey='')
if File.exists?(dir)
Mcmlln::Tools.deleteDir(dir)
else
logstring = 'n-a'
end
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
# Adding author meta element to head
# Replacing toc with empty nav, as required by htmlbook xsl
def firstHTMLEdit(file, logkey='')
filecontents = File.read(file)
hascreator = filecontents.match(/meta name="author"/)
haspublisher = filecontents.match(/meta name="publisher"/)
if hascreator.nil?
filecontents = filecontents.gsub(/<\/head>/,"<meta name='author' content=\"#{Metadata.bookauthor}\" /></head>")
end
if haspublisher.nil?
filecontents = filecontents.gsub(/<\/head>/,"<meta name='publisher' content=\"#{Metadata.imprint}\" /></head>")
end
filecontents = filecontents.gsub(/<\/head>/,"<meta name='isbn-13' content='#{Metadata.eisbn}' /></head>")
.gsub(/ /," ")
.gsub(/src="images\//,"src=\"")
return filecontents
rescue => logstring
return ''
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
# WDV-314: using nbsp's to try to keep ellipses and their preceding word together
# => where there's not a preceding word we still try to still add nbsps to the ellipsis itself
def makeEllipsesNonbreaking(content, logkey='')
filecontents = content.gsub(/ \. \. \./," . . .")
.gsub(/\. \. \./,". . .")
return filecontents
rescue => logstring
return content
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
## wrapping a Mcmlln::Tools method in a new method for this script; to return a result for json_logfile
def overwriteFile(path, filecontents, logkey='')
Mcmlln::Tools.overwriteFile(path, filecontents)
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
## wrapping Bkmkr::Tools.runnode in a new method for this script; to return a result for json_logfile
def localRunNode(jsfile, htmlfile, logkey='')
Bkmkr::Tools.runnode(jsfile, htmlfile)
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
def cdToProjectTmp(logkey='')
FileUtils.cd(Bkmkr::Paths.project_tmp_dir)
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
## wrapping Bkmkr::Tools.processxsl in a new method for this script; to return a result for json_logfile
def processxsl_epubmaker(epub_tmp_html, epub_xsl, tmp_epub, logkey='')
Bkmkr::Tools.processxsl(epub_tmp_html, epub_xsl, tmp_epub, '')
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
# fix cover.html doctype
def firstCoverEdit(file, logkey='')
covercontents = File.read(file).gsub(/<!DOCTYPE html>/,"<!DOCTYPE html>").gsub(/<body>/,"<body><h1 class=\"Nonprinting\">Cover</h1>")
return covercontents
rescue => logstring
return ''
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
# fix cover ncx entry
def firstNCXEdit(file, logkey='')
ncxcontents = File.read(file).gsub(/<text\/><\/navLabel><content src="\#bookcover01"\/>/,"<text>Cover</text></navLabel><content src=\"cover.xhtml\"/>")
return ncxcontents
rescue => logstring
return ''
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
# fix author info in opf
def firstOPFEdit(file, isbn, logkey='')
opfcontents = File.read(file).gsub(/<dc:creator/,"<dc:identifier id=\"isbn\">#{isbn}</dc:identifier><dc:creator id=\"creator\"")
return opfcontents
rescue => logstring
return ''
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
def convertCoverImg(file, logkey='')
unless Bkmkr::Tools.processimages == "false"
`convert "#{file}" -colorspace RGB -resize "600x800>" "#{file}"`
else
logstring = 'processimages is set to false in bookmaker config.rb, skipping'
end
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
## wrapping a Mcmlln::Tools method in a new method for this script; to return a result for json_logfile
def copyFile_epubmaker(source, dest, logkey='')
Mcmlln::Tools.copyFile(source, dest)
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
## wrapping a Mcmlln::Tools method in a new method for this script; to return a result for json_logfile
def getFilesinDir(path, logkey='')
files = Mcmlln::Tools.dirList(path)
return files
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
## wrapping a Mcmlln::Tools method in a new method for this script; to return a result for json_logfile
def makeEpubImgsDir(path, logkey='')
unless Dir.exist?(path)
Mcmlln::Tools.makeDir(path)
else
logstring = 'n-a'
end
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
## wrapping a Mcmlln::Tools method in a new method for this script; to return a result for json_logfile
def copyImgFiles(source, dest, logkey='')
Mcmlln::Tools.copyAllFiles(source,dest)
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
def convertInteriorImg(file, dir, logkey='')
path_to_i = File.join(dir, file)
myres = `identify -format "%y" "#{path_to_i}"`
myres = myres.to_f
if file.include?("_crop")
`convert "#{path_to_i}" -colorspace RGB -density #{myres} -bordercolor white -border 1x1 -trim -resize "600x800>" -quality 100 "#{path_to_i}"`
else
`convert "#{path_to_i}" -colorspace RGB -density #{myres} -resize "600x800>" -quality 100 "#{path_to_i}"`
end
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
def convertInteriorImgs(epub_img_dir, logkey='')
unless Bkmkr::Tools.processimages == "false"
images = getFilesinDir(epub_img_dir, 'get_epub_img_list')
images.each do |i|
convertInteriorImg(i, epub_img_dir)
end
else
logstring = 'processimages is set to false in bookmaker config.rb, skipping'
end
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
def copyInteriorImg(dir, opf, dest, logkey='')
images = Mcmlln::Tools.dirListFiles(dir)
opfcontents = File.read(opf)
copied = []
images.each do |i|
path_to_i = File.join(dir, i)
if opfcontents.include? "\"#{i}"
Mcmlln::Tools.copyFile(path_to_i, dest)
copied << i
end
end
return copied
rescue => logstring
return ''
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
## wrapping a Mcmlln::Tools method in a new method for this script; to return a result for json_logfile
def moveFileToDoneFolder(file, dest, logkey='')
Mcmlln::Tools.moveFile(file, dest)
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
## wrapping Bkmkr::Tools.runpython in a new method for this script; to return a result for json_logfile
def zipEpub(script, args, logkey='')
Bkmkr::Tools.runpython(script, args)
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
## wrapping a Mcmlln::Tools method in a new method for this script; to return a result for json_logfile
def deleteTmpEpub(file, logkey='')
Mcmlln::Tools.deleteFile(file)
rescue => logstring
ensure
Mcmlln::Tools.logtoJson(@log_hash, logkey, logstring)
end
# ---------------------- PROCESSES
data_hash = readConfigJson('read_config_json')
##### local definition(s) based on data from config.json
cover = data_hash['frontcover']
stage_dir = data_hash['stage']
project_name = data_hash['project']
# the path to the cover file
unless data_hash['frontcover'].nil? or data_hash['frontcover'].empty? or !data_hash['frontcover']
final_cover = File.join(Metadata.final_dir, "cover", cover)
else
final_cover = ""
end
# delete existing epub dirs
deleteOld(oebps_dir, 'delete_old_OEBPS')
deleteOld(metainf_dir, 'delete_old_METAINF')
# run method: firstHTMLEdit
if File.file?(epub_tmp_html)
filecontents = firstHTMLEdit(epub_tmp_html, 'first_html_edit--epub_tmp_html')
else
filecontents = firstHTMLEdit(Bkmkr::Paths.outputtmp_html, 'first_html_edit--outputtmp_html')
end
# run method: makeEllipsesNonbreaking
filecontents = makeEllipsesNonbreaking(filecontents, 'make_ellipses_nonbreaking')
overwriteFile(epub_tmp_html, filecontents, 'overwrite_epubtmp_html')
# run node method: add cover placeholder to html file
if !final_cover.nil? and File.file?(final_cover)
localRunNode(cover_placeholder_js, epub_tmp_html, 'cover_placeholder_js')
end
localRunNode(strip_tocnodes_js, epub_tmp_html, 'strip_tocnodes_js')
# convert to epub!
processxsl_epubmaker(epub_tmp_html, epub_xsl, tmp_epub, 'process_xsl')
# run method: firstCoverEdit
# run method: firstNCXEdit
# add cover image file to epub folder
# run method: convertCoverImg
if !final_cover.nil? and File.file?(final_cover)
@log_hash['final_cover_present'] = true
covercontents = firstCoverEdit(cover_html, 'first_cover_edit')
overwriteFile(cover_html, covercontents, 'overwrite_cover_html')
ncxcontents = firstNCXEdit(toc_ncx, 'first_ncx_edit')
overwriteFile(toc_ncx, ncxcontents, 'overwrite_toc_ncx')
copyFile_epubmaker(final_cover, cover_jpg, 'copy_final_cover_file')
convertCoverImg(cover_jpg, 'process_cover_file')
else
@log_hash['final_cover_present'] = false
end
# run method: firstOPFEdit, checking first to see if we are using pisbn or eisbn:
if stage_dir.include?("egalley") || stage_dir.include?("galley") || stage_dir.include?("firstpass") || \
(project_name == 'validator' && stage_dir == 'direct')
opfcontents = firstOPFEdit(content_opf, Metadata.pisbn, 'first_OPF_Edit')
else
opfcontents = firstOPFEdit(content_opf, Metadata.eisbn, 'first_OPF_Edit')
end
overwriteFile(content_opf, opfcontents, 'overwrite_content_opf')
# add epub css to epub folder
copyFile_epubmaker(epub_css, oebps_dir, 'copy_epub_css_to_OEBPSdir')
# add image files to epub folder
sourceimages = getFilesinDir(img_dir, 'get_image_list')
# using imgmagick to optimize image sizes for epub
# run method: convertInteriorImg
if sourceimages.any?
makeEpubImgsDir(epub_img_dir, 'mkdir-epub_images')
copyImgFiles(Bkmkr::Paths.project_tmp_dir_img, epub_img_dir, 'copy_img_files')
#this method checks for pis and loops through relevant imgs for method convertInteriorImg
convertInteriorImgs(epub_img_dir, 'convert_interior_imgs')
epubimages = copyInteriorImg(epub_img_dir, content_opf, oebps_dir, 'copy_interior_imgs')
puts epubimages
@log_hash['interior_img_list'] = epubimages
end
# zip epub
zipEpub(zipepub_py, "#{csfilename}.epub #{Bkmkr::Paths.project_tmp_dir}", 'zip_epub')
# move epub into archive folder
moveFileToDoneFolder(final_epub, final_dir, 'mv_epub_to_done_folder')
# remove temp epub file
deleteTmpEpub(tmp_epub2, 'rm_tmp_epub_file')
# ---------------------- LOGGING
# epub file should exist in done dir
if File.file?("#{Metadata.final_dir}/#{csfilename}.epub")
test_epub_status = "pass: the EPUB was created successfully"
else
test_epub_status = "FAIL: the EPUB was created successfully"
end
@log_hash['ebook_ISBN']=Metadata.eisbn
@log_hash['test_epub_status'] = test_epub_status
# Write json log:
Mcmlln::Tools.logtoJson(@log_hash, 'completed', Time.now)
Mcmlln::Tools.write_json(local_log_hash, Bkmkr::Paths.json_log)