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#mergeparams. version takes EPUB version (default is 2).



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

def initialize(params=nil, version=nil)
  @contents = []
  @params = {}
  @epub = nil
  @params["epubversion"] = version unless version.nil?
  
  unless params.nil?
    mergeparams(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

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

#colophon(wobj) ⇒ Object

Write colophon file to IO object wobj.



114
115
116
117
# File 'lib/epubmaker/producer.rb', line 114

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.



94
95
96
97
# File 'lib/epubmaker/producer.rb', line 94

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.



102
103
104
105
# File 'lib/epubmaker/producer.rb', line 102

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

#importImageInfo(path, base = nil) ⇒ Object

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/epubmaker/producer.rb', line 127

def importImageInfo(path, base=nil)
  Dir.foreach(path) do |f|
    next if f =~ /\A\./
    if f =~ /\.(png|jpg|jpeg|svg|gif)\Z/i  # FIXME:EPUB3 accepts more types...
      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}")
      importImageInfo("#{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)
  mergeparams(@params.merge(YAML.load_file(file)))
end

#mergeparams(params) ⇒ Object

Update parameters by merging from new parameter hash params.



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

def mergeparams(params)
  @params = @params.merge(params)
  complement
  @res = EPUBMaker::Resource.new(@params)
  
  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
end

#mimetype(wobj) ⇒ Object

Write mimetype file to IO object wobj.



75
76
77
78
# File 'lib/epubmaker/producer.rb', line 75

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.



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

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.



88
89
90
91
# File 'lib/epubmaker/producer.rb', line 88

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.



81
82
83
84
# File 'lib/epubmaker/producer.rb', line 81

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.



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

def produce(epubfile, basedir=nil, tmpdir=nil)
  current = Dir.pwd
  basedir = current if basedir.nil?
  # FIXME: produce cover, mytoc, titlepage, colophon?
  
  _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.



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

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