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.
@@ -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
- DATA_DIR = ::File.expand_path(::File.join ::File.dirname(__FILE__), '..', '..', 'data')
10
- SAMPLES_DIR = ::File.join DATA_DIR, 'samples'
11
- LF = ?\n
12
- CharEntityRx = ContentConverter::CharEntityRx
13
- XmlElementRx = ContentConverter::XmlElementRx
14
- FromHtmlSpecialCharsMap = ContentConverter::FromHtmlSpecialCharsMap
15
- FromHtmlSpecialCharsRx = ContentConverter::FromHtmlSpecialCharsRx
16
- CsvDelimiterRx = /\s*,\s*/
17
- DefaultCoverImage = 'images/default-cover.png'
18
- ImageMacroRx = /^image::?(.*?)\[(.*?)\]$/
19
- ImgSrcScanRx = /<img src="(.+?)"/
20
- SvgImgSniffRx = /<img src=".+?\.svg"/
21
-
22
- attr_reader :book, :format, :spine
23
-
24
- # FIXME move to Asciidoctor::Helpers
25
- def sanitize_doctitle_xml doc, content_spec
26
- doctitle = doc.header? ? doc.doctitle : (doc.attr 'untitled-label')
27
- sanitize_xml doctitle, content_spec
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
- case content_spec
39
- when :attribute_cdata
40
- content = content.gsub '"', '&quot;' if content.include? '"'
41
- when :cdata, :pcdata
42
- # noop
43
- when :plain_text
44
- if content.include? ';'
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
- def add_theme_assets doc
55
- builder = self
56
- format = @format
57
- workdir = if doc.attr? 'epub3-stylesdir'
58
- stylesdir = doc.attr 'epub3-stylesdir'
59
- # FIXME make this work for Windows paths!!
60
- if stylesdir.start_with? '/'
61
- stylesdir
62
- else
63
- docdir = doc.attr 'docdir', '.'
64
- docdir = '.' if docdir.empty?
65
- ::File.join docdir, stylesdir
40
+ case content_spec
41
+ when :attribute_cdata
42
+ content = content.gsub '"', '&quot;' 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
- # TODO improve design/UX of custom theme functionality, including custom fonts
72
- resources do
73
- if format == :kf8
74
- # NOTE add layer of indirection so Kindle Direct Publishing (KDP) doesn't strip font-related CSS rules
75
- file 'styles/epub3.css' => '@import url("epub3-proxied.css");'.to_ios
76
- file 'styles/epub3-css3-only.css' => '@import url("epub3-css3-only-proxied.css");'.to_ios
77
- file 'styles/epub3-proxied.css' => (builder.postprocess_css_file ::File.join(workdir, 'epub3.css'), format)
78
- file 'styles/epub3-css3-only-proxied.css' => (builder.postprocess_css_file ::File.join(workdir, 'epub3-css3-only.css'), format)
79
- else
80
- file 'styles/epub3.css' => (builder.postprocess_css_file ::File.join(workdir, 'epub3.css'), format)
81
- file 'styles/epub3-css3-only.css' => (builder.postprocess_css_file ::File.join(workdir, 'epub3-css3-only.css'), format)
82
- end
83
- end
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
- resources do
86
- font_files, font_css = builder.select_fonts ::File.join(DATA_DIR, 'styles/epub3-fonts.css'), (doc.attr 'scripts', 'latin')
87
- file 'styles/epub3-fonts.css' => font_css
88
- unless font_files.empty?
89
- # NOTE metadata property in oepbs package manifest doesn't work; must use proprietary iBooks file instead
90
- #(@book.metadata.add_metadata 'meta', 'true')['property'] = 'ibooks:specified-fonts' unless format == :kf8
91
- builder.optional_file 'META-INF/com.apple.ibooks.display-options.xml' => '<?xml version="1.0" encoding="UTF-8"?>
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
- # https://github.com/asciidoctor/asciidoctor-epub3/issues/120
99
- #
100
- # 'application/x-font-ttf' causes warnings in epubcheck 4.0.2,
101
- # "non-standard font type". Discussion:
102
- # https://www.mobileread.com/forums/showthread.php?t=231272
103
- #
104
- # 3.1 spec recommends 'application/font-sfnt', but epubcheck doesn't
105
- # implement that yet (warnings). https://idpf.github.io/epub-cmt/v3/
106
- #
107
- # 3.0 spec recommends 'application/vnd.ms-opentype', this works without
108
- # warnings.
109
- # http://www.idpf.org/epub/30/spec/epub30-publications.html#sec-core-media-types
110
- with_media_type 'application/vnd.ms-opentype' do
111
- font_files.each do |font_file|
112
- file font_file => ::File.join(DATA_DIR, font_file)
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
- def add_cover_image doc
121
- imagesdir = (doc.attr 'imagesdir', '.').chomp '/'
122
- imagesdir = (imagesdir == '.' ? nil : %(#{imagesdir}/))
122
+ def add_cover_image doc
123
+ imagesdir = (doc.attr 'imagesdir', '.').chomp '/'
124
+ imagesdir = (imagesdir == '.' ? nil : %(#{imagesdir}/))
123
125
 
124
- if (image_path = doc.attr 'front-cover-image')
125
- image_attrs = {}
126
- if (image_path.include? ':') && image_path =~ ImageMacroRx
127
- if image_path.start_with? 'image::'
128
- warn %(asciidoctor: WARNING: deprecated block macro syntax detected in front-cover-image attribute)
129
- end
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
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
- unless image_path
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
- resources do
149
- cover_image %(#{imagesdir}jacket/cover#{::File.extname image_path}) => (::File.join workdir, image_path)
150
- @last_defined_item.tap do |last_item|
151
- last_item['width'] = width
152
- last_item['height'] = height
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
- # NOTE must be called within the ordered block
159
- def add_cover_page doc, spine_builder, manifest
160
- cover_item_attrs = manifest.items['item_cover'].instance_variable_get :@attributes
161
- href = cover_item_attrs['href']
162
- # NOTE we only store width and height temporarily to pass through the values
163
- width = cover_item_attrs.delete 'width'
164
- height = cover_item_attrs.delete 'height'
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
- # NOTE SVG wrapper maintains aspect ratio and confines image to view box
167
- content = %(<!DOCTYPE html>
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
- # Gitden expects a cover.xhtml, so add it to the spine
197
- spine_builder.file 'cover.xhtml' => content
198
- assigned_id = (spine_builder.instance_variable_get :@last_defined_item).item.id
199
- spine_builder.id 'cover'
200
- # clearly a deficiency of gepub that it does not match the id correctly
201
- # FIXME can we move this hack elsewhere?
202
- @book.spine.itemref_by_id[assigned_id].idref = 'cover'
203
- nil
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
- def add_front_matter_page doc, spine_builder
216
- if ::File.file? 'front-matter.html'
217
- front_matter_content = ::File.read 'front-matter.html'
218
- spine_builder.file 'front-matter.xhtml' => (postprocess_xhtml front_matter_content, @format)
219
- unless (spine_builder.property? 'svg') || SvgImgSniffRx !~ front_matter_content
220
- spine_builder.add_property 'svg'
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
- def add_content_images doc, images
227
- docimagesdir = (doc.attr 'imagesdir', '.').chomp '/'
228
- docimagesdir = (docimagesdir == '.' ? nil : %(#{docimagesdir}/))
229
-
230
- workdir = (workdir = doc.attr 'docdir').nil_or_empty? ? '.' : workdir
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
- def add_profile_images doc, usernames
246
- imagesdir = (doc.attr 'imagesdir', '.').chomp '/'
247
- imagesdir = (imagesdir == '.' ? nil : %(#{imagesdir}/))
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
- resources do
250
- file %(#{imagesdir}avatars/default.jpg) => ::File.join(DATA_DIR, 'images/default-avatar.jpg')
251
- file %(#{imagesdir}headshots/default.jpg) => ::File.join(DATA_DIR, 'images/default-headshot.jpg')
252
- end
241
+ def add_profile_images doc, usernames
242
+ imagesdir = (doc.attr 'imagesdir', '.').chomp '/'
243
+ imagesdir = (imagesdir == '.' ? nil : %(#{imagesdir}/))
253
244
 
254
- workdir = (workdir = doc.attr 'docdir').nil_or_empty? ? '.' : workdir
255
- resources do
256
- usernames.each do |username|
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
- headshot = %(#{imagesdir}headshots/#{username}.jpg)
266
- if ::File.readable?(resolved_headshot = ::File.join(workdir, headshot))
267
- file headshot => resolved_headshot
268
- elsif doc.attr? 'builder', 'editions'
269
- warn %(asciidoctor: ERROR: headshot for #{username} not found or readable: #{headshot}; falling back to default headshot)
270
- file headshot => ::File.join(DATA_DIR, 'images/default-headshot.jpg')
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
- def add_content doc
278
- builder, spine, format, images = self, @spine, @format, {}
279
- workdir = (doc.attr 'docdir').nil_or_empty? ? '.' : workdir
280
- resources workdir: workdir do
281
- extend GepubResourceBuilderMixin
282
- builder.add_images_from_front_matter
283
- builder.add_nav_doc doc, self, spine, format
284
- builder.add_ncx_doc doc, self, spine
285
- ordered do
286
- builder.add_cover_page doc, self, @book.manifest unless format == :kf8
287
- builder.add_front_matter_page doc, self
288
- spine.each_with_index do |item, i|
289
- docfile = item.attr 'docfile'
290
- imagesdir = (item.attr 'imagesdir', '.').chomp '/'
291
- imagesdir = (imagesdir == '.' ? '' : %(#{imagesdir}/))
292
- file %(#{item.id || (item.attr 'docname')}.xhtml) => (builder.postprocess_xhtml item.convert, format)
293
- add_property 'svg' if ((item.attr 'epub-properties') || []).include? 'svg'
294
- # QUESTION should we pass the document itself?
295
- item.references[:images].each do |target|
296
- images[image_path = %(#{imagesdir}#{target})] ||= { docfile: docfile, path: image_path }
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
- def add_nav_doc doc, spine_builder, spine, format
308
- spine_builder.nav 'nav.xhtml' => (postprocess_xhtml nav_doc(doc, spine), format)
309
- spine_builder.id 'nav'
310
- nil
311
- end
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
- # TODO aggregate authors of spine document into authors attribute(s) on main document
314
- def nav_doc doc, spine
315
- lines = [%(<!DOCTYPE html>
316
- <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}">
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
- lines << (nav_level spine, [(doc.attr 'toclevels', 1).to_i, 0].max)
328
- lines << %(</nav>
323
+ lines << (nav_level spine, [(doc.attr 'toclevels', 1).to_i, 0].max)
324
+ lines << %(</nav>
329
325
  </body>
330
326
  </html>)
331
- lines * LF
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
- lines << %(<li><a href="#{item_href}">#{item_label}</a>)
348
- unless depth == 0 || (child_sections = item.sections).empty?
349
- lines << (nav_level child_sections, depth - 1, state)
350
- lines << '</li>'
351
- else
352
- lines[-1] = %(#{lines[-1]}</li>)
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
- # NOTE gepub doesn't support building a ncx TOC with depth > 1, so do it ourselves
361
- def add_ncx_doc doc, spine_builder, spine
362
- spine_builder.file 'toc.ncx' => (ncx_doc doc, spine).to_ios
363
- spine_builder.id 'ncx'
364
- nil
365
- end
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
- def ncx_doc doc, spine
368
- # TODO populate docAuthor element based on unique authors in work
369
- lines = [%(<?xml version="1.0" encoding="utf-8"?>
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
- lines << (ncx_level spine, [(doc.attr 'toclevels', 1).to_i, 0].max, state = {})
380
- lines[0] = lines[0].sub '%{depth}', %(<meta name="dtb:depth" content="#{state[:max_depth]}"/>)
381
- lines << %(</navMap>
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
- lines * LF
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
- lines << %(<navPoint id="#{item_id}" playOrder="#{index}">)
401
- lines << %(<navLabel><text>#{item_label}</text></navLabel>)
402
- lines << %(<content src="#{item_href}"/>)
403
- unless depth == 0 || (child_sections = item.sections).empty?
404
- lines << (ncx_level child_sections, depth - 1, state)
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
- def collect_keywords doc, spine
413
- ([doc] + spine).map do |item|
414
- if item.attr? 'keywords'
415
- (item.attr 'keywords').split CsvDelimiterRx
416
- else
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
- # Swap fonts in CSS based on the value of the document attribute 'scripts',
423
- # then return the list of fonts as well as the font CSS.
424
- def select_fonts filename, scripts = 'latin'
425
- font_css = ::File.read(filename)
426
- font_css = font_css.gsub(/(?<=-)latin(?=\.ttf\))/, scripts) unless scripts == 'latin'
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
- # match CSS font urls in the forms of:
429
- # src: url(../fonts/notoserif-regular-latin.ttf);
430
- # src: url(../fonts/notoserif-regular-latin.ttf) format("truetype");
431
- font_list = font_css.scan(/url\(\.\.\/([^)]+\.ttf)\)/).flatten
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
- return [font_list, font_css.to_ios]
434
- end
428
+ [font_list, font_css.to_ios]
429
+ end
435
430
 
436
- def postprocess_css_file filename, format
437
- return filename unless format == :kf8
438
- postprocess_css ::File.read(filename), format
439
- end
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
- def postprocess_css content, format
442
- return content.to_ios unless format == :kf8
443
- # TODO convert regular expressions to constants
444
- content
445
- .gsub(/^ -webkit-column-break-.*\n/, '')
446
- .gsub(/^ max-width: .*\n/, '')
447
- .to_ios
448
- end
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
- def postprocess_xhtml_file filename, format
451
- return filename unless format == :kf8
452
- postprocess_xhtml ::File.read(filename), format
453
- end
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
- # NOTE Kindle requires that
456
- # <meta charset="utf-8"/>
457
- # be converted to
458
- # <meta http-equiv="Content-Type" content="application/xml+xhtml; charset=UTF-8"/>
459
- def postprocess_xhtml content, format
460
- return content.to_ios unless format == :kf8
461
- # TODO convert regular expressions to constants
462
- content
463
- .gsub(/<meta charset="(.+?)"\/>/, '<meta http-equiv="Content-Type" content="application/xml+xhtml; charset=\1"/>')
464
- .gsub(/<img([^>]+) style="width: (\d\d)%;"/, '<img\1 style="width: \2%; height: \2%;"')
465
- .gsub(/<script type="text\/javascript">.*?<\/script>\n?/m, '')
466
- .to_ios
467
- end
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
- # Add missing method to builder to add a property to last defined item
472
- def add_property property
473
- @last_defined_item.add_property property
474
- end
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
- # Add helper method to builder to check if property is set on last defined item
477
- def property? property
478
- (@last_defined_item['properties'] || []).include? property
479
- end
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
- KINDLEGEN = ENV['KINDLEGEN'] || 'kindlegen'
484
- EPUBCHECK = ENV['EPUBCHECK'] || %(epubcheck#{::Gem.win_platform? ? '.bat' : '.sh'})
485
- EpubExtensionRx = /\.epub$/i
486
- KindlegenCompression = ::Hash['0', '-c0', '1', '-c1', '2', '-c2', 'none', '-c0', 'standard', '-c1', 'huffdic', '-c2']
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
- def initialize spine_doc, spine, format = :epub3, options = {}
489
- @document = spine_doc
490
- @spine = spine || []
491
- @format = format
492
- end
483
+ def initialize spine_doc, spine, format = :epub3, _options = {}
484
+ @document = spine_doc
485
+ @spine = spine || []
486
+ @format = format
487
+ end
493
488
 
494
- def package options = {}
495
- doc = @document
496
- spine = @spine
497
- fmt = @format
498
- target = options[:target]
499
- dest = File.dirname target
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
- builder = ::GEPUB::Builder.new do
509
- extend GepubBuilderMixin
510
- @document = doc
511
- @spine = spine
512
- @format = fmt
513
- @book.epub_backward_compat = (fmt != :kf8)
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
- language(doc.attr 'lang', 'en')
516
- id 'pub-language'
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
- if doc.attr? 'uuid'
519
- unique_identifier doc.attr('uuid'), 'pub-identifier', 'uuid'
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
- if doc.attr? 'creator'
550
- # marc role: Creator (see http://www.loc.gov/marc/relators/relaterm.html)
551
- creator doc.attr('creator'), 'cre'
552
- else
553
- # marc role: Manufacturer (see http://www.loc.gov/marc/relators/relaterm.html)
554
- # QUESTION should this be bkp?
555
- creator 'Asciidoctor', 'mfr'
556
- end
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
- # TODO getting author list should be a method on Asciidoctor API
559
- contributors(*authors)
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
- if doc.attr? 'revdate'
562
- # TODO ensure this is a real date
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
- if doc.attr? 'description'
569
- description(doc.attr 'description')
570
- end
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
- (collect_keywords doc, spine).each do |s|
573
- subject s
574
- end
561
+ description doc.attr('description') if doc.attr? 'description'
575
562
 
576
- if doc.attr? 'source'
577
- source(doc.attr 'source')
578
- end
563
+ (collect_keywords doc, spine).each do |s|
564
+ subject s
565
+ end
579
566
 
580
- if doc.attr? 'copyright'
581
- rights(doc.attr 'copyright')
582
- end
567
+ source doc.attr('source') if doc.attr? 'source'
583
568
 
584
- #add_metadata 'ibooks:specified-fonts', true
569
+ rights doc.attr('copyright') if doc.attr? 'copyright'
585
570
 
586
- add_theme_assets doc
587
- add_cover_image doc
588
- if (doc.attr 'publication-type', 'book') != 'book'
589
- usernames = spine.map {|item| item.attr 'username' }.compact.uniq
590
- add_profile_images doc, usernames
591
- end
592
- add_content doc
593
- end
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
- ::FileUtils.mkdir_p dest unless ::File.directory? dest
596
-
597
- epub_file = fmt == :kf8 ? %(#{::Asciidoctor::Helpers.rootname target}-kf8.epub) : target
598
- builder.generate_epub epub_file
599
- puts %(Wrote #{fmt.upcase} to #{epub_file}) if $VERBOSE
600
- if options[:extract]
601
- extract_dir = epub_file.sub EpubExtensionRx, ''
602
- ::FileUtils.remove_dir extract_dir if ::File.directory? extract_dir
603
- ::Dir.mkdir extract_dir
604
- ::Dir.chdir extract_dir do
605
- ::Zip::File.open epub_file do |entries|
606
- entries.each do |entry|
607
- next unless entry.file?
608
- unless (entry_dir = ::File.dirname entry.name) == '.' || (::File.directory? entry_dir)
609
- ::FileUtils.mkdir_p entry_dir
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
- if fmt == :kf8
619
- # QUESTION shouldn't we validate this epub file too?
620
- distill_epub_to_mobi epub_file, target, options[:compress]
621
- elsif options[:validate]
622
- validate_epub epub_file
623
- end
624
- end
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
- def distill_epub_to_mobi epub_file, target, compress
627
- kindlegen_cmd = KINDLEGEN
628
- unless ::File.executable? kindlegen_cmd
629
- require 'kindlegen' unless defined? ::Kindlegen
630
- kindlegen_cmd = ::Kindlegen.command
631
- end
632
- mobi_file = ::File.basename(target.sub EpubExtensionRx, '.mobi')
633
- compress_flag = KindlegenCompression[compress ? (compress.empty? ? '1' : compress.to_s) : '0']
634
- cmd = [kindlegen_cmd, '-dont_append_source', compress_flag, '-o', mobi_file, epub_file].compact
635
- ::Open3.popen2e(::Shellwords.join cmd) {|input, output, wait_thr|
636
- output.each {|line| puts line } unless $VERBOSE.nil?
637
- }
638
- puts %(Wrote MOBI to #{::File.join ::File.dirname(epub_file), mobi_file}) if $VERBOSE
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
- def validate_epub epub_file
642
- epubcheck_cmd = EPUBCHECK
643
- unless ::File.executable? epubcheck_cmd
644
- epubcheck_cmd = ::Gem.bin_path 'epubcheck', 'epubcheck'
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