Class: Repub::Epub::OPF

Inherits:
Object show all
Includes:
ContainerItem
Defined in:
lib/repub/epub/opf.rb

Overview

Open Packaging Format (OPF) 2.0 wrapper (see www.idpf.org/2007/opf/OPF_2.0_final_spec.html)

Defined Under Namespace

Classes: Metadata

Instance Attribute Summary collapse

Attributes included from ContainerItem

#file_path, #media_type

Instance Method Summary collapse

Methods included from ContainerItem

#document?

Constructor Details

#initialize(uid, file_path = 'package.opf') ⇒ OPF

Returns a new instance of OPF.



13
14
15
16
17
18
19
# File 'lib/repub/epub/opf.rb', line 13

def initialize(uid, file_path = 'package.opf')
  @file_path = file_path
  @media_type = 'application/oebps-package+xml'
  @metadata = Metadata.new('Untitled', 'en', uid, Date.today.to_s)
  @items = []
  @ncx = nil
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



76
77
78
# File 'lib/repub/epub/opf.rb', line 76

def items
  @items
end

#metadataObject (readonly)

Returns the value of attribute metadata.



75
76
77
# File 'lib/repub/epub/opf.rb', line 75

def 
  @metadata
end

Instance Method Details

#<<(item) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/repub/epub/opf.rb', line 78

def <<(item)
  if item.kind_of? NCX
    @ncx = item
  elsif 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



104
105
106
107
108
# File 'lib/repub/epub/opf.rb', line 104

def save
  File.open(@file_path, 'w') do |f|
    f << to_xml
  end
end

#to_xmlObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/repub/epub/opf.rb', line 90

def to_xml
  out = ''
  builder = Builder::XmlMarkup.new(:target => out)
  builder.instruct!
  builder.package :xmlns => "http://www.idpf.org/2007/opf",
      'unique-identifier' => "dcidid",
      'version' => "2.0" do
    @metadata.to_xml(builder)
    manifest_to_xml(builder)
    spine_to_xml(builder)
  end
  out
end