lib/asciidoctor_fb2.rb in asciidoctor-fb2-0.2.4 vs lib/asciidoctor_fb2.rb in asciidoctor-fb2-0.3.0

- old
+ new

@@ -1,20 +1,22 @@ # frozen_string_literal: true require 'asciidoctor' require 'asciidoctor/converter' require 'fb2rb' +require 'mime/types' module Asciidoctor module FB2 DATA_DIR = File.expand_path(File.join(__dir__, '..', 'data')) # Converts AsciiDoc documents to FB2 e-book formats class Converter < Asciidoctor::Converter::Base # rubocop:disable Metrics/ClassLength include ::Asciidoctor::Writer CSV_DELIMITER_REGEX = /\s*,\s*/.freeze + IMAGE_ATTRIBUTE_VALUE_RX = /^image:{1,2}(.*?)\[(.*?)\]$/.freeze register_for 'fb2' # @return [FB2rb::Book] attr_reader(:book) @@ -60,12 +62,13 @@ date = node.attr('revdate') || node.attr('docdate') fb2date = FB2rb::FB2Date.new(date, Date.parse(date)) title_info.date = document_info.date = fb2date unless (cover_image = node.attr('front-cover-image')).nil? + cover_image = Regexp.last_match(1) if cover_image =~ IMAGE_ATTRIBUTE_VALUE_RX cover_image_path = node.image_uri(cover_image) - register_binary(node, cover_image_path) + register_binary(node, cover_image_path, 'image') title_info.coverpage = FB2rb::Coverpage.new([%(##{cover_image_path})]) end document_info.id = node.attr('uuid', '') document_info.version = node.attr('revnumber') @@ -245,17 +248,17 @@ %(<a l:href="#note-#{index}" type="note">[#{index}]</a>) end # @param node [Asciidoctor::Inline] def convert_inline_image(node) - image_attrs = register_binary(node, node.image_uri(node.target)) + image_attrs = register_binary(node, node.image_uri(node.target), 'image') %(<image #{image_attrs * ' '}/>) end # @param node [Asciidoctor::Block] def convert_image(node) - image_attrs = register_binary(node, node.image_uri(node.attr('target'))) + image_attrs = register_binary(node, node.image_uri(node.attr('target')), 'image') image_attrs << %(title="#{node.captioned_title}") if node.title? image_attrs << %(id="#{node.id}") if node.id %(<p><image #{image_attrs * ' '}/></p>) end @@ -264,13 +267,19 @@ def root_document(doc) doc = doc.parent_document until doc.parent_document.nil? doc end + def determine_mime_type(filename, media_type) + mime_types = MIME::Types.type_for(filename) + mime_types.delete_if { |x| x.media_type != media_type } + mime_types.empty? ? nil : mime_types[0].content_type + end + # @param node [Asciidoctor::AbstractNode] # @param target [String] - def register_binary(node, target) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength + def register_binary(node, target, media_type) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength unless Asciidoctor::Helpers.uriish?(target) out_dir = node.attr('outdir', nil, true) || doc_option(node.document, :to_dir) fs_path = File.join(out_dir, target) unless File.readable?(fs_path) base_dir = root_document(node.document).base_dir @@ -280,10 +289,11 @@ if File.readable?(fs_path) # Calibre fails to load images if they contain path separators target.sub!('/', '_') target.sub!('\\', '_') - @book.add_binary(target, fs_path) + mime_type = determine_mime_type(target, media_type) + @book.add_binary(target, fs_path, mime_type) target = %(##{target}) end end image_attrs = [%(l:href="#{target}")]