Class: Thepub::Epub::OCF

Inherits:
Object
  • Object
show all
Defined in:
lib/thepub/epub/ocf.rb

Overview

OEBPS Container Format (OCF) 1.0 wrapper (see www.idpf.org/ocf/ocf1.0/download/ocf10.htm)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOCF

Returns a new instance of OCF.



13
14
15
# File 'lib/thepub/epub/ocf.rb', line 13

def initialize
  @items = []
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



17
18
19
# File 'lib/thepub/epub/ocf.rb', line 17

def items
  @items
end

Instance Method Details

#<<(item) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/thepub/epub/ocf.rb', line 19

def <<(item)
  if item.kind_of? ContainerItem
    @items << item
  elsif item.is_a? String
    @items << Item.new(item)
  else
    raise "Unsupported item class: #{item.class}"
  end
end

#saveObject



43
44
45
46
47
48
49
# File 'lib/thepub/epub/ocf.rb', line 43

def save
  meta_inf = 'META-INF'
  FileUtils.mkdir_p(meta_inf)
  File.open(File.join(meta_inf, 'container.xml'), 'w') do |f|
    f << to_xml
  end
end

#to_xmlObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/thepub/epub/ocf.rb', line 29

def to_xml
  out = ''
  builder = Builder::XmlMarkup.new(:target => out)
  builder.instruct!
  builder.container :xmlns => "urn:oasis:names:tc:opendocument:xmlns:container", :version => "1.0" do
    builder.rootfiles do
      @items.each do |item|
        builder.rootfile 'full-path' => item.file_path, 'media-type' => item.media_type
      end
    end
  end
  out
end

#zip(output_path) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/thepub/epub/ocf.rb', line 51

def zip(output_path)
  File.open('mimetype', 'w') do |f|
    f << 'application/epub+zip'
  end
  # mimetype has to be first in the archive
  %x(zip -X9 \"#{output_path}\" mimetype)
  %x(zip -Xr9D \"#{output_path}\" * -xi mimetype)
end