Class: BbEPUB::Transform::Metadata

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

Overview

Takes all the elements in the <metadata> tag of the OPF file and generates a clean hash of the data. Other transforms will pick and choose from this hash. What’s left over is mostly just for preserving EPUB’s guff.

So this:

<metadata>
  <dc:identifier id="isbn-id">
    urn:isbn:9780101010101
  </dc:identifier>
  <meta
    refines="#isbn-id"
    property="identifier-type"
    scheme="onix:codelist5"
  >
    15
  </meta>
  <dc:source id="src-id">
    urn:isbn:9780375704024
  </dc:source>
  <meta
    refines="#src-id"
    property="identifier-type"
    scheme="onix:codelist5"
  >
    15
  </meta>
</metadata>

… becomes this:

"metadata": {
  "identifier": [{
    "@": "urn:isbn:9780101010101",
    "identifier-type": {
      "@": 15,
      "scheme": {
        "@": "onix-codelist5"
      }
    }
  }],
  "source": [{
    "@": "urn:isbn:9780375704024",
    "identifier-type": {
      "@": 15,
      "scheme": {
        "@": "onix-codelist5"
      }
    }
  }]
}

You can nest properties arbitrarily deep in the map’s “metadata” structure.

"metadata": {
  "some-property": [{
    "@": "value-of-some-property",
    "some-property-property": {
      "@": "value-of-some-property-property",
      "some-property-property-property": {
        "@": "value-of-etc"
      }
    }
  }],
  "other-property": [
    { "@": "other-property-first-value" },
    { "@": "other-property-second-value" }
  ]
}

Instance Method Summary collapse

Instance Method Details

#dependenciesObject



75
76
77
# File 'lib/bb-epub/transform/metadata.rb', line 75

def dependencies
  [BbEPUB::Transform::OPF]
end

#from_map(package) ⇒ Object



133
134
# File 'lib/bb-epub/transform/metadata.rb', line 133

def from_map(package)
end

#to_map(package) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
130
# File 'lib/bb-epub/transform/metadata.rb', line 80

def to_map(package)
  opf_doc = package.file(:opf).document('r')
  md = {}
  opf_doc.each('opf|metadata > *') { |tag|
    # If this tag refines another metadata tag, we can skip it
    if refines = tag['refines']
      refines = "opf|metadata > *[id=\"#{refines.sub(/^#/,'')}\"]"
      next if opf_doc.find(refines)
    end

    # Find the basic name and value of the meta tag
    name = tag.node_name
    value = { '@' => tag.content.strip }
    # If the meta tag has attributes, add them to the value
    # (but note that these are deprecated in EPUB3 in favor
    # of refinements).
    tag.attributes.each_pair { |key, attr|
      value[key] = { '@' => attr.value }
      value[key]['deprecated'] = true  unless key == 'id' || key == 'refines'
    }
    if name == 'meta'
      if tag['property']
        name = tag['property']
        value.delete('property')
      elsif tag['name']
        # Note that <meta name="" content=""> is deprecated in EPUB3.
        name = tag['name']
        value['@'] = tag['content']
        value['deprecated'] = true
        value.delete('name')
      end
    end
    # If the meta tag is refined by another meta tag, add the
    # refinements to our value hash.
    if tag['id']
      refines = opf_doc.search(
        'opf|metadata > opf|meta[refines="#'+tag['id']+'"]'
      )
      refines.each { |refinement|
        refine_value = { '@' => refinement.content.strip }
        refinement.attributes.each_pair { |key, attr|
          refine_value[key] = attr.value
        }
        value[refinement['property']] = refine_value
      }
    end
    # Add the name and value to our metadata hash
    add_to_hash(md, name, value)
  }
  package.map['metadata'] = md
end