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(params = nil, version = nil) ⇒ Producer

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



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

def initialize(params=nil, version=nil)
  @contents = []
  @params = {}
  @epub = nil
  @params["epubversion"] = version unless version.nil?
  @res = ReVIEW::I18n

  unless params.nil?
    merge_params(params)
  end
end

Instance Attribute Details

#contentsObject

Array of content objects.



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

def contents
  @contents
end

#paramsObject

Parameter hash.



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

def params
  @params
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
# File 'lib/epubmaker/producer.rb', line 31

def Producer.load(file)
  raise "Can't open #{yamlfile}." if file.nil? || !File.exist?(file)
  return YAML.load_file(file)
end

Instance Method Details

#call_hook(filename, *params) ⇒ Object



185
186
187
188
189
190
191
192
193
# File 'lib/epubmaker/producer.rb', line 185

def call_hook(filename, *params)
  if !filename.nil? && 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
end

#colophon(wobj) ⇒ Object

Write colophon file to IO object wobj.



131
132
133
134
# File 'lib/epubmaker/producer.rb', line 131

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

#container(wobj) ⇒ Object

Write container file to IO object wobj.



110
111
112
113
# File 'lib/epubmaker/producer.rb', line 110

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

#cover(wobj) ⇒ Object

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



118
119
120
121
122
# File 'lib/epubmaker/producer.rb', line 118

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

#coverimageObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/epubmaker/producer.rb', line 57

def coverimage
  if !params["coverimage"]
    return nil
  end
  @contents.each do |item|
    if item.media =~ /\Aimage/ && item.file =~ /#{params["coverimage"]}\Z/ # /
      return item.file
    end
  end
  return 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.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/epubmaker/producer.rb', line 144

def import_imageinfo(path, base=nil, allow_exts=nil)
  return nil unless File.exist?(path)
  allow_exts = @params["image_ext"] if allow_exts.nil?
  Dir.foreach(path) do |f|
    next if f =~ /\A\./
    if f =~ /\.(#{allow_exts.join("|")})\Z/i
      path.chop! if path =~ /\/\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

#load(file) ⇒ Object

Take YAML file and update parameter hash.



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

def load(file)
  raise "Can't open #{yamlfile}." if file.nil? || !File.exist?(file)
  merge_params(@params.merge(YAML.load_file(file)))
end

#merge_params(params) ⇒ Object

Update parameters by merging from new parameter hash params.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/epubmaker/producer.rb', line 70

def merge_params(params)
  @params = @params.merge(params)
  complement

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

#mimetype(wobj) ⇒ Object

Write mimetype file to IO object wobj.



91
92
93
94
# File 'lib/epubmaker/producer.rb', line 91

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

#mytoc(wobj) ⇒ Object

Write own toc file to IO object wobj.



137
138
139
140
# File 'lib/epubmaker/producer.rb', line 137

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

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

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



104
105
106
107
# File 'lib/epubmaker/producer.rb', line 104

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

#opf(wobj) ⇒ Object

Write opf file to IO object wobj.



97
98
99
100
# File 'lib/epubmaker/producer.rb', line 97

def opf(wobj)
  s = @epub.opf
  wobj.puts s if !s.nil? && !wobj.nil?
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.



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

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

  _tmpdir = tmpdir.nil? ? Dir.mktmpdir : tmpdir
  epubfile = "#{current}/#{epubfile}" if epubfile !~ /\A\// # /

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

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

#titlepage(wobj) ⇒ Object

Write title file (copying) to IO object wobj.



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

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