diff --git a/lib/review/epubmaker.rb b/lib/review/epubmaker.rb index 9daf4e13..8a281d24 100644 --- a/lib/review/epubmaker.rb +++ b/lib/review/epubmaker.rb @@ -214,12 +214,17 @@ def verify_target_images(basetmpdir) File.open(File.join(basetmpdir, content.file)) do |f| f.each_line do |l| l.scan(/url\((.+?)\)/) do |_m| - @config['epubmaker']['force_include_images'].push($1.strip) + @config['epubmaker']['force_include_images'].push($1.strip.gsub(/\A(['"])(.*)\1\Z/, '\2')) end end end end end + + if @config['coverimage'] + @config['epubmaker']['force_include_images'].push(File.join(@config['imagedir'], @config['coverimage'])) + end + @config['epubmaker']['force_include_images'] = @config['epubmaker']['force_include_images'].compact.sort.uniq end diff --git a/test/test_epubmaker.rb b/test/test_epubmaker.rb index cdca5f82..ef3ad77a 100644 --- a/test/test_epubmaker.rb +++ b/test/test_epubmaker.rb @@ -854,34 +854,45 @@ def test_epub_unsafe_id end def epubmaker_instance - Dir.mktmpdir do |tmpdir| - epubmaker = ReVIEW::EPUBMaker.new - epubmaker.instance_eval do - @config = ReVIEW::Configure.create(maker: 'epubmaker') - @config['titlepage'] = nil - @producer = ReVIEW::EPUBMaker::Producer.new(@config) - - @htmltoc = ReVIEW::HTMLToc.new(tmpdir) - - def config - @config + begin + Dir.mktmpdir do |tmpdir| + epubmaker = ReVIEW::EPUBMaker.new + epubmaker.instance_eval do + @config = ReVIEW::Configure.create(maker: 'epubmaker') + @config['titlepage'] = nil + @producer = ReVIEW::EPUBMaker::Producer.new(@config) + + @htmltoc = ReVIEW::HTMLToc.new(tmpdir) + + def config + @config + end + + def producer + @producer + end + + def error(s) + raise ApplicationError, s + end end - def error(s) - raise ApplicationError, s - end - end + File.write(File.join(tmpdir, 'exist.css'), 'body {}') + File.write(File.join(tmpdir, 'exist.html'), '') - File.write(File.join(tmpdir, 'exist.css'), 'body {}') - File.write(File.join(tmpdir, 'exist.html'), '') + Dir.mkdir(File.join(tmpdir, 'subdir')) + File.write(File.join(tmpdir, 'subdir', 'exist.html'), '') - Dir.mkdir(File.join(tmpdir, 'subdir')) - File.write(File.join(tmpdir, 'subdir', 'exist.html'), '') + Dir.mkdir(File.join(tmpdir, 'test')) + File.write(File.join(tmpdir, 'test', 'ch01.html'), '') + File.write(File.join(tmpdir, 'test', 'style.css'), 'div { background-image: url("images/bg.jpg")}') - Dir.chdir(tmpdir) do - Dir.mkdir('test') - yield(epubmaker, File.join(tmpdir, 'test')) + Dir.chdir(tmpdir) do + yield(epubmaker, File.join(tmpdir, 'test')) + end end + rescue Errno::EACCES, Errno::ENOTEMPTY + # Windows fails unlink when file is opened end end @@ -940,4 +951,19 @@ def test_copy_static_file end end end + + def test_verify_target_images + epubmaker_instance do |epubmaker, tmpdir| + epubmaker.config['epubmaker']['verify_target_images'] = true + epubmaker.config['coverimage'] = 'cover.png' + + epubmaker.producer.contents << ReVIEW::EPUBMaker::Content.new(file: 'ch01.html', title: 'CH01', level: 1) + epubmaker.producer.contents << ReVIEW::EPUBMaker::Content.new(file: 'style.css') + epubmaker.verify_target_images(tmpdir) + + expect = %w[images/bg.jpg images/ch01.png images/cover.png] + assert_equal expect, epubmaker.config['epubmaker']['force_include_images'] + assert_equal true, true + end + end end