Class: EPUBMaker::Producer

Inherits:
Object show all
Defined in:
lib/epubmaker/producer.rb

Overview

EPUBMaker produces EPUB file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, version = nil) ⇒ Producer

Construct producer object. config takes initial parameter hash. This parameters can be overriden by EPUBMaker#load or EPUBMaker#merge_config. version takes EPUB version (default is 2).



55
56
57
58
59
60
61
62
63
64
# File 'lib/epubmaker/producer.rb', line 55

def initialize(config = nil, version = nil)
  @contents = []
  @config = ReVIEW::Configure.new
  @epub = nil
  @config['epubversion'] = version unless version.nil?
  @res = ReVIEW::I18n
  @logger = ReVIEW.logger

  merge_config(config) if config
end

Instance Attribute Details

#configObject

Parameter hash.



26
27
28
# File 'lib/epubmaker/producer.rb', line 26

def config
  @config
end

#contentsObject

Array of content objects.



24
25
26
# File 'lib/epubmaker/producer.rb', line 24

def contents
  @contents
end

#resObject (readonly)

Message resource object.



28
29
30
# File 'lib/epubmaker/producer.rb', line 28

def res
  @res
end

Class Method Details

.load(file) ⇒ Object

Take YAML file and return parameter hash.



31
32
33
34
35
36
37
# File 'lib/epubmaker/producer.rb', line 31

def self.load(file)
  if file.nil? || !File.exist?(file)
    raise "Can't open #{file}."
  end
  loader = ReVIEW::YAMLLoader.new
  loader.load_file(file)
end

Instance Method Details

#call_hook(filename, *params) ⇒ Object



211
212
213
214
215
216
217
218
# File 'lib/epubmaker/producer.rb', line 211

def call_hook(filename, *params)
  return if !filename.present? || !File.exist?(filename) || !FileTest.executable?(filename)
  if ENV['REVIEW_SAFE_MODE'].to_i & 1 > 0
    warn 'hook is prohibited in safe mode. ignored.'
  else
    system(filename, *params)
  end
end

#colophon(wobj) ⇒ Object

Write colophon file to IO object wobj.



150
151
152
153
154
155
# File 'lib/epubmaker/producer.rb', line 150

def colophon(wobj)
  s = @epub.colophon
  if !s.nil? && !wobj.nil?
    wobj.puts s
  end
end

#container(wobj) ⇒ Object

Write container file to IO object wobj.



123
124
125
126
127
128
# File 'lib/epubmaker/producer.rb', line 123

def container(wobj)
  s = @epub.container
  if !s.nil? && !wobj.nil?
    wobj.puts s
  end
end

#cover(wobj) ⇒ Object

Write cover file to IO object wobj. If Producer#config is defined, it will be used for the cover image.



133
134
135
136
137
138
139
# File 'lib/epubmaker/producer.rb', line 133

def cover(wobj)
  type = @config['epubversion'] >= 3 ? 'cover' : nil
  s = @epub.cover(type)
  if !s.nil? && !wobj.nil?
    wobj.puts s
  end
end

#coverimageObject



66
67
68
69
70
71
72
73
74
# File 'lib/epubmaker/producer.rb', line 66

def coverimage
  return nil unless config['coverimage']
  @contents.each do |item|
    if item.media.start_with?('image') && item.file =~ /#{config['coverimage']}\Z/
      return item.file
    end
  end
  nil
end

#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.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/epubmaker/producer.rb', line 167

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 f =~ /\.(#{allow_exts.join('|')})\Z/i
      path.chop! if path =~ %r{/\Z}
      if base.nil?
        @contents.push(EPUBMaker::Content.new('file' => "#{path}/#{f}"))
      else
        @contents.push(EPUBMaker::Content.new('file' => "#{path.sub(base + '/', '')}/#{f}"))
      end
    end
    if FileTest.directory?("#{path}/#{f}")
      import_imageinfo("#{path}/#{f}", base)
    end
  end
end

#isbn_hyphenObject



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/epubmaker/producer.rb', line 220

def isbn_hyphen
  str = @config['isbn'].to_s

  if str =~ /\A\d{10}\Z/
    return "#{str[0..0]}-#{str[1..5]}-#{str[6..8]}-#{str[9..9]}"
  end
  if str =~ /\A\d{13}\Z/
    return "#{str[0..2]}-#{str[3..3]}-#{str[4..8]}-#{str[9..11]}-#{str[12..12]}"
  end
  nil
end

#load(file) ⇒ Object

Take YAML file and update parameter hash.



40
41
42
43
44
45
46
# File 'lib/epubmaker/producer.rb', line 40

def load(file)
  if file.nil? || !File.exist?(file)
    raise "Can't open #{file}."
  end
  loader = ReVIEW::YAMLLoader.new
  merge_config(@config.deep_merge(loader.load_file(file)))
end

#merge_config(config) ⇒ Object

Update parameters by merging from new parameter hash config.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/epubmaker/producer.rb', line 77

def merge_config(config)
  @config.deep_merge!(config)
  complement

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

#mimetype(wobj) ⇒ Object

Write mimetype file to IO object wobj.



98
99
100
101
102
103
# File 'lib/epubmaker/producer.rb', line 98

def mimetype(wobj)
  s = @epub.mimetype
  if !s.nil? && !wobj.nil?
    wobj.print s
  end
end

#mytoc(wobj) ⇒ Object

Write own toc file to IO object wobj.



158
159
160
161
162
163
# File 'lib/epubmaker/producer.rb', line 158

def mytoc(wobj)
  s = @epub.mytoc
  if !s.nil? && !wobj.nil?
    wobj.puts s
  end
end

#ncx(wobj, indentarray = []) ⇒ Object

Write ncx file to IO object wobj. indentarray defines prefix string for each level.



115
116
117
118
119
120
# File 'lib/epubmaker/producer.rb', line 115

def ncx(wobj, indentarray = [])
  s = @epub.ncx(indentarray)
  if !s.nil? && !wobj.nil?
    wobj.puts s
  end
end

#opf(wobj) ⇒ Object

Write opf file to IO object wobj.



106
107
108
109
110
111
# File 'lib/epubmaker/producer.rb', line 106

def opf(wobj)
  s = @epub.opf
  if !s.nil? && !wobj.nil?
    wobj.puts s
  end
end

#produce(epubfile, basedir = nil, tmpdir = nil) ⇒ Object

Produce EPUB file epubfile. basedir points the directory has contents (default: current directory.) tmpdir defines temporary directory.



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/epubmaker/producer.rb', line 191

def produce(epubfile, basedir = nil, tmpdir = nil)
  current = Dir.pwd
  basedir ||= current

  # use Dir to solve a path for Windows (see #1011)
  new_tmpdir = Dir[File.join(tmpdir.nil? ? Dir.mktmpdir : tmpdir)][0]
  if epubfile !~ %r{\A/}
    epubfile = "#{current}/#{epubfile}"
  end

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

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

#titlepage(wobj) ⇒ Object

Write title file (copying) to IO object wobj.



142
143
144
145
146
147
# File 'lib/epubmaker/producer.rb', line 142

def titlepage(wobj)
  s = @epub.titlepage
  if !s.nil? && !wobj.nil?
    wobj.puts s
  end
end

#warn(msg) ⇒ Object



48
49
50
# File 'lib/epubmaker/producer.rb', line 48

def warn(msg)
  @logger.warn(msg)
end