Class: EmbedXMP::XMP

Inherits:
Object
  • Object
show all
Defined in:
lib/embed_xmp/xmp.rb

Overview

XMP sidecars

Constant Summary collapse

XPACKET_PDATA =
"begin=\"\uFEFF\" id=\"W5M0MpCehiHzreSzNTczkc9d\""
XPACKET_START =
"<?xpacket #{XPACKET_PDATA}?>"
XPACKET_END_R =
'<?xpacket end="r"?>'
XPACKET_END_W =
'<?xpacket end="w"?>'

Instance Method Summary collapse

Constructor Details

#initialize(xmp_data_blob, writable: false, xpacked: false) ⇒ XMP

Returns a new instance of XMP.



20
21
22
23
24
# File 'lib/embed_xmp/xmp.rb', line 20

def initialize(xmp_data_blob, writable: false, xpacked: false)
  @xmp = xmp_str = xmp_xml_to_str(xmp_data_blob)

  @xmp = xpack_sidecar(xmp_str, writable: writable) unless xpacked
end

Instance Method Details

#to_sObject

Return XMP sidecar as String.



27
28
29
# File 'lib/embed_xmp/xmp.rb', line 27

def to_s
  xmp_xml_to_str(@xmp)
end

#xmp_xml_to_str(xmp_data) ⇒ Object

Read XML-formatted XMP data into a format suitable for embedding.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/embed_xmp/xmp.rb', line 32

def xmp_xml_to_str(xmp_data)
  xmp = Nokogiri::XML(xmp_data) do |conf|
    conf.options = Nokogiri::XML::ParseOptions::NOBLANKS
  end

  raise 'XMPIsMalformedXML' unless xmp.errors.empty?

  xmp.to_xml(indent: 0,
             encoding: 'utf-8',
             save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
end

#xpack_sidecar(xmp_data, writable: false) ⇒ Object

Wrap XMP sidecar data in in an magic XML processing instruction (xpacket). writable is approperiate for file formats where the XMP data can be modified in place by staying within the confines of the XPACKET. (E.g. file formats with no chunk checksums.)



48
49
50
51
52
# File 'lib/embed_xmp/xmp.rb', line 48

def xpack_sidecar(xmp_data, writable: false)
  xpacket_end = writable ? XPACKET_END_W : XPACKET_END_R

  "#{XPACKET_START}\n#{xmp_data}\n#{xpacket_end}"
end