Skip to content

Commit

Permalink
eradicate the use of the base64 library
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed Mar 8, 2024
1 parent 4d5cfb8 commit a18bcc2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Bug Fixes::
* remove deprecated, undocumented `svg-font-family` theme key (the correct key is `svg-fallback-font-family`)
* major improvement to OTF support following release of ttfunk 1.8.0 (automatic transitive dependency of prawn)
* propagate source location to table cell for description in horizontal dlist so it is included in truncation warning message (#2502)
* eradicate use of the base64 library to fix waarning about base64 gem when using Ruby >= 3.3
* upgrade prawn-svg to 0.34 to fix warning about base64 gem when using Ruby >= 3.3; apply additional patch to fix bug in prawn-svg

== 2.3.13 (2024-02-16) - @mojavelinux
Expand Down
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ gemspec
gem 'asciidoctor', ENV['ASCIIDOCTOR_VERSION'], require: false if ENV.key? 'ASCIIDOCTOR_VERSION'
gem 'asciidoctor-diagram', ENV['ASCIIDOCTOR_DIAGRAM_VERSION'], require: false if ENV.key? 'ASCIIDOCTOR_DIAGRAM_VERSION'
gem 'asciidoctor-kroki', ENV['ASCIIDOCTOR_KROKI_VERSION'], require: false if ENV.key? 'ASCIIDOCTOR_KROKI_VERSION'
gem 'base64', require: false if (Gem::Version.new RUBY_VERSION) >= (Gem::Version.new '3.3.0') # required until asciidoctor 2.0.22 is out
gem 'coderay', '~> 1.1.0', require: false
gem 'ffi-icu', ENV['FFI_ICU_VERSION'], require: false if ENV.key? 'FFI_ICU_VERSION'
gem 'open-uri-cached', '~> 1.0.0', require: false
Expand Down
14 changes: 7 additions & 7 deletions lib/asciidoctor/pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1738,7 +1738,7 @@ def convert_image node, opts = {}
if image_format == 'gif' && !(defined? ::GMagick::Image)
log :warn, %(GIF image format not supported. Install the prawn-gmagick gem or convert #{target} to PNG.)
image_path = nil
elsif ::Base64 === target
elsif ::Asciidoctor::Image::Base64Encoded === target
image_path = target
elsif (image_path = resolve_image_path node, target, image_format, (opts.fetch :relative_to_imagesdir, true))
if image_format == 'pdf'
Expand Down Expand Up @@ -1807,8 +1807,8 @@ def convert_image node, opts = {}
rendered_h = rendered_w = nil
span_page_width_if align_to_page do
if image_format == 'svg'
if ::Base64 === image_path
svg_data = ::Base64.decode64 image_path
if ::Asciidoctor::Image::Base64Encoded === image_path
svg_data = image_path.unpack1 'm'
file_request_root = false
else
svg_data = ::File.read image_path, mode: 'r:UTF-8'
Expand Down Expand Up @@ -1858,8 +1858,8 @@ def convert_image node, opts = {}
else
# FIXME: this code really needs to be better organized!
# NOTE: use low-level API to access intrinsic dimensions; build_image_object caches image data previously loaded
image_obj, image_info = ::Base64 === image_path ?
::StringIO.open((::Base64.decode64 image_path), 'rb') {|fd| build_image_object fd } :
image_obj, image_info = ::Asciidoctor::Image::Base64Encoded === image_path ?
::StringIO.open((image_path.unpack1 'm'), 'rb') {|fd| build_image_object fd } :
::File.open(image_path, 'rb') {|fd| build_image_object fd }
actual_w = to_pt image_info.width, :px
width = actual_w * scale if scale
Expand Down Expand Up @@ -4344,11 +4344,11 @@ def resolve_image_path node, image_path, image_format, relative_to = true
imagesdir = relative_to
end
# NOTE: base64 logic currently used for inline images
if ::Base64 === image_path
if ::Asciidoctor::Image::Base64Encoded === image_path
return @tmp_files[image_path] if @tmp_files.key? image_path
tmp_image = ::Tempfile.create %W(image- .#{image_format})
tmp_image.binmode unless image_format == 'svg'
tmp_image.write ::Base64.decode64 image_path
tmp_image.write image_path.unpack1 'm'
tmp_image.close
@tmp_files[image_path] = tmp_image.path
# NOTE: this will catch a classloader resource path on JRuby (e.g., uri:classloader:/path/to/image)
Expand Down
5 changes: 3 additions & 2 deletions lib/asciidoctor/pdf/ext/asciidoctor/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Asciidoctor
module Image
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' }

Expand All @@ -11,7 +12,7 @@ def self.format image_path

def self.target_and_format image_path, attributes = nil
if (image_path.start_with? 'data:') && (m = DataUriRx.match image_path)
[(m[:data].extend ::Base64), (FormatAliases.fetch m[:fmt], m[:fmt])]
[(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
Expand All @@ -20,7 +21,7 @@ def self.target_and_format image_path, attributes = nil
def target_and_format
image_path = inline? ? target : (attr 'target')
if (image_path.start_with? 'data:') && (m = DataUriRx.match image_path)
[(m[:data].extend ::Base64), (FormatAliases.fetch m[:fmt], m[:fmt])]
[(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
Expand Down

0 comments on commit a18bcc2

Please sign in to comment.