Skip to content

Commit

Permalink
Merge pull request #1923 from kmuto/include-coverimage
Browse files Browse the repository at this point in the history
EPUBMaker: verify_target_imagesが有効なときに、coverimageを暗黙に取り込む
  • Loading branch information
kmuto authored Aug 19, 2024
2 parents 378d1dc + 78a561e commit f143153
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
7 changes: 6 additions & 1 deletion lib/review/epubmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
70 changes: 48 additions & 22 deletions test/test_epubmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'), '<html></html>')

File.write(File.join(tmpdir, 'exist.css'), 'body {}')
File.write(File.join(tmpdir, 'exist.html'), '<html></html>')
Dir.mkdir(File.join(tmpdir, 'subdir'))
File.write(File.join(tmpdir, 'subdir', 'exist.html'), '<html></html>')

Dir.mkdir(File.join(tmpdir, 'subdir'))
File.write(File.join(tmpdir, 'subdir', 'exist.html'), '<html></html>')
Dir.mkdir(File.join(tmpdir, 'test'))
File.write(File.join(tmpdir, 'test', 'ch01.html'), '<html><img src="images/ch01.png" /></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

Expand Down Expand Up @@ -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

0 comments on commit f143153

Please sign in to comment.