Class: ReVIEW::EPUBMaker::Producer

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/review/epubmaker/producer.rb

Overview

EPUBMaker produces EPUB file.

Instance Attribute Summary collapse

Attributes included from Loggable

#logger

Instance Method Summary collapse

Methods included from Loggable

#app_error, #debug, #error, #error!, #warn

Constructor Details

#initialize(config) ⇒ Producer

Construct producer object. config takes initial parameter hash.



45
46
47
48
49
50
51
52
53
# File 'lib/review/epubmaker/producer.rb', line 45

def initialize(config)
  @contents = []
  @config = config
  @config.maker = 'epubmaker'
  @epub = nil
  @res = ReVIEW::I18n
  @logger = ReVIEW.logger
  modify_config
end

Instance Attribute Details

#configObject

Parameter hash.



39
40
41
# File 'lib/review/epubmaker/producer.rb', line 39

def config
  @config
end

#contentsObject

Array of content objects.



37
38
39
# File 'lib/review/epubmaker/producer.rb', line 37

def contents
  @contents
end

#resObject (readonly)

Message resource object.



41
42
43
# File 'lib/review/epubmaker/producer.rb', line 41

def res
  @res
end

Instance Method Details

#import_imageinfo(path, base = nil, allow_exts = nil) ⇒ Object Also known as: importImageInfo

Add informations of figure files in path to contents array. base defines a string to remove from path name.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/review/epubmaker/producer.rb', line 85

def import_imageinfo(path, base = nil, allow_exts = nil)
  return nil unless File.exist?(path)

  allow_exts ||= @config['image_ext']
  Dir.foreach(path) do |f|
    next if f.start_with?('.')

    if /\.(#{allow_exts.join('|')})\Z/i.match?(f)
      path.chop! if %r{/\Z}.match?(path)
      if base.nil?
        @contents.push(ReVIEW::EPUBMaker::Content.new(file: "#{path}/#{f}"))
      else
        @contents.push(ReVIEW::EPUBMaker::Content.new(file: "#{path.sub(base + '/', '')}/#{f}"))
      end
    end
    if FileTest.directory?("#{path}/#{f}")
      import_imageinfo("#{path}/#{f}", base)
    end
  end
end

#modify_configObject

Modify parameters for EPUB specific.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/review/epubmaker/producer.rb', line 56

def modify_config
  if @config['epubversion'] >= 3
    @config['htmlversion'] = 5
  end

  @config['title'] ||= @config['booktitle']
  @config['cover'] ||= "#{@config['bookname']}.#{@config['htmlext']}"

  %w[bookname title].each do |k|
    unless @config[k]
      raise "Key #{k} must have a value. Abort."
    end
  end

  case @config['epubversion'].to_i
  when 2
    @epub = ReVIEW::EPUBMaker::EPUBv2.new(self)
  when 3
    @epub = ReVIEW::EPUBMaker::EPUBv3.new(self)
  else
    raise "Invalid EPUB version (#{@config['epubversion']}.)"
  end

  ReVIEW::I18n.locale = @config['language']
  support_legacy_maker
end

#produce(epubfile, work_dir, tmpdir = nil, base_dir: nil) ⇒ Object

Produce EPUB file epubfile. work_dir points the directory has contents (default: current directory.) tmpdir defines temporary directory. base_dir is original root dir.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/review/epubmaker/producer.rb', line 112

def produce(epubfile, work_dir, tmpdir = nil, base_dir: nil)
  current = Dir.pwd
  base_dir ||= current

  # use Dir to solve a path for Windows (see #1011)
  new_tmpdir = Dir[File.join(tmpdir.nil? ? Dir.mktmpdir : tmpdir)][0]
  unless epubfile.start_with?('/')
    epubfile = "#{current}/#{epubfile}"
  end

  # FIXME: error check
  File.unlink(epubfile) if File.exist?(epubfile)

  begin
    @epub.produce(epubfile, work_dir, new_tmpdir, base_dir: base_dir)
  ensure
    FileUtils.rm_r(new_tmpdir) if tmpdir.nil?
  end
end