Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EPUBの差し込み静的ファイルの存在チェック #1670

Merged
merged 2 commits into from
Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions lib/review/epubmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,44 +504,49 @@ def copy_stylesheet(basetmpdir)

@config['stylesheet'].each do |sfile|
unless File.exist?(sfile)
error "#{sfile} is not found."
error "stylesheet: #{sfile} is not found."
end
FileUtils.cp(sfile, basetmpdir)
@producer.contents.push(ReVIEW::EPUBMaker::Content.new(file: sfile))
end
end

def copy_static_file(configname, destdir, destfilename: nil)
destfilename ||= @config[configname]
unless File.exist?(@config[configname])
error "#{configname}: #{@config[configname]} is not found."
end
FileUtils.cp(@config[configname],
File.join(destdir, destfilename))
end

def copy_frontmatter(basetmpdir)
if @config['cover'].present? && File.exist?(@config['cover'])
FileUtils.cp(@config['cover'],
File.join(basetmpdir, File.basename(@config['cover'])))
copy_static_file('cover', basetmpdir)
end

if @config['titlepage']
if @config['titlefile'].nil?
build_titlepage(basetmpdir, "titlepage.#{@config['htmlext']}")
else
FileUtils.cp(@config['titlefile'],
File.join(basetmpdir, "titlepage.#{@config['htmlext']}"))
copy_static_file('titlefile', basetmpdir, destfilename: "titlepage.#{@config['htmlext']}")
end
@htmltoc.add_item(1,
"titlepage.#{@config['htmlext']}",
ReVIEW::I18n.t('titlepagetitle'),
chaptype: 'pre')
end

if @config['originaltitlefile'].present? && File.exist?(@config['originaltitlefile'])
FileUtils.cp(@config['originaltitlefile'],
File.join(basetmpdir, File.basename(@config['originaltitlefile'])))
if @config['originaltitlefile'].present?
copy_static_file('originaltitlefile', basetmpdir)
@htmltoc.add_item(1,
File.basename(@config['originaltitlefile']),
ReVIEW::I18n.t('originaltitle'),
chaptype: 'pre')
end

if @config['creditfile'].present? && File.exist?(@config['creditfile'])
FileUtils.cp(@config['creditfile'],
File.join(basetmpdir, File.basename(@config['creditfile'])))
if @config['creditfile'].present?
copy_static_file('creditfile', basetmpdir)
@htmltoc.add_item(1,
File.basename(@config['creditfile']),
ReVIEW::I18n.t('credittitle'),
Expand Down Expand Up @@ -577,27 +582,24 @@ def build_titlepage(basetmpdir, htmlfile)

def copy_backmatter(basetmpdir)
if @config['profile']
FileUtils.cp(@config['profile'],
File.join(basetmpdir, File.basename(@config['profile'])))
copy_static_file('profile', basetmpdir)
@htmltoc.add_item(1,
File.basename(@config['profile']),
ReVIEW::I18n.t('profiletitle'),
chaptype: 'post')
end

if @config['advfile']
FileUtils.cp(@config['advfile'],
File.join(basetmpdir, File.basename(@config['advfile'])))
copy_static_file('advfile', basetmpdir)
@htmltoc.add_item(1,
File.basename(@config['advfile']),
ReVIEW::I18n.t('advtitle'),
chaptype: 'post')
end

if @config['colophon']
if @config['colophon'].is_a?(String) # FIXME: should let obsolete this style?
FileUtils.cp(@config['colophon'],
File.join(basetmpdir, "colophon.#{@config['htmlext']}"))
if @config['colophon'].is_a?(String)
copy_static_file('colophon', basetmpdir, destfilename: "colophon.#{@config['htmlext']}") # override pre-built colophon
end
@htmltoc.add_item(1,
"colophon.#{@config['htmlext']}",
Expand All @@ -606,8 +608,7 @@ def copy_backmatter(basetmpdir)
end

if @config['backcover']
FileUtils.cp(@config['backcover'],
File.join(basetmpdir, File.basename(@config['backcover'])))
copy_static_file('backcover', basetmpdir)
@htmltoc.add_item(1,
File.basename(@config['backcover']),
ReVIEW::I18n.t('backcovertitle'),
Expand Down
4 changes: 3 additions & 1 deletion lib/review/epubmaker/epubcommon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ def produce_write_common(basedir, tmpdir)
next if item.file =~ /#/ # skip subgroup

fname = "#{basedir}/#{item.file}"
raise "#{fname} doesn't exist. Abort." unless File.exist?(fname)
unless File.exist?(fname)
raise "#{fname} is not found."
end

FileUtils.mkdir_p(File.dirname("#{tmpdir}/OEBPS/#{item.file}"))
FileUtils.cp(fname, "#{tmpdir}/OEBPS/#{item.file}")
Expand Down
73 changes: 73 additions & 0 deletions test/test_epubmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -865,4 +865,77 @@ def test_epub_unsafe_id
content = ReVIEW::EPUBMaker::Content.new(file: 'sample-&()-=+@:,漢字.png')
assert_equal 'sample-_25_26_25_28_25_29-_25_3D_25_2B_25_40_25_3A_25_2C_25_E6_25_BC_25_A2_25_E5_25_AD_25_97-png', content.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
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>')

Dir.chdir(tmpdir) do
Dir.mkdir('test')
yield(epubmaker, File.join(tmpdir, 'test'))
end
end
end

def test_copy_static_file
epubmaker_instance do |epubmaker, tmpdir|
epubmaker.config['stylesheet'] = ['exist.css']
assert_nothing_raised { epubmaker.copy_stylesheet(tmpdir) }

epubmaker.config['stylesheet'] = ['nothing.css']
e = assert_raise(ApplicationError) { epubmaker.copy_stylesheet(tmpdir) }
assert_equal 'stylesheet: nothing.css is not found.', e.message
end

epubmaker_instance do |epubmaker, tmpdir|
epubmaker.config['titlepage'] = true
epubmaker.config['titlefile'] = 'exist.html'
assert_nothing_raised { epubmaker.copy_frontmatter(tmpdir) }

epubmaker.config['titlefile'] = 'nothing.html'
e = assert_raise(ApplicationError) { epubmaker.copy_frontmatter(tmpdir) }
assert_equal 'titlefile: nothing.html is not found.', e.message
end

# XXX: only `cover' is allowed to have invalid file name.
%w[originaltitlefile creditfile].each do |name|
epubmaker_instance do |epubmaker, tmpdir|
epubmaker.config[name] = 'exist.html'
assert_nothing_raised { epubmaker.copy_frontmatter(tmpdir) }

epubmaker.config[name] = 'nothing.html'
e = assert_raise(ApplicationError) { epubmaker.copy_frontmatter(tmpdir) }
assert_equal "#{name}: nothing.html is not found.", e.message
end
end

%w[profile advfile colophon backcover].each do |name|
epubmaker_instance do |epubmaker, tmpdir|
epubmaker.config[name] = 'exist.html'
assert_nothing_raised { epubmaker.copy_backmatter(tmpdir) }

epubmaker.config[name] = 'nothing.html'
e = assert_raise(ApplicationError) { epubmaker.copy_backmatter(tmpdir) }
assert_equal "#{name}: nothing.html is not found.", e.message
end
end
end
end