lib/asciidoctor-epub3/packager.rb in asciidoctor-epub3-1.5.0.alpha.7 vs lib/asciidoctor-epub3/packager.rb in asciidoctor-epub3-1.5.0.alpha.8

- old
+ new

@@ -92,11 +92,23 @@ <platform name="*"> <option name="specified-fonts">true</option> </platform> </display_options>'.to_ios unless format == :kf8 - with_media_type 'application/x-font-ttf' do + # https://github.com/asciidoctor/asciidoctor-epub3/issues/120 + # + # 'application/x-font-ttf' causes warnings in epubcheck 4.0.2, + # "non-standard font type". Discussion: + # https://www.mobileread.com/forums/showthread.php?t=231272 + # + # 3.1 spec recommends 'application/font-sfnt', but epubcheck doesn't + # implement that yet (warnings). https://idpf.github.io/epub-cmt/v3/ + # + # 3.0 spec recommends 'application/vnd.ms-opentype', this works without + # warnings. + # http://www.idpf.org/epub/30/spec/epub30-publications.html#sec-core-media-types + with_media_type 'application/vnd.ms-opentype' do font_files.each do |font_file| file font_file => ::File.join(DATA_DIR, font_file) end end end @@ -121,11 +133,11 @@ if ::File.readable?(::File.join workdir, image_path) unless !image_attrs.empty? && (width = image_attrs['width']) && (height = image_attrs['height']) width, height = 1050, 1600 end else - warn %(asciidoctor: ERROR: front cover image not found or readable: #{image_path}) + warn %(asciidoctor: ERROR: #{::File.basename(doc.attr 'docfile')}: front cover image not found or readable: #{::File.expand_path image_path, workdir}) image_path = nil end end unless image_path @@ -208,27 +220,23 @@ end end nil end - # FIXME add inline images def add_content_images doc, images docimagesdir = (doc.attr 'imagesdir', '.').chomp '/' docimagesdir = (docimagesdir == '.' ? nil : %(#{docimagesdir}/)) workdir = (workdir = doc.attr 'docdir').nil_or_empty? ? '.' : workdir resources workdir: workdir do images.each do |image| - imagesdir = (image.document.attr 'imagesdir', '.').chomp '/' - imagesdir = (imagesdir == '.' ? nil : %(#{imagesdir}/)) - image_path = %(#{imagesdir}#{image.attr 'target'}) - if image_path.start_with? %(#{docimagesdir}jacket/cover.) + if (image_path = image[:path]).start_with? %(#{docimagesdir}jacket/cover.) warn %(asciidoctor: WARNING: image path is reserved for cover artwork: #{image_path}; skipping image found in content) elsif ::File.readable? image_path file image_path else - warn %(asciidoctor: ERROR: image not found or not readable: #{image_path}) + warn %(asciidoctor: ERROR: #{::File.basename image[:docfile]}: image not found or not readable: #{::File.expand_path image_path, workdir}) end end end nil end @@ -264,28 +272,36 @@ end nil end def add_content doc - builder, spine, format = self, @spine, @format + builder, spine, format, images = self, @spine, @format, {} workdir = (doc.attr 'docdir').nil_or_empty? ? '.' : workdir resources workdir: workdir do extend GepubResourceBuilderMixin builder.add_images_from_front_matter builder.add_nav_doc doc, self, spine, format builder.add_ncx_doc doc, self, spine ordered do builder.add_cover_page doc, self, @book.manifest unless format == :kf8 builder.add_front_matter_page doc, self spine.each_with_index do |item, i| + docfile = item.attr 'docfile' + imagesdir = (item.attr 'imagesdir', '.').chomp '/' + imagesdir = (imagesdir == '.' ? '' : %(#{imagesdir}/)) file %(#{item.id || (item.attr 'docname')}.xhtml) => (builder.postprocess_xhtml item.convert, format) add_property 'svg' if ((item.attr 'epub-properties') || []).include? 'svg' + # QUESTION should we pass the document itself? + item.references[:images].each do |target| + images[image_path = %(#{imagesdir}#{target})] ||= { docfile: docfile, path: image_path } + end # QUESTION reenable? #linear 'yes' if i == 0 end end end + add_content_images doc, images.values nil end def add_nav_doc doc, spine_builder, spine, format spine_builder.nav 'nav.xhtml' => (postprocess_xhtml nav_doc(doc, spine), format) @@ -405,11 +421,16 @@ # Swap fonts in CSS based on the value of the document attribute 'scripts', # then return the list of fonts as well as the font CSS. def select_fonts filename, scripts = 'latin' font_css = ::File.read(filename) font_css = font_css.gsub(/(?<=-)latin(?=\.ttf\))/, scripts) unless scripts == 'latin' - font_list = font_css.scan(/url\(\.\.\/(.+\.ttf)\);$/).flatten + + # match CSS font urls in the forms of: + # src: url(../fonts/notoserif-regular-latin.ttf); + # src: url(../fonts/notoserif-regular-latin.ttf) format("truetype"); + font_list = font_css.scan(/url\(\.\.\/([^)]+\.ttf)\)/).flatten + return [font_list, font_css.to_ios] end def postprocess_css_file filename, format return filename unless format == :kf8 @@ -474,15 +495,13 @@ spine = @spine fmt = @format target = options[:target] dest = File.dirname target - images = spine.map {|item| item.find_by context: :image }.compact.flatten - .uniq {|img| %(#{(img.document.attr 'imagesdir', '.').chomp '/'}/#{img.attr 'target'}) } # FIXME authors should be aggregated already on parent document authors = if doc.attr? 'authors' - (doc.attr 'authors').split(GepubBuilderMixin::CsvDelimiterRx).concat(spine.map {|item| item.attr 'author' }).uniq + (doc.attr 'authors').split(GepubBuilderMixin::CsvDelimiterRx).concat(spine.map {|item| item.attr 'author' }.compact).uniq else [] end builder = ::GEPUB::Builder.new do @@ -567,11 +586,9 @@ add_cover_image doc if (doc.attr 'publication-type', 'book') != 'book' usernames = spine.map {|item| item.attr 'username' }.compact.uniq add_profile_images doc, usernames end - # QUESTION move add_content_images to add_content method? - add_content_images doc, images add_content doc end ::FileUtils.mkdir_p dest unless ::File.directory? dest