Class: SvgInlineFileExtractor::InlineImage

Inherits:
Object
  • Object
show all
Defined in:
lib/svg_inline_file_extractor/inline_image.rb

Constant Summary collapse

DATA_IMAGE_HEADER_PATTERN =

Regex pattern to match against the beginning of the href string. the type, if found, will be the raw string, eg: jpeg, jpg, png, etc.

/data:image\/(?<type>.+);base64\,/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nokogiri_element) ⇒ InlineImage

Returns a new instance of InlineImage.

Parameters:

  • nokogiri_element (Nokogiri::XML::Attr)

    the href node from the SVG file



15
16
17
18
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 15

def initialize(nokogiri_element)
  self.nokogiri_element = nokogiri_element
  self.href_contents = nokogiri_element.value
end

Instance Attribute Details

#href_contentsObject



12
13
14
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 12

def href_contents
  @href_contents
end

#nokogiri_elementObject



12
13
14
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 12

def nokogiri_element
  @nokogiri_element
end

Instance Method Details

#binary_imageString

Note:

I’m not sure if memoization is the best here, let’s see what happens in the wild.

Returns Base64 decoded binary image from href.

Returns:

  • (String)

    Base64 decoded binary image from href



61
62
63
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 61

def binary_image
  @binary_image ||= Base64.decode64(without_header)
end

#declared_image_typeString|Nil

Returns The image type according to the DATA_IMAGE_HEADER_PATTERN, nil if pattern not matched.

Returns:

  • (String|Nil)

    The image type according to the DATA_IMAGE_HEADER_PATTERN, nil if pattern not matched.



21
22
23
24
25
26
27
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 21

def declared_image_type
  @declared_image_type ||= begin
    if (match = href_contents.match(DATA_IMAGE_HEADER_PATTERN))
      match[:type]
    end
  end
end

#element_classString | nil

Returns the value of the class attribute in the parent element.

Returns:

  • (String | nil)

    the value of the class attribute in the parent element



40
41
42
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 40

def element_class
  @element_class ||= get_attribute('class')
end

#element_idString | nil

Returns the value of the id attribute in the parent element.

Returns:

  • (String | nil)

    the value of the id attribute in the parent element



35
36
37
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 35

def element_id
  @element_id ||= get_attribute('id')
end

#get_attribute(attribute) ⇒ String | Nil

Returns the value in the specified attribute, or nil if not set.

Parameters:

  • attribute (String)

    an attribute to retreive from the parent element (the entity that this href came from)

Returns:

  • (String | Nil)

    the value in the specified attribute, or nil if not set



46
47
48
49
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 46

def get_attribute(attribute)
  attribute = nokogiri_element.parent.attribute(attribute)
  attribute.value if attribute
end

#inline_image?Boolean

Returns true if declared_image_type is not nil.

Returns:

  • (Boolean)

    true if declared_image_type is not nil



30
31
32
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 30

def inline_image?
  !!declared_image_type
end

#set_binary_image_from_uri!Boolean

If the image href is set to a URI, attempt to download/open and convert to an

base64 encoded inline image

Returns:

  • (Boolean)

    true

Raises:



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 69

def set_binary_image_from_uri!
  unless SvgInlineFileExtractor.use_mini_magick?
    raise MiniMagickMissing, '#set_binary_image_from_uri! requires the MiniMagick gem to be installed.'
  end
  SvgInlineFileExtractor.with_temp_image(nokogiri_element.value) do |temp_image|
    format = SvgInlineFileExtractor.identify_image(temp_image).to_s.downcase
    nokogiri_element.value = "data:image/#{format};base64,#{encode(temp_image)}"
    nokogiri_element.name = 'xlink:href'
  end
  true
end