Class: GEPUB::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/gepub/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ Generator

Returns a new instance of Generator.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gepub/generator.rb', line 13

def initialize(title)
  warn('GEPUB::Generator is obsolete. use GEPUB::Book instead.')
  @metadata = Hash.new
  @manifest = Hash.new
  @spine = Array.new
  @toc = Array.new
  @metadata[:title] = title
  @manifest['ncx'] = { :href => 'toc.ncx', :mediatype => 'application/x-dtbncx+xml' }
  @contents_prefix = "" # may insert "OEBPS/"
  @locale = 'en'
end

Instance Attribute Details

#spineObject

Returns the value of attribute spine.



11
12
13
# File 'lib/gepub/generator.rb', line 11

def spine
  @spine
end

Instance Method Details

#addManifest(id, href, mediatype) ⇒ Object



85
86
87
# File 'lib/gepub/generator.rb', line 85

def addManifest(id, href, mediatype)
  @manifest[id] = { :href => href, :mediatype => mediatype }
end

#addNav(id, text, ref) ⇒ Object



89
90
91
# File 'lib/gepub/generator.rb', line 89

def addNav(id, text, ref)
  @toc.push({ :id => id, :text => text, :ref => ref})
end

#authorObject



37
38
39
# File 'lib/gepub/generator.rb', line 37

def author
  @metadata[:creator]
end

#author=(author) ⇒ Object



41
42
43
# File 'lib/gepub/generator.rb', line 41

def author=(author)
  @metadata[:creator] = author
end

#container_xmlObject



143
144
145
146
147
148
149
150
151
152
# File 'lib/gepub/generator.rb', line 143

def container_xml
  <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
  <rootfiles>
<rootfile full-path="#{@contents_prefix}content.opf" media-type="application/oebps-package+xml"/>
  </rootfiles>
</container>
EOF
end

#contents_prefix=(prefix) ⇒ Object



25
26
27
# File 'lib/gepub/generator.rb', line 25

def contents_prefix=(prefix)
  @contents_prefix =  prefix + "/"
end

#contributorObject



45
46
47
# File 'lib/gepub/generator.rb', line 45

def contributor
  @metadata[:contributor]
end

#contributor=(contributor) ⇒ Object



49
50
51
# File 'lib/gepub/generator.rb', line 49

def contributor=(contributor)
  @metadata[:contributor] = contributor
end

#create(destdir) ⇒ Object



97
98
99
100
101
102
# File 'lib/gepub/generator.rb', line 97

def create(destdir)
  create_mimetype(destdir)
  create_container(destdir)
  create_toc(destdir)
  create_opf(destdir)
end

#create_container(destdir) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/gepub/generator.rb', line 154

def create_container(destdir)
  infdir = destdir + "/META-INF"
  Dir.mkdir(infdir) if !File.exist?(infdir)

  File.open(infdir + "/container.xml", 'w') {
    |file|
    file << container_xml
  }
end

#create_epub(destdir, targetdir, epubname = ) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gepub/generator.rb', line 104

def create_epub(destdir, targetdir, epubname = @metadata[:title])
  realtarget = File::expand_path(targetdir)

  FileUtils.cd("#{destdir}") do
    |dir|
    epubname = "#{realtarget}/#{epubname}.epub"
    File.delete(epubname) if File.exist?(epubname)
    
    Zip::ZipOutputStream::open(epubname) {
      |epub|
      epub.put_next_entry('mimetype', '', '', Zip::ZipEntry::STORED)
      epub << 'application/epub+zip'

      Dir["**/*"].each do
        |f|
        if File.basename(f) != 'mimetype' && !File.directory?(f)
          File.open(f,'rb') do
            |file|
            epub.put_next_entry(f)
            epub << file.read
          end
        end
      end
    }
  end
end

#create_mimetype(destdir) ⇒ Object



136
137
138
139
140
141
# File 'lib/gepub/generator.rb', line 136

def create_mimetype(destdir)
  File.open(destdir + '/mimetype', 'w') {
    | file |
    file << mimetype_contents
  }
end

#create_opf(destdir) ⇒ Object



214
215
216
# File 'lib/gepub/generator.rb', line 214

def create_opf(destdir)
  File.open(destdir + "/" + @contents_prefix + "content.opf", 'w') { | file | file << opf_xml }
end

#create_toc(destdir) ⇒ Object



267
268
269
270
271
272
# File 'lib/gepub/generator.rb', line 267

def create_toc(destdir)
  File.open(destdir + "/" + @contents_prefix + "toc.ncx", 'w') {
    |file|
    file << ncx_xml
  }
end

#dateObject



61
62
63
# File 'lib/gepub/generator.rb', line 61

def date
  @metadata[:date]
end

#date=(date) ⇒ Object



65
66
67
# File 'lib/gepub/generator.rb', line 65

def date=(date)
  @metadata[:date] = date
end

#identifierObject



69
70
71
# File 'lib/gepub/generator.rb', line 69

def identifier
  @metadata[:itentifier]
end

#identifier=(id) ⇒ Object



73
74
75
# File 'lib/gepub/generator.rb', line 73

def identifier=(id)
  @metadata[:identifier] = id
end

#localeObject



77
78
79
# File 'lib/gepub/generator.rb', line 77

def locale
  @locale
end

#locale=(locale) ⇒ Object



81
82
83
# File 'lib/gepub/generator.rb', line 81

def locale=(locale)
  @locale = locale
end

#mimetype_contentsObject



131
132
133
134
135
# File 'lib/gepub/generator.rb', line 131

def mimetype_contents
  <<EOF
application/epub+zip
EOF
end

#ncx_xmlObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/gepub/generator.rb', line 218

def ncx_xml
  result = XML::Document.new
  result.root = XML::Node.new('ncx')
  root = result.root
  XML::Namespace.new(root, nil, "http://www.daisy.org/z3986/2005/ncx/")
  root['version'] = "2005-1"
  root << head = XML::Node.new('head')
  head << uid = XML::Node.new('meta')
  uid['name'] = 'dtb:uid'
  uid['content'] = "#{@metadata[:identifier]}"

  head << depth = XML::Node.new('meta')
  depth['name'] = 'dtb:depth'
  depth['content'] = '1'

  head << totalPageCount = XML::Node.new('meta')
  totalPageCount['name'] = 'dtb:totalPageCount'
  totalPageCount['content'] = '0'

  head << maxPageNumber = XML::Node.new('meta')
  maxPageNumber['name'] = 'dtb:maxPageNumber'
  maxPageNumber['content'] = '0'


  root << docTitle = XML::Node.new('docTitle')
  docTitle << XML::Node.new('text', "#{@metadata[:title]}")
  
  root << nav_map = XML::Node.new('navMap')
  count = 1
  @toc.each {
    |x|
    nav_point = XML::Node.new('navPoint')
    nav_point['id'] = "#{x[:id]}"
    nav_point['playOrder'] = "#{count}"
    
    nav_label = XML::Node.new('navLabel')
    nav_label << XML::Node.new('text', "#{x[:text]}")
    
    nav_content = XML::Node.new('content')
    nav_content['src'] = "#{x[:ref]}"
    count = count + 1

    nav_map << nav_point
    nav_point << nav_label
    nav_point << nav_content
  }
  result.to_s
end

#opf_xmlObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/gepub/generator.rb', line 164

def opf_xml
  result = XML::Document.new
  result.root = XML::Node.new('package')
  package = result.root
  XML::Namespace.new(package, nil, 'http://www.idpf.org/2007/opf')
  package['version'] = '2.0'
  package['unique-identifier'] = 'BookID'


  package << metadataelem = XML::Node.new('metadata')
  XML::Namespace.new(metadataelem, 'opf', 'http://www.idpf.org/2007/opf')
  XML::Namespace.new(metadataelem, 'dc', "http://purl.org/dc/elements/1.1/")

  metadataelem << XML::Node.new('dc:language', @locale)

  @metadata.each { | k, v |
    if (k == :cover)
      metadataelem << node = XML::Node.new("meta")
      node['name'] = 'cover'
      node['content'] = v
    else
      metadataelem << node = XML::Node.new("dc:#{k}",v)
      if (k == :identifier)
        node['id'] = 'BookID'
        node['opf:scheme'] = 'URL'
      end
    end
  }

  package << manifestelem = XML::Node.new('manifest')
  @manifest.each {|k,v|
    manifestelem << node = XML::Node.new("item")
    node['id'] = "#{k}"
    node['href'] = "#{v[:href]}"
    node['media-type'] = "#{v[:mediatype]}" 
  }
  
  package << spineelem = XML::Node.new('spine')
  spineelem['toc'] = 'ncx'

  @spine.each {
    |v|
    spineelem << node = XML::Node.new('itemref')
    node['idref'] = "#{v}"
  }

  result.to_s

end

#publisherObject



53
54
55
# File 'lib/gepub/generator.rb', line 53

def publisher
  @metadata[:publisher]
end

#publisher=(publisher) ⇒ Object



57
58
59
# File 'lib/gepub/generator.rb', line 57

def publisher=(publisher)
  @metadata[:publisher] = publisher
end

#specifyCoverImage(id) ⇒ Object



93
94
95
# File 'lib/gepub/generator.rb', line 93

def specifyCoverImage(id)
  @metadata[:cover] = id
end

#titleObject



29
30
31
# File 'lib/gepub/generator.rb', line 29

def title
  @metadata[:title]
end

#title=(title) ⇒ Object



33
34
35
# File 'lib/gepub/generator.rb', line 33

def title=(title)
  @metadata[:title] = title
end