Module: Asciidoctor::Image

Defined in:
lib/asciidoctor/pdf/ext/asciidoctor/image.rb

Constant Summary collapse

Base64Encoded =
::Module.new
DataUriRx =
%r(^data:image/(?<fmt>png|jpe?g|gif|pdf|bmp|tiff|svg\+xml);base64,(?<data>.*)$)
FormatAliases =
{ 'jpg' => 'jpeg', 'svg+xml' => 'svg' }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.format(image_path) ⇒ Object



9
10
11
# File 'lib/asciidoctor/pdf/ext/asciidoctor/image.rb', line 9

def self.format image_path
  ((ext = ::File.extname image_path).downcase.slice 1, ext.length)
end

.target_and_format(image_path, attributes = nil) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/asciidoctor/pdf/ext/asciidoctor/image.rb', line 13

def self.target_and_format image_path, attributes = nil
  if (image_path.start_with? 'data:') && (m = DataUriRx.match image_path)
    [(m[:data].extend Base64Encoded), (FormatAliases.fetch m[:fmt], m[:fmt])]
  else
    [image_path, attributes&.[]('format') || ((ext = ::File.extname image_path).downcase.slice 1, ext.length)]
  end
end

Instance Method Details

#target_and_formatObject



21
22
23
24
25
26
27
28
# File 'lib/asciidoctor/pdf/ext/asciidoctor/image.rb', line 21

def target_and_format
  image_path = inline? ? target : (attr 'target')
  if (image_path.start_with? 'data:') && (m = DataUriRx.match image_path)
    [(m[:data].extend Base64Encoded), (FormatAliases.fetch m[:fmt], m[:fmt])]
  else
    [image_path, (attr 'format', nil, false) || ((ext = ::File.extname image_path).downcase.slice 1, ext.length)]
  end
end