lib/softcover/builders/epub.rb in softcover-1.0.beta12 vs lib/softcover/builders/epub.rb in softcover-1.0.beta13

- old
+ new

@@ -5,12 +5,16 @@ # Returns the name of the cover file. # We support (in order) JPG/JPEG, PNG, and TIFF. def cover_img extensions = %w[jpg jpeg png tiff] extensions.each do |ext| - file = Dir[path("#{images_dir}/cover.#{ext}")].first - return File.basename(file) if file + origin = "images/cover.#{ext}" + target = "#{images_dir}/cover.#{ext}" + if File.exist?(origin) + FileUtils.cp(origin, target) + return File.basename(target) + end end return false end def cover? @@ -37,10 +41,11 @@ if manifest.markdown? opts = options.merge({ source: :polytex, origin: :markdown }) self.manifest = Softcover::BookManifest.new(opts) end remove_html + remove_images create_directories write_mimetype write_container_xml write_ibooks_xml write_toc @@ -62,15 +67,21 @@ # All the HTML is generated, so this clears out any unused files. def remove_html FileUtils.rm(Dir.glob(path('epub/OEBPS/*.html'))) end + # Removes images in case they are stale. + def remove_images + rm_r images_dir + end + def create_directories mkdir('epub') mkdir(path('epub/OEBPS')) mkdir(path('epub/OEBPS/styles')) mkdir(path('epub/META-INF')) + mkdir(images_dir) mkdir('ebooks') end # Writes the mimetype file. # This is required by the EPUB standard. @@ -274,14 +285,27 @@ def clean_book_id(filename) File.read(filename).gsub(/#book/, '') end # Copies the image files from the HTML version of the document. - # We remove PDF images, which are valid in PDF documents but not in EPUB. def copy_image_files + # Copy over all images to guarantee the same directory structure. FileUtils.cp_r(File.join('html', 'images'), File.join('epub', 'OEBPS')) - File.delete(*Dir['epub/OEBPS/images/**/*.pdf']) + # Parse the full HTML file with Nokogiri to get images actually used. + html = File.read(manifest.full_html_file) + html_image_filenames = Nokogiri::HTML(html).css('img').map do |node| + node.attributes['src'].value + end + # Form the corresponding EPUB image paths. + used_image_filenames = html_image_filenames.map do |filename| + "epub/OEBPS/#{filename}" + end.to_set + # Delete unused images. + Dir.glob("epub/OEBPS/images/**/*").each do |image| + next if File.directory?(image) + rm image unless used_image_filenames.include?(image) + end end # Make the EPUB, which is basically just a zipped HTML file. def make_epub(options={}) filename = manifest.filename \ No newline at end of file