Class: EeePub::OPF

Inherits:
ContainerItem show all
Defined in:
lib/eeepub/opf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ContainerItem

#initialize, #save, #set_values, #to_xml

Constructor Details

This class inherits a constructor from EeePub::ContainerItem

Instance Attribute Details

#creatorObject

Returns the value of attribute creator.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def creator
  @creator
end

#dateObject

Returns the value of attribute date.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def date
  @date
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def description
  @description
end

#guideObject

Returns the value of attribute guide.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def guide
  @guide
end

#identifierObject

Returns the value of attribute identifier.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def identifier
  @identifier
end

#languageObject

Returns the value of attribute language.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def language
  @language
end

#manifestObject

Returns the value of attribute manifest.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def manifest
  @manifest
end

#ncxObject

Returns the value of attribute ncx.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def ncx
  @ncx
end

#publisherObject

Returns the value of attribute publisher.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def publisher
  @publisher
end

#relationObject

Returns the value of attribute relation.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def relation
  @relation
end

#rightsObject

Returns the value of attribute rights.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def rights
  @rights
end

#spineObject

Returns the value of attribute spine.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def spine
  @spine
end

#subjectObject

Returns the value of attribute subject.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def subject
  @subject
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def title
  @title
end

#tocObject

Returns the value of attribute toc.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def toc
  @toc
end

#unique_identifierObject

Returns the value of attribute unique_identifier.



3
4
5
# File 'lib/eeepub/opf.rb', line 3

def unique_identifier
  @unique_identifier
end

Instance Method Details

#build_guide(builder) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/eeepub/opf.rb', line 105

def build_guide(builder)
  return if guide.nil? || guide.empty?

  builder.guide do
    guide.each do |i|
      builder.reference convert_to_xml_attributes(i)
    end
  end
end

#build_manifest(builder) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/eeepub/opf.rb', line 89

def build_manifest(builder)
  builder.manifest do
    complete_manifest.each do |i|
      builder.item :id => i[:id], :href => i[:href], 'media-type' => i[:media_type]
    end
  end
end

#build_metadata(builder) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/eeepub/opf.rb', line 60

def (builder)
  builder. 'xmlns:dc' => "http://purl.org/dc/elements/1.1/",
    'xmlns:dcterms' => "http://purl.org/dc/terms/",
    'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
    'xmlns:opf' => "http://www.idpf.org/2007/opf" do

    identifier.each do |i|
      attrs = {}
      attrs['opf:scheme'] = i[:scheme] if i[:scheme]
      attrs[:id] = i[:id] if i[:id]
      builder.dc :identifier, i[:value], attrs
    end

    [:title, :language, :subject, :description, :relation, :creator, :publisher, :date, :rights].each do |i|
      value = self.send(i)
      next unless value

      [value].flatten.each do |v|
        case v
        when Hash
          builder.dc i, v[:value], convert_to_xml_attributes(v.reject {|k, v| k == :value})
        else
          builder.dc i, v
        end
      end
    end
  end
end

#build_spine(builder) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/eeepub/opf.rb', line 97

def build_spine(builder)
  builder.spine :toc => toc do
    spine.each do |i|
      builder.itemref :idref => i
    end
  end
end

#build_xml(builder) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/eeepub/opf.rb', line 48

def build_xml(builder)
  builder.package :xmlns => "http://www.idpf.org/2007/opf",
      'unique-identifier' => unique_identifier,
      'version' => "2.0" do

    (builder)
    build_manifest(builder)
    build_spine(builder)
    build_guide(builder)
  end
end

#complete_manifestObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/eeepub/opf.rb', line 115

def complete_manifest
  item_id_cache = {}

  result = manifest.map do |i|
    case i
    when String
      id = create_unique_item_id(i, item_id_cache)
      href = i
      media_type = guess_media_type(i)
    when Hash
      id = i[:id] || create_unique_item_id(i[:href], item_id_cache)
      href = i[:href]
      media_type = i[:media_type] || guess_media_type(i[:href])
    end
    {:id => id, :href => href, :media_type => media_type}
  end

  result += [{:id => 'ncx', :href => ncx, :media_type => 'application/x-dtbncx+xml'}] if ncx
  result
end

#create_unique_item_id(filename, id_cache) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/eeepub/opf.rb', line 136

def create_unique_item_id(filename, id_cache)
  basename = File.basename(filename)
  unless id_cache[basename]
    id_cache[basename] = 0
    name = basename
  else
    name = "#{basename}-#{id_cache[basename]}"
  end
  id_cache[basename] += 1
  name
end