lib/asciidoctor-epub3/converter.rb in asciidoctor-epub3-1.5.0.alpha.19 vs lib/asciidoctor-epub3/converter.rb in asciidoctor-epub3-1.5.0
- old
+ new
@@ -225,12 +225,12 @@
# For list of supported landmark types see
# https://idpf.github.io/epub-vocabs/structure/
landmarks = []
- cover_page = add_cover_page node
- landmarks << { type: 'cover', href: cover_page.href, title: 'Cover' } unless cover_page.nil?
+ front_cover = add_cover_page node, 'front-cover'
+ landmarks << { type: 'cover', href: front_cover.href, title: 'Front Cover' } unless front_cover.nil?
front_matter_page = add_front_matter_page node
landmarks << { type: 'frontmatter', href: front_matter_page.href, title: 'Front Matter' } unless front_matter_page.nil?
nav_item = @book.add_item('nav.xhtml', id: 'nav').nav
@@ -251,10 +251,13 @@
else
toc_items = [node]
add_chapter node
end
+ _back_cover = add_cover_page node, 'back-cover'
+ # TODO: add landmark for back cover? But what epub:type?
+
landmarks << { type: 'bodymatter', href: %(#{get_chapter_name toc_items[0]}.xhtml), title: 'Start of Content' } unless toc_items.empty?
toc_items.each do |item|
landmarks << { type: item.style, href: %(#{get_chapter_name item}.xhtml), title: item.title } if %w(appendix bibliography glossary index preface).include? item.style
end
@@ -990,17 +993,26 @@
def resolve_image_attrs node
img_attrs = []
img_attrs << %(alt="#{node.attr 'alt'}") if node.attr? 'alt'
- width = node.attr 'scaledwidth'
- width = node.attr 'width' if width.nil?
-
# Unlike browsers, Calibre/Kindle *do* scale image if only height is specified
# So, in order to match browser behavior, we just always omit height
- img_attrs << %(width="#{width}") unless width.nil?
+ if (scaledwidth = node.attr 'scaledwidth')
+ img_attrs << %(style="width: #{scaledwidth}")
+ elsif (width = node.attr 'width')
+ # HTML5 spec (and EPUBCheck) only allows pixels in width, but browsers also accept percents
+ # and there are multiple AsciiDoc files in the wild that have width=percents%
+ # So, for compatibility reasons, output percentage width as a CSS style
+ if width[/^\d+%$/]
+ img_attrs << %(style="width: #{width}")
+ else
+ img_attrs << %(width="#{width}")
+ end
+ end
+
img_attrs
end
def convert_audio node
id_attr = node.id ? %( id="#{node.id}") : ''
@@ -1336,32 +1348,34 @@
end
end
nil
end
- def add_cover_page doc
- return nil if (image_path = doc.attr 'front-cover-image').nil?
+ def add_cover_page doc, name
+ image_attr_name = %(#{name}-image)
+ return nil if (image_path = doc.attr image_attr_name).nil?
+
imagesdir = (doc.attr 'imagesdir', '.').chomp '/'
imagesdir = (imagesdir == '.' ? '' : %(#{imagesdir}/))
image_attrs = {}
if (image_path.include? ':') && image_path =~ ImageMacroRx
- logger.warn %(deprecated block macro syntax detected in front-cover-image attribute) if image_path.start_with? 'image::'
+ logger.warn %(deprecated block macro syntax detected in :#{image_attr_name}: attribute) if image_path.start_with? 'image::'
image_path = %(#{imagesdir}#{$1})
(::Asciidoctor::AttributeList.new $2).parse_into image_attrs, %w(alt width height) unless $2.empty?
end
- image_href = %(#{imagesdir}jacket/cover#{::File.extname image_path})
+ image_href = %(#{imagesdir}jacket/#{name}#{::File.extname image_path})
workdir = doc.attr 'docdir'
workdir = '.' if workdir.nil_or_empty?
begin
@book.add_item(image_href, content: File.join(workdir, image_path)).cover_image
rescue => e
- logger.error %(#{::File.basename doc.attr('docfile')}: error adding front cover image. Make sure that :front-cover-image: attribute points to a valid image file. #{e})
+ logger.error %(#{::File.basename doc.attr('docfile')}: error adding cover image. Make sure that :#{image_attr_name}: attribute points to a valid image file. #{e})
return nil
end
return nil if @format == :kf8
@@ -1398,11 +1412,11 @@
width="100%" height="100%" viewBox="0 0 #{width} #{height}" preserveAspectRatio="xMidYMid meet">
<image width="#{width}" height="#{height}" xlink:href="#{image_href}"/>
</svg></body>
</html>).to_ios
- @book.add_ordered_item 'cover.xhtml', content: content, id: 'cover'
+ @book.add_ordered_item %(#{name}.xhtml), content: content, id: name
end
def get_frontmatter_files doc, workdir
if doc.attr? 'epub3-frontmatterdir'
fmdir = doc.attr 'epub3-frontmatterdir'
@@ -1642,11 +1656,18 @@
unless (result = ENV['KINDLEGEN']).nil?
logger.debug %(Using KINDLEGEN env variable: #{result})
return [result]
end
- logger.debug 'Using KindleGen from PATH'
- [%(kindlegen#{::Gem.win_platform? ? '.exe' : ''})]
+ begin
+ require 'kindlegen' unless defined? ::Kindlegen
+ result = ::Kindlegen.command.to_s
+ logger.debug %(Using KindleGen from gem: #{result})
+ [result]
+ rescue LoadError => e
+ logger.debug %(#{e}; Using KindleGen from PATH)
+ [%(kindlegen#{::Gem.win_platform? ? '.exe' : ''})]
+ end
end
def distill_epub_to_mobi epub_file, target, compress
mobi_file = ::File.basename target.sub(EpubExtensionRx, '.mobi')
compress_flag = KindlegenCompression[compress ? (compress.empty? ? '1' : compress.to_s) : '0']