Class: BbEPUB::Transform::PackageIdentifier

Inherits:
Bookbinder::Transform
  • Object
show all
Defined in:
lib/bb-epub/transform/package_identifier.rb

Overview

See: www.idpf.org/epub/30/spec/epub30-publications.html#sec-opf-metadata-identifiers-pid

“To redress this problem of identifying minor modifications and releases without changing the Unique Identifier, this specification defines the semantics for a Package Identifier, or means of distinguishing and sequentially ordering Publications with the same Unique Identifier. The Package Identifier is not an actual property in the package metadata section, but is a value that can be obtained from two required pieces of metadata: the Unique Identifier and the last modification date of the Publication.

“When the taken together, the combined value represents a unique identity that can be used to distinguish any particular version of an EPUB Manifestation from another. To ensure that a Package Identifier can be constructed, the Publication must include exactly one ‘modified` property containing the last modification date (see meta).”

Instance Method Summary collapse

Instance Method Details

#dependenciesObject



19
20
21
# File 'lib/bb-epub/transform/package_identifier.rb', line 19

def dependencies
  [BbEPUB::Transform::Metadata, BbEPUB::Transform::NCX]
end

#from_map(package) ⇒ Object



58
59
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
# File 'lib/bb-epub/transform/package_identifier.rb', line 58

def from_map(package)
  map_uid = package.map['unique-identifier']
  map_mod = package.map['modified']
  opf_doc = package.file(:opf).document
   = opf_doc.find('opf|metadata')

  # Unique identifier
  opf_doc.root['unique-identifier'] = 'unique-identifier'
  opf_doc.new_node('dc:identifier', :append => ) { |uid_tag|
    uid_tag['id'] = 'unique-identifier'
    uid_tag.content = map_uid
  }

  # Add the unique identifier to the NCX as well, because too
  # much data redundancy is never enough.
  package.if_file(:ncx) { |ncx_file|
    ncx_file.document.new_node('meta', :append => 'ncx|head') { |uid_meta_tag|
      uid_meta_tag['name'] = 'dtb:uid'
      uid_meta_tag['content'] = map_uid
    }
  }

  # Modified
  opf_doc.new_node('meta', :append => ) { |mod_tag|
    mod_tag['property'] = 'dcterms:modified'
    mod_tag.content = map_mod
  }
end

#to_map(package) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bb-epub/transform/package_identifier.rb', line 24

def to_map(package)
  opf_doc = package.file(:opf).document('r')

  # Unique identifier
  unless id_hashes = package.map['metadata']['identifier']
    package.warn('No identifiers found in package metadata')
  end
  if id_hashes && uid_id = opf_doc.root['unique-identifier']
    uid_hash = id_hashes.detect { |id_hash|
      id_hash['id'] && id_hash['id']['@'] == uid_id
    }
  else
    package.warn('OPF root "unique-identifier" attribute not found')
  end
  if uid_hash
    id_hashes.delete(uid_hash)
    package.map['metadata'].delete('identifier')  if id_hashes.empty?
    package.map['unique-identifier'] = uid_hash['@']
  else
    package.warn('OPF metadata for required "unique-identifier" not found')
  end

  # Modified
  begin
    mod_hash = package.map['metadata'].delete('dcterms:modified').first
    timestamp = Time.parse(mod_hash['@'])
  rescue
    package.warn('OPF metadata for "modified" not found - using now()')
    timestamp = Time.now
  end
  package.map['modified'] = timestamp.utc.iso8601
end