asciidoctor-epub3 1.5.0.alpha.9 → 1.5.0.alpha.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +4 -4
- data/CHANGELOG.adoc +13 -0
- data/Gemfile +11 -8
- data/README.adoc +8 -6
- data/Rakefile +4 -80
- data/asciidoctor-epub3.gemspec +9 -5
- data/bin/adb-push-ebook +4 -3
- data/bin/asciidoctor-epub3 +4 -3
- data/lib/asciidoctor-epub3.rb +2 -0
- data/lib/asciidoctor-epub3/converter.rb +748 -760
- data/lib/asciidoctor-epub3/core_ext/string.rb +2 -0
- data/lib/asciidoctor-epub3/font_icon_map.rb +376 -374
- data/lib/asciidoctor-epub3/packager.rb +507 -512
- data/lib/asciidoctor-epub3/spine_item_processor.rb +77 -75
- data/lib/asciidoctor-epub3/version.rb +5 -3
- metadata +56 -14
@@ -1,170 +1,168 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'core_ext/string'
|
2
4
|
autoload :FileUtils, 'fileutils'
|
3
5
|
autoload :Open3, 'open3'
|
4
6
|
autoload :Shellwords, 'shellwords'
|
5
7
|
|
6
8
|
module Asciidoctor
|
7
|
-
module Epub3
|
8
|
-
module GepubBuilderMixin
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
# FIXME move to Asciidoctor::Helpers
|
31
|
-
def sanitize_xml content, content_spec
|
32
|
-
if content_spec != :pcdata && (content.include? '<')
|
33
|
-
if (content = (content.gsub XmlElementRx, '').strip).include? ' '
|
34
|
-
content = content.tr_s ' ', ' '
|
9
|
+
module Epub3
|
10
|
+
module GepubBuilderMixin
|
11
|
+
DATA_DIR = ::File.expand_path ::File.join(::File.dirname(__FILE__), '..', '..', 'data')
|
12
|
+
SAMPLES_DIR = ::File.join DATA_DIR, 'samples'
|
13
|
+
LF = ?\n
|
14
|
+
CharEntityRx = ContentConverter::CharEntityRx
|
15
|
+
XmlElementRx = ContentConverter::XmlElementRx
|
16
|
+
FromHtmlSpecialCharsMap = ContentConverter::FromHtmlSpecialCharsMap
|
17
|
+
FromHtmlSpecialCharsRx = ContentConverter::FromHtmlSpecialCharsRx
|
18
|
+
CsvDelimiterRx = /\s*,\s*/
|
19
|
+
DefaultCoverImage = 'images/default-cover.png'
|
20
|
+
ImageMacroRx = /^image::?(.*?)\[(.*?)\]$/
|
21
|
+
ImgSrcScanRx = /<img src="(.+?)"/
|
22
|
+
SvgImgSniffRx = /<img src=".+?\.svg"/
|
23
|
+
|
24
|
+
attr_reader :book, :format, :spine
|
25
|
+
|
26
|
+
# FIXME: move to Asciidoctor::Helpers
|
27
|
+
def sanitize_doctitle_xml doc, content_spec
|
28
|
+
doctitle = doc.header? ? doc.doctitle : (doc.attr 'untitled-label')
|
29
|
+
sanitize_xml doctitle, content_spec
|
35
30
|
end
|
36
|
-
end
|
37
31
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
content = content.gsub(CharEntityRx) { [$1.to_i].pack 'U*' } if content.include? '&#'
|
46
|
-
content = content.gsub FromHtmlSpecialCharsRx, FromHtmlSpecialCharsMap
|
47
|
-
end
|
48
|
-
else
|
49
|
-
raise ::ArgumentError, %(Unknown content spec: #{content_spec})
|
50
|
-
end
|
51
|
-
content
|
52
|
-
end
|
32
|
+
# FIXME: move to Asciidoctor::Helpers
|
33
|
+
def sanitize_xml content, content_spec
|
34
|
+
if content_spec != :pcdata && (content.include? '<')
|
35
|
+
if (content = (content.gsub XmlElementRx, '').strip).include? ' '
|
36
|
+
content = content.tr_s ' ', ' '
|
37
|
+
end
|
38
|
+
end
|
53
39
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
40
|
+
case content_spec
|
41
|
+
when :attribute_cdata
|
42
|
+
content = content.gsub '"', '"' if content.include? '"'
|
43
|
+
when :cdata, :pcdata
|
44
|
+
# noop
|
45
|
+
when :plain_text
|
46
|
+
if content.include? ';'
|
47
|
+
content = content.gsub(CharEntityRx) { [$1.to_i].pack 'U*' } if content.include? '&#'
|
48
|
+
content = content.gsub FromHtmlSpecialCharsRx, FromHtmlSpecialCharsMap
|
49
|
+
end
|
50
|
+
else
|
51
|
+
raise ::ArgumentError, %(Unknown content spec: #{content_spec})
|
52
|
+
end
|
53
|
+
content
|
66
54
|
end
|
67
|
-
else
|
68
|
-
::File.join DATA_DIR, 'styles'
|
69
|
-
end
|
70
55
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
56
|
+
def add_theme_assets doc
|
57
|
+
builder = self
|
58
|
+
format = @format
|
59
|
+
workdir = if doc.attr? 'epub3-stylesdir'
|
60
|
+
stylesdir = doc.attr 'epub3-stylesdir'
|
61
|
+
# FIXME: make this work for Windows paths!!
|
62
|
+
if stylesdir.start_with? '/'
|
63
|
+
stylesdir
|
64
|
+
else
|
65
|
+
docdir = doc.attr 'docdir', '.'
|
66
|
+
docdir = '.' if docdir.empty?
|
67
|
+
::File.join docdir, stylesdir
|
68
|
+
end
|
69
|
+
else
|
70
|
+
::File.join DATA_DIR, 'styles'
|
71
|
+
end
|
72
|
+
|
73
|
+
# TODO: improve design/UX of custom theme functionality, including custom fonts
|
74
|
+
resources do
|
75
|
+
if format == :kf8
|
76
|
+
# NOTE add layer of indirection so Kindle Direct Publishing (KDP) doesn't strip font-related CSS rules
|
77
|
+
file 'styles/epub3.css' => '@import url("epub3-proxied.css");'.to_ios
|
78
|
+
file 'styles/epub3-css3-only.css' => '@import url("epub3-css3-only-proxied.css");'.to_ios
|
79
|
+
file 'styles/epub3-proxied.css' => (builder.postprocess_css_file ::File.join(workdir, 'epub3.css'), format)
|
80
|
+
file 'styles/epub3-css3-only-proxied.css' => (builder.postprocess_css_file ::File.join(workdir, 'epub3-css3-only.css'), format)
|
81
|
+
else
|
82
|
+
file 'styles/epub3.css' => (builder.postprocess_css_file ::File.join(workdir, 'epub3.css'), format)
|
83
|
+
file 'styles/epub3-css3-only.css' => (builder.postprocess_css_file ::File.join(workdir, 'epub3-css3-only.css'), format)
|
84
|
+
end
|
85
|
+
end
|
84
86
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
87
|
+
resources do
|
88
|
+
font_files, font_css = builder.select_fonts ::File.join(DATA_DIR, 'styles/epub3-fonts.css'), (doc.attr 'scripts', 'latin')
|
89
|
+
file 'styles/epub3-fonts.css' => font_css
|
90
|
+
unless font_files.empty?
|
91
|
+
# NOTE metadata property in oepbs package manifest doesn't work; must use proprietary iBooks file instead
|
92
|
+
#(@book.metadata.add_metadata 'meta', 'true')['property'] = 'ibooks:specified-fonts' unless format == :kf8
|
93
|
+
builder.optional_file 'META-INF/com.apple.ibooks.display-options.xml' => '<?xml version="1.0" encoding="UTF-8"?>
|
92
94
|
<display_options>
|
93
95
|
<platform name="*">
|
94
96
|
<option name="specified-fonts">true</option>
|
95
97
|
</platform>
|
96
98
|
</display_options>'.to_ios unless format == :kf8
|
97
99
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
100
|
+
# https://github.com/asciidoctor/asciidoctor-epub3/issues/120
|
101
|
+
#
|
102
|
+
# 'application/x-font-ttf' causes warnings in epubcheck 4.0.2,
|
103
|
+
# "non-standard font type". Discussion:
|
104
|
+
# https://www.mobileread.com/forums/showthread.php?t=231272
|
105
|
+
#
|
106
|
+
# 3.1 spec recommends 'application/font-sfnt', but epubcheck doesn't
|
107
|
+
# implement that yet (warnings). https://idpf.github.io/epub-cmt/v3/
|
108
|
+
#
|
109
|
+
# 3.0 spec recommends 'application/vnd.ms-opentype', this works without
|
110
|
+
# warnings.
|
111
|
+
# http://www.idpf.org/epub/30/spec/epub30-publications.html#sec-core-media-types
|
112
|
+
with_media_type 'application/vnd.ms-opentype' do
|
113
|
+
font_files.each do |font_file|
|
114
|
+
file font_file => ::File.join(DATA_DIR, font_file)
|
115
|
+
end
|
116
|
+
end
|
113
117
|
end
|
114
118
|
end
|
119
|
+
nil
|
115
120
|
end
|
116
|
-
end
|
117
|
-
nil
|
118
|
-
end
|
119
121
|
|
120
|
-
|
121
|
-
|
122
|
-
|
122
|
+
def add_cover_image doc
|
123
|
+
imagesdir = (doc.attr 'imagesdir', '.').chomp '/'
|
124
|
+
imagesdir = (imagesdir == '.' ? nil : %(#{imagesdir}/))
|
123
125
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
126
|
+
if (image_path = doc.attr 'front-cover-image')
|
127
|
+
image_attrs = {}
|
128
|
+
if (image_path.include? ':') && image_path =~ ImageMacroRx
|
129
|
+
warn %(asciidoctor: WARNING: deprecated block macro syntax detected in front-cover-image attribute) if image_path.start_with? 'image::'
|
130
|
+
image_path = %(#{imagesdir}#{$1})
|
131
|
+
(::Asciidoctor::AttributeList.new $2).parse_into image_attrs, %w(alt width height) unless $2.empty?
|
132
|
+
end
|
133
|
+
workdir = (workdir = doc.attr 'docdir').nil_or_empty? ? '.' : workdir
|
134
|
+
if ::File.readable? ::File.join(workdir, image_path)
|
135
|
+
unless !image_attrs.empty? && (width = image_attrs['width']) && (height = image_attrs['height'])
|
136
|
+
width, height = 1050, 1600
|
137
|
+
end
|
138
|
+
else
|
139
|
+
warn %(asciidoctor: ERROR: #{::File.basename doc.attr('docfile')}: front cover image not found or readable: #{::File.expand_path image_path, workdir})
|
140
|
+
image_path = nil
|
141
|
+
end
|
137
142
|
end
|
138
|
-
else
|
139
|
-
warn %(asciidoctor: ERROR: #{::File.basename(doc.attr 'docfile')}: front cover image not found or readable: #{::File.expand_path image_path, workdir})
|
140
|
-
image_path = nil
|
141
|
-
end
|
142
|
-
end
|
143
143
|
|
144
|
-
|
145
|
-
image_path, workdir, width, height = DefaultCoverImage, DATA_DIR, 1050, 1600
|
146
|
-
end
|
144
|
+
image_path, workdir, width, height = DefaultCoverImage, DATA_DIR, 1050, 1600 unless image_path
|
147
145
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
146
|
+
resources do
|
147
|
+
cover_image %(#{imagesdir}jacket/cover#{::File.extname image_path}) => (::File.join workdir, image_path)
|
148
|
+
@last_defined_item.tap do |last_item|
|
149
|
+
last_item['width'] = width
|
150
|
+
last_item['height'] = height
|
151
|
+
end
|
152
|
+
end
|
153
|
+
nil
|
153
154
|
end
|
154
|
-
end
|
155
|
-
nil
|
156
|
-
end
|
157
155
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
156
|
+
# NOTE must be called within the ordered block
|
157
|
+
def add_cover_page doc, spine_builder, manifest
|
158
|
+
cover_item_attrs = manifest.items['item_cover'].instance_variable_get :@attributes
|
159
|
+
href = cover_item_attrs['href']
|
160
|
+
# NOTE we only store width and height temporarily to pass through the values
|
161
|
+
width = cover_item_attrs.delete 'width'
|
162
|
+
height = cover_item_attrs.delete 'height'
|
165
163
|
|
166
|
-
|
167
|
-
|
164
|
+
# NOTE SVG wrapper maintains aspect ratio and confines image to view box
|
165
|
+
content = %(<!DOCTYPE html>
|
168
166
|
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en" lang="en">
|
169
167
|
<head>
|
170
168
|
<meta charset="UTF-8"/>
|
@@ -193,127 +191,125 @@ body > svg {
|
|
193
191
|
<image width="#{width}" height="#{height}" xlink:href="#{href}"/>
|
194
192
|
</svg></body>
|
195
193
|
</html>).to_ios
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
end
|
205
|
-
|
206
|
-
def add_images_from_front_matter
|
207
|
-
(::File.read 'front-matter.html').scan ImgSrcScanRx do
|
208
|
-
resources do
|
209
|
-
file $1
|
194
|
+
# Gitden expects a cover.xhtml, so add it to the spine
|
195
|
+
spine_builder.file 'cover.xhtml' => content
|
196
|
+
assigned_id = (spine_builder.instance_variable_get :@last_defined_item).item.id
|
197
|
+
spine_builder.id 'cover'
|
198
|
+
# clearly a deficiency of gepub that it does not match the id correctly
|
199
|
+
# FIXME can we move this hack elsewhere?
|
200
|
+
@book.spine.itemref_by_id[assigned_id].idref = 'cover'
|
201
|
+
nil
|
210
202
|
end
|
211
|
-
end if ::File.file? 'front-matter.html'
|
212
|
-
nil
|
213
|
-
end
|
214
203
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
204
|
+
def add_images_from_front_matter
|
205
|
+
(::File.read 'front-matter.html').scan ImgSrcScanRx do
|
206
|
+
resources do
|
207
|
+
file $1
|
208
|
+
end
|
209
|
+
end if ::File.file? 'front-matter.html'
|
210
|
+
nil
|
221
211
|
end
|
222
|
-
end
|
223
|
-
nil
|
224
|
-
end
|
225
212
|
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
resources workdir: workdir do
|
232
|
-
images.each do |image|
|
233
|
-
if (image_path = image[:path]).start_with? %(#{docimagesdir}jacket/cover.)
|
234
|
-
warn %(asciidoctor: WARNING: image path is reserved for cover artwork: #{image_path}; skipping image found in content)
|
235
|
-
elsif ::File.readable? image_path
|
236
|
-
file image_path
|
237
|
-
else
|
238
|
-
warn %(asciidoctor: ERROR: #{::File.basename image[:docfile]}: image not found or not readable: #{::File.expand_path image_path, workdir})
|
213
|
+
def add_front_matter_page _doc, spine_builder
|
214
|
+
if ::File.file? 'front-matter.html'
|
215
|
+
front_matter_content = ::File.read 'front-matter.html'
|
216
|
+
spine_builder.file 'front-matter.xhtml' => (postprocess_xhtml front_matter_content, @format)
|
217
|
+
spine_builder.add_property 'svg' unless (spine_builder.property? 'svg') || SvgImgSniffRx !~ front_matter_content
|
239
218
|
end
|
219
|
+
nil
|
240
220
|
end
|
241
|
-
end
|
242
|
-
nil
|
243
|
-
end
|
244
221
|
|
245
|
-
|
246
|
-
|
247
|
-
|
222
|
+
def add_content_images doc, images
|
223
|
+
docimagesdir = (doc.attr 'imagesdir', '.').chomp '/'
|
224
|
+
docimagesdir = (docimagesdir == '.' ? nil : %(#{docimagesdir}/))
|
225
|
+
|
226
|
+
workdir = (workdir = doc.attr 'docdir').nil_or_empty? ? '.' : workdir
|
227
|
+
resources workdir: workdir do
|
228
|
+
images.each do |image|
|
229
|
+
if (image_path = image[:path]).start_with? %(#{docimagesdir}jacket/cover.)
|
230
|
+
warn %(asciidoctor: WARNING: image path is reserved for cover artwork: #{image_path}; skipping image found in content)
|
231
|
+
elsif ::File.readable? image_path
|
232
|
+
file image_path
|
233
|
+
else
|
234
|
+
warn %(asciidoctor: ERROR: #{::File.basename image[:docfile]}: image not found or not readable: #{::File.expand_path image_path, workdir})
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
nil
|
239
|
+
end
|
248
240
|
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
end
|
241
|
+
def add_profile_images doc, usernames
|
242
|
+
imagesdir = (doc.attr 'imagesdir', '.').chomp '/'
|
243
|
+
imagesdir = (imagesdir == '.' ? nil : %(#{imagesdir}/))
|
253
244
|
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
avatar = %(#{imagesdir}avatars/#{username}.jpg)
|
258
|
-
if ::File.readable?(resolved_avatar = ::File.join(workdir, avatar))
|
259
|
-
file avatar => resolved_avatar
|
260
|
-
else
|
261
|
-
warn %(asciidoctor: ERROR: avatar for #{username} not found or readable: #{avatar}; falling back to default avatar)
|
262
|
-
file avatar => ::File.join(DATA_DIR, 'images/default-avatar.jpg')
|
245
|
+
resources do
|
246
|
+
file %(#{imagesdir}avatars/default.jpg) => ::File.join(DATA_DIR, 'images/default-avatar.jpg')
|
247
|
+
file %(#{imagesdir}headshots/default.jpg) => ::File.join(DATA_DIR, 'images/default-headshot.jpg')
|
263
248
|
end
|
264
249
|
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
250
|
+
workdir = (workdir = doc.attr 'docdir').nil_or_empty? ? '.' : workdir
|
251
|
+
resources do
|
252
|
+
usernames.each do |username|
|
253
|
+
avatar = %(#{imagesdir}avatars/#{username}.jpg)
|
254
|
+
if ::File.readable? (resolved_avatar = (::File.join workdir, avatar))
|
255
|
+
file avatar => resolved_avatar
|
256
|
+
else
|
257
|
+
warn %(asciidoctor: ERROR: avatar for #{username} not found or readable: #{avatar}; falling back to default avatar)
|
258
|
+
file avatar => ::File.join(DATA_DIR, 'images/default-avatar.jpg')
|
259
|
+
end
|
260
|
+
|
261
|
+
headshot = %(#{imagesdir}headshots/#{username}.jpg)
|
262
|
+
if ::File.readable? (resolved_headshot = (::File.join workdir, headshot))
|
263
|
+
file headshot => resolved_headshot
|
264
|
+
elsif doc.attr? 'builder', 'editions'
|
265
|
+
warn %(asciidoctor: ERROR: headshot for #{username} not found or readable: #{headshot}; falling back to default headshot)
|
266
|
+
file headshot => ::File.join(DATA_DIR, 'images/default-headshot.jpg')
|
267
|
+
end
|
268
|
+
end
|
271
269
|
end
|
270
|
+
nil
|
272
271
|
end
|
273
|
-
end
|
274
|
-
nil
|
275
|
-
end
|
276
272
|
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
273
|
+
def add_content doc
|
274
|
+
builder, spine, format, images = self, @spine, @format, {}
|
275
|
+
workdir = (doc.attr 'docdir').nil_or_empty? ? '.' : workdir
|
276
|
+
resources workdir: workdir do
|
277
|
+
extend GepubResourceBuilderMixin
|
278
|
+
builder.add_images_from_front_matter
|
279
|
+
builder.add_nav_doc doc, self, spine, format
|
280
|
+
builder.add_ncx_doc doc, self, spine
|
281
|
+
ordered do
|
282
|
+
builder.add_cover_page doc, self, @book.manifest unless format == :kf8
|
283
|
+
builder.add_front_matter_page doc, self
|
284
|
+
spine.each_with_index do |item, _i|
|
285
|
+
docfile = item.attr 'docfile'
|
286
|
+
imagesdir = (item.attr 'imagesdir', '.').chomp '/'
|
287
|
+
imagesdir = (imagesdir == '.' ? '' : %(#{imagesdir}/))
|
288
|
+
file %(#{item.id || (item.attr 'docname')}.xhtml) => (builder.postprocess_xhtml item.convert, format)
|
289
|
+
add_property 'svg' if ((item.attr 'epub-properties') || []).include? 'svg'
|
290
|
+
# QUESTION should we pass the document itself?
|
291
|
+
item.references[:images].each do |target|
|
292
|
+
images[image_path = %(#{imagesdir}#{target})] ||= { docfile: docfile, path: image_path }
|
293
|
+
end
|
294
|
+
# QUESTION reenable?
|
295
|
+
#linear 'yes' if i == 0
|
296
|
+
end
|
297
297
|
end
|
298
|
-
# QUESTION reenable?
|
299
|
-
#linear 'yes' if i == 0
|
300
298
|
end
|
299
|
+
add_content_images doc, images.values
|
300
|
+
nil
|
301
301
|
end
|
302
|
-
end
|
303
|
-
add_content_images doc, images.values
|
304
|
-
nil
|
305
|
-
end
|
306
302
|
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
303
|
+
def add_nav_doc doc, spine_builder, spine, format
|
304
|
+
spine_builder.nav 'nav.xhtml' => (postprocess_xhtml nav_doc(doc, spine), format)
|
305
|
+
spine_builder.id 'nav'
|
306
|
+
nil
|
307
|
+
end
|
312
308
|
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="#{lang =
|
309
|
+
# TODO: aggregate authors of spine document into authors attribute(s) on main document
|
310
|
+
def nav_doc doc, spine
|
311
|
+
lines = [%(<!DOCTYPE html>
|
312
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="#{lang = doc.attr 'lang', 'en'}" lang="#{lang}">
|
317
313
|
<head>
|
318
314
|
<meta charset="UTF-8"/>
|
319
315
|
<title>#{sanitize_doctitle_xml doc, :cdata}</title>
|
@@ -324,49 +320,49 @@ body > svg {
|
|
324
320
|
<h1>#{sanitize_doctitle_xml doc, :pcdata}</h1>
|
325
321
|
<nav epub:type="toc" id="toc">
|
326
322
|
<h2>#{doc.attr 'toc-title'}</h2>)]
|
327
|
-
|
328
|
-
|
323
|
+
lines << (nav_level spine, [(doc.attr 'toclevels', 1).to_i, 0].max)
|
324
|
+
lines << %(</nav>
|
329
325
|
</body>
|
330
326
|
</html>)
|
331
|
-
|
332
|
-
end
|
333
|
-
|
334
|
-
def nav_level items, depth, state = {}
|
335
|
-
lines = []
|
336
|
-
lines << '<ol>'
|
337
|
-
items.each do |item|
|
338
|
-
#index = (state[:index] = (state.fetch :index, 0) + 1)
|
339
|
-
if item.context == :document
|
340
|
-
# NOTE we sanitize the chapter titles because we use formatting to control layout
|
341
|
-
item_label = sanitize_doctitle_xml item, :cdata
|
342
|
-
item_href = (state[:content_doc_href] = %(#{item.id || (item.attr 'docname')}.xhtml))
|
343
|
-
else
|
344
|
-
item_label = sanitize_xml item.title, :pcdata
|
345
|
-
item_href = %(#{state[:content_doc_href]}##{item.id})
|
327
|
+
lines * LF
|
346
328
|
end
|
347
|
-
|
348
|
-
|
349
|
-
lines
|
350
|
-
lines << '
|
351
|
-
|
352
|
-
|
329
|
+
|
330
|
+
def nav_level items, depth, state = {}
|
331
|
+
lines = []
|
332
|
+
lines << '<ol>'
|
333
|
+
items.each do |item|
|
334
|
+
#index = (state[:index] = (state.fetch :index, 0) + 1)
|
335
|
+
if item.context == :document
|
336
|
+
# NOTE we sanitize the chapter titles because we use formatting to control layout
|
337
|
+
item_label = sanitize_doctitle_xml item, :cdata
|
338
|
+
item_href = (state[:content_doc_href] = %(#{item.id || (item.attr 'docname')}.xhtml))
|
339
|
+
else
|
340
|
+
item_label = sanitize_xml item.title, :pcdata
|
341
|
+
item_href = %(#{state[:content_doc_href]}##{item.id})
|
342
|
+
end
|
343
|
+
lines << %(<li><a href="#{item_href}">#{item_label}</a>)
|
344
|
+
if depth == 0 || (child_sections = item.sections).empty?
|
345
|
+
lines[-1] = %(#{lines[-1]}</li>)
|
346
|
+
else
|
347
|
+
lines << (nav_level child_sections, depth - 1, state)
|
348
|
+
lines << '</li>'
|
349
|
+
end
|
350
|
+
state.delete :content_doc_href if item.context == :document
|
351
|
+
end
|
352
|
+
lines << '</ol>'
|
353
|
+
lines * LF
|
353
354
|
end
|
354
|
-
state.delete :content_doc_href if item.context == :document
|
355
|
-
end
|
356
|
-
lines << '</ol>'
|
357
|
-
lines * LF
|
358
|
-
end
|
359
355
|
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
356
|
+
# NOTE gepub doesn't support building a ncx TOC with depth > 1, so do it ourselves
|
357
|
+
def add_ncx_doc doc, spine_builder, spine
|
358
|
+
spine_builder.file 'toc.ncx' => (ncx_doc doc, spine).to_ios
|
359
|
+
spine_builder.id 'ncx'
|
360
|
+
nil
|
361
|
+
end
|
366
362
|
|
367
|
-
|
368
|
-
|
369
|
-
|
363
|
+
def ncx_doc doc, spine
|
364
|
+
# TODO: populate docAuthor element based on unique authors in work
|
365
|
+
lines = [%(<?xml version="1.0" encoding="utf-8"?>
|
370
366
|
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1" xml:lang="#{doc.attr 'lang', 'en'}">
|
371
367
|
<head>
|
372
368
|
<meta name="dtb:uid" content="#{@book.identifier}"/>
|
@@ -376,278 +372,277 @@ body > svg {
|
|
376
372
|
</head>
|
377
373
|
<docTitle><text>#{sanitize_doctitle_xml doc, :cdata}</text></docTitle>
|
378
374
|
<navMap>)]
|
379
|
-
|
380
|
-
|
381
|
-
|
375
|
+
lines << (ncx_level spine, [(doc.attr 'toclevels', 1).to_i, 0].max, state = {})
|
376
|
+
lines[0] = lines[0].sub '%{depth}', %(<meta name="dtb:depth" content="#{state[:max_depth]}"/>)
|
377
|
+
lines << %(</navMap>
|
382
378
|
</ncx>)
|
383
|
-
|
384
|
-
end
|
385
|
-
|
386
|
-
def ncx_level items, depth, state = {}
|
387
|
-
lines = []
|
388
|
-
state[:max_depth] = (state.fetch :max_depth, 0) + 1
|
389
|
-
items.each do |item|
|
390
|
-
index = (state[:index] = (state.fetch :index, 0) + 1)
|
391
|
-
if item.context == :document
|
392
|
-
item_id = %(nav_#{index})
|
393
|
-
item_label = sanitize_doctitle_xml item, :cdata
|
394
|
-
item_href = (state[:content_doc_href] = %(#{item.id || (item.attr 'docname')}.xhtml))
|
395
|
-
else
|
396
|
-
item_id = %(nav_#{index})
|
397
|
-
item_label = sanitize_xml item.title, :cdata
|
398
|
-
item_href = %(#{state[:content_doc_href]}##{item.id})
|
379
|
+
lines * LF
|
399
380
|
end
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
381
|
+
|
382
|
+
def ncx_level items, depth, state = {}
|
383
|
+
lines = []
|
384
|
+
state[:max_depth] = (state.fetch :max_depth, 0) + 1
|
385
|
+
items.each do |item|
|
386
|
+
index = (state[:index] = (state.fetch :index, 0) + 1)
|
387
|
+
item_id = %(nav_#{index})
|
388
|
+
if item.context == :document
|
389
|
+
item_label = sanitize_doctitle_xml item, :cdata
|
390
|
+
item_href = (state[:content_doc_href] = %(#{item.id || (item.attr 'docname')}.xhtml))
|
391
|
+
else
|
392
|
+
item_label = sanitize_xml item.title, :cdata
|
393
|
+
item_href = %(#{state[:content_doc_href]}##{item.id})
|
394
|
+
end
|
395
|
+
lines << %(<navPoint id="#{item_id}" playOrder="#{index}">)
|
396
|
+
lines << %(<navLabel><text>#{item_label}</text></navLabel>)
|
397
|
+
lines << %(<content src="#{item_href}"/>)
|
398
|
+
unless depth == 0 || (child_sections = item.sections).empty?
|
399
|
+
lines << (ncx_level child_sections, depth - 1, state)
|
400
|
+
end
|
401
|
+
lines << %(</navPoint>)
|
402
|
+
state.delete :content_doc_href if item.context == :document
|
403
|
+
end
|
404
|
+
lines * LF
|
405
405
|
end
|
406
|
-
lines << %(</navPoint>)
|
407
|
-
state.delete :content_doc_href if item.context == :document
|
408
|
-
end
|
409
|
-
lines * LF
|
410
|
-
end
|
411
406
|
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
407
|
+
def collect_keywords doc, spine
|
408
|
+
([doc] + spine).map {|item|
|
409
|
+
if item.attr? 'keywords'
|
410
|
+
(item.attr 'keywords').split CsvDelimiterRx
|
411
|
+
else
|
412
|
+
[]
|
413
|
+
end
|
414
|
+
}.flatten.uniq
|
418
415
|
end
|
419
|
-
end.flatten.uniq
|
420
|
-
end
|
421
416
|
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
417
|
+
# Swap fonts in CSS based on the value of the document attribute 'scripts',
|
418
|
+
# then return the list of fonts as well as the font CSS.
|
419
|
+
def select_fonts filename, scripts = 'latin'
|
420
|
+
font_css = ::File.read filename
|
421
|
+
font_css = font_css.gsub(/(?<=-)latin(?=\.ttf\))/, scripts) unless scripts == 'latin'
|
427
422
|
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
423
|
+
# match CSS font urls in the forms of:
|
424
|
+
# src: url(../fonts/notoserif-regular-latin.ttf);
|
425
|
+
# src: url(../fonts/notoserif-regular-latin.ttf) format("truetype");
|
426
|
+
font_list = font_css.scan(/url\(\.\.\/([^)]+\.ttf)\)/).flatten
|
432
427
|
|
433
|
-
|
434
|
-
|
428
|
+
[font_list, font_css.to_ios]
|
429
|
+
end
|
435
430
|
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
431
|
+
def postprocess_css_file filename, format
|
432
|
+
return filename unless format == :kf8
|
433
|
+
postprocess_css ::File.read(filename), format
|
434
|
+
end
|
440
435
|
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
436
|
+
def postprocess_css content, format
|
437
|
+
return content.to_ios unless format == :kf8
|
438
|
+
# TODO: convert regular expressions to constants
|
439
|
+
content
|
440
|
+
.gsub(/^ -webkit-column-break-.*\n/, '')
|
441
|
+
.gsub(/^ max-width: .*\n/, '')
|
442
|
+
.to_ios
|
443
|
+
end
|
449
444
|
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
445
|
+
def postprocess_xhtml_file filename, format
|
446
|
+
return filename unless format == :kf8
|
447
|
+
postprocess_xhtml ::File.read(filename), format
|
448
|
+
end
|
454
449
|
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
end
|
450
|
+
# NOTE Kindle requires that
|
451
|
+
# <meta charset="utf-8"/>
|
452
|
+
# be converted to
|
453
|
+
# <meta http-equiv="Content-Type" content="application/xml+xhtml; charset=UTF-8"/>
|
454
|
+
def postprocess_xhtml content, format
|
455
|
+
return content.to_ios unless format == :kf8
|
456
|
+
# TODO: convert regular expressions to constants
|
457
|
+
content
|
458
|
+
.gsub(/<meta charset="(.+?)"\/>/, '<meta http-equiv="Content-Type" content="application/xml+xhtml; charset=\1"/>')
|
459
|
+
.gsub(/<img([^>]+) style="width: (\d\d)%;"/, '<img\1 style="width: \2%; height: \2%;"')
|
460
|
+
.gsub(/<script type="text\/javascript">.*?<\/script>\n?/m, '')
|
461
|
+
.to_ios
|
462
|
+
end
|
463
|
+
end
|
469
464
|
|
470
|
-
module GepubResourceBuilderMixin
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
465
|
+
module GepubResourceBuilderMixin
|
466
|
+
# Add missing method to builder to add a property to last defined item
|
467
|
+
def add_property property
|
468
|
+
@last_defined_item.add_property property
|
469
|
+
end
|
475
470
|
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
end
|
471
|
+
# Add helper method to builder to check if property is set on last defined item
|
472
|
+
def property? property
|
473
|
+
(@last_defined_item['properties'] || []).include? property
|
474
|
+
end
|
475
|
+
end
|
481
476
|
|
482
|
-
class Packager
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
477
|
+
class Packager
|
478
|
+
KINDLEGEN = ENV['KINDLEGEN'] || 'kindlegen'
|
479
|
+
EPUBCHECK = ENV['EPUBCHECK'] || %(epubcheck#{::Gem.win_platform? ? '.bat' : '.sh'})
|
480
|
+
EpubExtensionRx = /\.epub$/i
|
481
|
+
KindlegenCompression = ::Hash['0', '-c0', '1', '-c1', '2', '-c2', 'none', '-c0', 'standard', '-c1', 'huffdic', '-c2']
|
487
482
|
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
483
|
+
def initialize spine_doc, spine, format = :epub3, _options = {}
|
484
|
+
@document = spine_doc
|
485
|
+
@spine = spine || []
|
486
|
+
@format = format
|
487
|
+
end
|
493
488
|
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
# FIXME authors should be aggregated already on parent document
|
502
|
-
authors = if doc.attr? 'authors'
|
503
|
-
(doc.attr 'authors').split(GepubBuilderMixin::CsvDelimiterRx).concat(spine.map {|item| item.attr 'author' }.compact).uniq
|
504
|
-
else
|
505
|
-
[]
|
506
|
-
end
|
489
|
+
def package options = {}
|
490
|
+
doc = @document
|
491
|
+
spine = @spine
|
492
|
+
fmt = @format
|
493
|
+
target = options[:target]
|
494
|
+
dest = File.dirname target
|
507
495
|
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
496
|
+
# FIXME: authors should be aggregated already on parent document
|
497
|
+
if doc.attr? 'authors'
|
498
|
+
authors = (doc.attr 'authors').split(GepubBuilderMixin::CsvDelimiterRx).concat(spine.map {|item| item.attr 'author' }.compact).uniq
|
499
|
+
else
|
500
|
+
authors = []
|
501
|
+
end
|
514
502
|
|
515
|
-
|
516
|
-
|
503
|
+
builder = ::GEPUB::Builder.new do
|
504
|
+
extend GepubBuilderMixin
|
505
|
+
@document = doc
|
506
|
+
@spine = spine
|
507
|
+
@format = fmt
|
508
|
+
@book.epub_backward_compat = fmt != :kf8
|
517
509
|
|
518
|
-
|
519
|
-
|
520
|
-
else
|
521
|
-
unique_identifier doc.id, 'pub-identifier', 'uuid'
|
522
|
-
end
|
523
|
-
# replace with next line once the attributes argument is supported
|
524
|
-
#unique_identifier doc.id, 'pub-id', 'uuid', 'scheme' => 'xsd:string'
|
525
|
-
|
526
|
-
# NOTE we must use :plain_text here since gepub reencodes
|
527
|
-
title(sanitize_doctitle_xml doc, :plain_text)
|
528
|
-
id 'pub-title'
|
529
|
-
|
530
|
-
# FIXME this logic needs some work
|
531
|
-
if doc.attr? 'publisher'
|
532
|
-
publisher(publisher_name = doc.attr('publisher'))
|
533
|
-
# marc role: Book producer (see http://www.loc.gov/marc/relators/relaterm.html)
|
534
|
-
creator doc.attr('producer', publisher_name), 'bkp'
|
535
|
-
else
|
536
|
-
# NOTE Use producer as both publisher and producer if publisher isn't specified
|
537
|
-
if doc.attr? 'producer'
|
538
|
-
producer_name = doc.attr 'producer'
|
539
|
-
publisher producer_name
|
540
|
-
# marc role: Book producer (see http://www.loc.gov/marc/relators/relaterm.html)
|
541
|
-
creator producer_name, 'bkp'
|
542
|
-
# NOTE Use author as creator if both publisher or producer are absent
|
543
|
-
elsif doc.attr? 'author'
|
544
|
-
# marc role: Author (see http://www.loc.gov/marc/relators/relaterm.html)
|
545
|
-
creator doc.attr('author'), 'aut'
|
546
|
-
end
|
547
|
-
end
|
510
|
+
language doc.attr('lang', 'en')
|
511
|
+
id 'pub-language'
|
548
512
|
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
513
|
+
if doc.attr? 'uuid'
|
514
|
+
unique_identifier doc.attr('uuid'), 'pub-identifier', 'uuid'
|
515
|
+
else
|
516
|
+
unique_identifier doc.id, 'pub-identifier', 'uuid'
|
517
|
+
end
|
518
|
+
# replace with next line once the attributes argument is supported
|
519
|
+
#unique_identifier doc.id, 'pub-id', 'uuid', 'scheme' => 'xsd:string'
|
520
|
+
|
521
|
+
# NOTE we must use :plain_text here since gepub reencodes
|
522
|
+
title sanitize_doctitle_xml(doc, :plain_text)
|
523
|
+
id 'pub-title'
|
524
|
+
|
525
|
+
# FIXME: this logic needs some work
|
526
|
+
if doc.attr? 'publisher'
|
527
|
+
publisher (publisher_name = (doc.attr 'publisher'))
|
528
|
+
# marc role: Book producer (see http://www.loc.gov/marc/relators/relaterm.html)
|
529
|
+
creator (doc.attr 'producer', publisher_name), 'bkp'
|
530
|
+
elsif doc.attr? 'producer'
|
531
|
+
# NOTE Use producer as both publisher and producer if publisher isn't specified
|
532
|
+
producer_name = doc.attr 'producer'
|
533
|
+
publisher producer_name
|
534
|
+
# marc role: Book producer (see http://www.loc.gov/marc/relators/relaterm.html)
|
535
|
+
creator producer_name, 'bkp'
|
536
|
+
elsif doc.attr? 'author'
|
537
|
+
# NOTE Use author as creator if both publisher or producer are absent
|
538
|
+
# marc role: Author (see http://www.loc.gov/marc/relators/relaterm.html)
|
539
|
+
creator doc.attr('author'), 'aut'
|
540
|
+
end
|
557
541
|
|
558
|
-
|
559
|
-
|
542
|
+
if doc.attr? 'creator'
|
543
|
+
# marc role: Creator (see http://www.loc.gov/marc/relators/relaterm.html)
|
544
|
+
creator doc.attr('creator'), 'cre'
|
545
|
+
else
|
546
|
+
# marc role: Manufacturer (see http://www.loc.gov/marc/relators/relaterm.html)
|
547
|
+
# QUESTION should this be bkp?
|
548
|
+
creator 'Asciidoctor', 'mfr'
|
549
|
+
end
|
560
550
|
|
561
|
-
|
562
|
-
|
563
|
-
date(doc.attr 'revdate')
|
564
|
-
else
|
565
|
-
date ::Time.now.strftime('%Y-%m-%dT%H:%M:%SZ')
|
566
|
-
end
|
551
|
+
# TODO: getting author list should be a method on Asciidoctor API
|
552
|
+
contributors(*authors)
|
567
553
|
|
568
|
-
|
569
|
-
|
570
|
-
|
554
|
+
if doc.attr? 'revdate'
|
555
|
+
# TODO: ensure this is a real date
|
556
|
+
date doc.attr('revdate')
|
557
|
+
else
|
558
|
+
date ::Time.now.strftime('%Y-%m-%dT%H:%M:%SZ')
|
559
|
+
end
|
571
560
|
|
572
|
-
|
573
|
-
subject s
|
574
|
-
end
|
561
|
+
description doc.attr('description') if doc.attr? 'description'
|
575
562
|
|
576
|
-
|
577
|
-
|
578
|
-
|
563
|
+
(collect_keywords doc, spine).each do |s|
|
564
|
+
subject s
|
565
|
+
end
|
579
566
|
|
580
|
-
|
581
|
-
rights(doc.attr 'copyright')
|
582
|
-
end
|
567
|
+
source doc.attr('source') if doc.attr? 'source'
|
583
568
|
|
584
|
-
|
569
|
+
rights doc.attr('copyright') if doc.attr? 'copyright'
|
585
570
|
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
571
|
+
#add_metadata 'ibooks:specified-fonts', true
|
572
|
+
|
573
|
+
add_theme_assets doc
|
574
|
+
add_cover_image doc
|
575
|
+
if (doc.attr 'publication-type', 'book') != 'book'
|
576
|
+
usernames = spine.map {|item| item.attr 'username' }.compact.uniq
|
577
|
+
add_profile_images doc, usernames
|
578
|
+
end
|
579
|
+
add_content doc
|
580
|
+
end
|
594
581
|
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
582
|
+
::FileUtils.mkdir_p dest unless ::File.directory? dest
|
583
|
+
|
584
|
+
epub_file = fmt == :kf8 ? %(#{::Asciidoctor::Helpers.rootname target}-kf8.epub) : target
|
585
|
+
builder.generate_epub epub_file
|
586
|
+
puts %(Wrote #{fmt.upcase} to #{epub_file}) if $VERBOSE
|
587
|
+
if options[:extract]
|
588
|
+
extract_dir = epub_file.sub EpubExtensionRx, ''
|
589
|
+
::FileUtils.remove_dir extract_dir if ::File.directory? extract_dir
|
590
|
+
::Dir.mkdir extract_dir
|
591
|
+
::Dir.chdir extract_dir do
|
592
|
+
::Zip::File.open epub_file do |entries|
|
593
|
+
entries.each do |entry|
|
594
|
+
next unless entry.file?
|
595
|
+
unless (entry_dir = ::File.dirname entry.name) == '.' || (::File.directory? entry_dir)
|
596
|
+
::FileUtils.mkdir_p entry_dir
|
597
|
+
end
|
598
|
+
entry.extract
|
599
|
+
end
|
610
600
|
end
|
611
|
-
entry.extract
|
612
601
|
end
|
602
|
+
puts %(Extracted #{fmt.upcase} to #{extract_dir}) if $VERBOSE
|
603
|
+
end
|
604
|
+
|
605
|
+
if fmt == :kf8
|
606
|
+
# QUESTION shouldn't we validate this epub file too?
|
607
|
+
distill_epub_to_mobi epub_file, target, options[:compress]
|
608
|
+
elsif options[:validate]
|
609
|
+
validate_epub epub_file
|
613
610
|
end
|
614
611
|
end
|
615
|
-
puts %(Extracted #{fmt.upcase} to #{extract_dir}) if $VERBOSE
|
616
|
-
end
|
617
612
|
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
613
|
+
def distill_epub_to_mobi epub_file, target, compress
|
614
|
+
kindlegen_cmd = KINDLEGEN
|
615
|
+
unless ::File.executable? kindlegen_cmd
|
616
|
+
require 'kindlegen' unless defined? ::Kindlegen
|
617
|
+
kindlegen_cmd = ::Kindlegen.command
|
618
|
+
end
|
619
|
+
mobi_file = ::File.basename target.sub(EpubExtensionRx, '.mobi')
|
620
|
+
compress_flag = KindlegenCompression[compress ? (compress.empty? ? '1' : compress.to_s) : '0']
|
621
|
+
cmd = [kindlegen_cmd, '-dont_append_source', compress_flag, '-o', mobi_file, epub_file].compact
|
622
|
+
::Open3.popen2e ::Shellwords.join(cmd) do |_input, output, _wait_thr|
|
623
|
+
output.each {|line| puts line } unless $VERBOSE.nil?
|
624
|
+
end
|
625
|
+
puts %(Wrote MOBI to #{::File.join ::File.dirname(epub_file), mobi_file}) if $VERBOSE
|
626
|
+
end
|
625
627
|
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
end
|
628
|
+
def validate_epub epub_file
|
629
|
+
if ::File.executable? EPUBCHECK
|
630
|
+
argv = [EPUBCHECK]
|
631
|
+
else
|
632
|
+
argv = [::Gem.ruby, ::Gem.bin_path('epubcheck-ruby', 'epubcheck')]
|
633
|
+
end
|
634
|
+
|
635
|
+
if $VERBOSE.nil?
|
636
|
+
argv << '-q'
|
637
|
+
else
|
638
|
+
argv << '-w'
|
639
|
+
end
|
640
|
+
argv << epub_file
|
640
641
|
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
642
|
+
::Open3.popen2e ::Shellwords.join(argv) do |_input, output, _wait_thr|
|
643
|
+
output.each {|line| puts line }
|
644
|
+
end
|
645
|
+
end
|
645
646
|
end
|
646
|
-
# NOTE epubcheck gem doesn't support epubcheck command options; enable -quiet once supported
|
647
|
-
::Open3.popen2e(::Shellwords.join [epubcheck_cmd, epub_file]) {|input, output, wait_thr|
|
648
|
-
output.each {|line| puts line } unless $VERBOSE.nil?
|
649
|
-
}
|
650
647
|
end
|
651
648
|
end
|
652
|
-
end
|
653
|
-
end
|