Skip to content

Commit

Permalink
Merge pull request #1914 from kmuto/staticfiles-in-subdir
Browse files Browse the repository at this point in the history
support to load static files from subdir
  • Loading branch information
kmuto authored Aug 1, 2024
2 parents 2f993b3 + ce9d53b commit b070710
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/review/epubmaker.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2010-2023 Kenshi Muto and Masayoshi Takahashi
# Copyright (c) 2010-2024 Kenshi Muto and Masayoshi Takahashi
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -523,7 +523,7 @@ def copy_stylesheet(basetmpdir)
end

def copy_static_file(configname, destdir, destfilename: nil)
destfilename ||= @config[configname]
destfilename ||= File.basename(@config[configname])
unless File.exist?(@config[configname])
error! "#{configname}: #{@config[configname]} is not found."
end
Expand Down
22 changes: 11 additions & 11 deletions lib/review/epubmaker/epubcommon.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# = epubcommon.rb -- super class for EPUBv2 and EPUBv3
#
# Copyright (c) 2010-2023 Kenshi Muto and Masayoshi Takahashi
# Copyright (c) 2010-2024 Kenshi Muto and Masayoshi Takahashi
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -320,19 +320,19 @@ def flat_ncx(type, indent = nil)
end

def produce_write_common(basedir, tmpdir)
File.write("#{tmpdir}/mimetype", mimetype)
File.write(File.join(tmpdir, 'mimetype'), mimetype)

FileUtils.mkdir_p("#{tmpdir}/META-INF")
File.write("#{tmpdir}/META-INF/container.xml", container)
FileUtils.mkdir_p(File.join(tmpdir, 'META-INF'))
File.write(File.join(tmpdir, 'META-INF', 'container.xml'), container)

FileUtils.mkdir_p("#{tmpdir}/OEBPS")
FileUtils.mkdir_p(File.join(tmpdir, 'OEBPS'))
File.write(File.join(tmpdir, opf_path), opf)

if config['cover']
if File.exist?("#{basedir}/#{config['cover']}")
FileUtils.cp("#{basedir}/#{config['cover']}", "#{tmpdir}/OEBPS")
if File.exist?(File.join(basedir, File.basename(config['cover'])))
FileUtils.cp(File.join(basedir, File.basename(config['cover'])), File.join(tmpdir, 'OEBPS'))
else
File.write("#{tmpdir}/OEBPS/#{config['cover']}", cover)
File.write(File.join(tmpdir, 'OEBPS', File.basename(config['cover'])), cover)
end
end

Expand All @@ -344,13 +344,13 @@ def produce_write_common(basedir, tmpdir)
contents.each do |item|
next if /#/.match?(item.file) # skip subgroup

fname = "#{basedir}/#{item.file}"
fname = File.join(basedir, item.file)
unless File.exist?(fname)
raise ApplicationError, "#{fname} is not found."
end

FileUtils.mkdir_p(File.dirname("#{tmpdir}/OEBPS/#{item.file}"))
FileUtils.cp(fname, "#{tmpdir}/OEBPS/#{item.file}")
FileUtils.mkdir_p(File.dirname(File.join(tmpdir, 'OEBPS', item.file)))
FileUtils.cp(fname, File.join(tmpdir, 'OEBPS', item.file))
end
end

Expand Down
12 changes: 11 additions & 1 deletion lib/review/pdfmaker.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2010-2023 Kenshi Muto and Masayoshi Takahashi
# Copyright (c) 2010-2024 Kenshi Muto and Masayoshi Takahashi
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -339,6 +339,7 @@ def make_custom_page(file)
File.read(file_sty)
else
warn "File #{file_sty} is not found."
nil
end
end

Expand Down Expand Up @@ -505,6 +506,15 @@ def template_content
template_dir = @basedir
template_path = 'layouts/layout.tex.erb'
end

if @config['cover'] && !File.exist?(@config['cover'])
error! "File #{@config['cover']} is not found."
end

if @config['titlepage'] && @config['titlefile'] && !File.exist?(@config['titlefile'])
error! "File #{@config['titlefile']} is not found."
end

ReVIEW::Template.generate(path: template_path, mode: '-', binding: binding, template_dir: template_dir)
rescue StandardError => e
if defined?(e.full_message)
Expand Down
2 changes: 1 addition & 1 deletion review.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |gem|
gem.add_dependency('rouge')
gem.add_dependency('rubyzip')
gem.add_dependency('tty-logger')
gem.add_development_dependency('mini_magick')
gem.add_development_dependency('mini_magick', '~> 5.0.0')
gem.add_development_dependency('playwright-runner')
gem.add_development_dependency('pygments.rb')
gem.add_development_dependency('rake')
Expand Down
12 changes: 12 additions & 0 deletions test/test_epubmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,9 @@ def error(s)
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.chdir(tmpdir) do
Dir.mkdir('test')
yield(epubmaker, File.join(tmpdir, 'test'))
Expand All @@ -897,6 +900,9 @@ def test_copy_static_file
epubmaker.config['titlefile'] = 'exist.html'
assert_nothing_raised { epubmaker.copy_frontmatter(tmpdir) }

epubmaker.config['titlefile'] = 'subdir/exist.html'
assert_nothing_raised { epubmaker.copy_frontmatter(tmpdir) }

epubmaker.config['titlefile'] = 'nothing.html'
@log_io.string = ''
assert_raise(SystemExit) { epubmaker.copy_frontmatter(tmpdir) }
Expand All @@ -909,6 +915,9 @@ def test_copy_static_file
epubmaker.config[name] = 'exist.html'
assert_nothing_raised { epubmaker.copy_frontmatter(tmpdir) }

epubmaker.config[name] = 'subdir/exist.html'
assert_nothing_raised { epubmaker.copy_frontmatter(tmpdir) }

epubmaker.config[name] = 'nothing.html'
@log_io.string = ''
assert_raise(SystemExit) { epubmaker.copy_frontmatter(tmpdir) }
Expand All @@ -921,6 +930,9 @@ def test_copy_static_file
epubmaker.config[name] = 'exist.html'
assert_nothing_raised { epubmaker.copy_backmatter(tmpdir) }

epubmaker.config[name] = 'subdir/exist.html'
assert_nothing_raised { epubmaker.copy_backmatter(tmpdir) }

epubmaker.config[name] = 'nothing.html'
@log_io.string = ''
assert_raise(SystemExit) { epubmaker.copy_backmatter(tmpdir) }
Expand Down
3 changes: 2 additions & 1 deletion test/test_img_math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def test_make_math_image
private

def compare_images(image1, image2)
compare = MiniMagick::Tool::Compare.new(whiny: false)
MiniMagick.errors = false
compare = MiniMagick::Tool::Compare.new
compare << '-fuzz'
compare << '10%'
compare.metric('AE')
Expand Down

0 comments on commit b070710

Please sign in to comment.