Class: ReVIEW::EPUBMaker::ZipExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/review/epubmaker/zip_exporter.rb

Overview

Export into zip file for EPUB producer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tmpdir, config) ⇒ ZipExporter

Returns a new instance of ZipExporter.



25
26
27
28
# File 'lib/review/epubmaker/zip_exporter.rb', line 25

def initialize(tmpdir, config)
  @tmpdir = tmpdir
  @config = config
end

Instance Attribute Details

#tmpdirObject (readonly)

Returns the value of attribute tmpdir.



23
24
25
# File 'lib/review/epubmaker/zip_exporter.rb', line 23

def tmpdir
  @tmpdir
end

Instance Method Details

#export_zip(epubfile) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/review/epubmaker/zip_exporter.rb', line 30

def export_zip(epubfile)
  if defined?(Zip)
    export_zip_rubyzip(epubfile)
  else
    export_zip_extcmd(epubfile)
  end
end

#export_zip_extcmd(epubfile) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/review/epubmaker/zip_exporter.rb', line 38

def export_zip_extcmd(epubfile)
  stage1 = @config['epubmaker']['zip_stage1'].to_s.split
  path1 = stage1[0] || 'zip'
  opt1 = stage1[1] || '-0Xq'
  stage2 = @config['epubmaker']['zip_stage2'].to_s.split
  path2 = stage2[0] || 'zip'
  opt2 = stage2[1] || '-Xr9Dq'

  Dir.chdir(tmpdir) do
    system(path1, opt1, epubfile, 'mimetype')
    addpath = @config['epubmaker']['zip_addpath']
    if addpath
      system(path2, opt2, epubfile, 'META-INF', 'OEBPS', addpath)
    else
      system(path2, opt2, epubfile, 'META-INF', 'OEBPS')
    end
  end
end

#export_zip_rubyzip(epubfile) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/review/epubmaker/zip_exporter.rb', line 57

def export_zip_rubyzip(epubfile)
  Dir.chdir(tmpdir) do
    Zip::OutputStream.open(epubfile) do |epub|
      root_pathname = Pathname.new(tmpdir)
      epub.put_next_entry('mimetype', nil, nil, Zip::Entry::STORED)
      epub << 'application/epub+zip'

      export_zip_rubyzip_addpath(epub, File.join(tmpdir, 'META-INF'), root_pathname)
      export_zip_rubyzip_addpath(epub, File.join(tmpdir, 'OEBPS'), root_pathname)
      if @config['zip_addpath'].present?
        export_zip_rubyzip_addpath(epub, File.join(tmpdir, @config['zip_addpath']), root_pathname)
      end
    end
  end
end

#export_zip_rubyzip_addpath(epub, dirname, rootdir) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/review/epubmaker/zip_exporter.rb', line 73

def export_zip_rubyzip_addpath(epub, dirname, rootdir)
  Dir[File.join(dirname, '**', '**')].each do |path|
    next if File.directory?(path)

    relpath = Pathname.new(path).relative_path_from(rootdir)
    epub.put_next_entry(relpath)
    epub << File.binread(path)
  end
end