Skip to content

Commit

Permalink
backport fix for #4561 remove use of base64 library to prevent warnin…
Browse files Browse the repository at this point in the history
…g in Ruby >= 3.3
  • Loading branch information
mojavelinux committed Mar 7, 2024
1 parent ac595c0 commit 4fcef38
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.adoc
Expand Up @@ -18,7 +18,9 @@ This project utilizes semantic versioning.
// tag::compact[]
== Unreleased

_No changes since previous release._
Compliance::

* Remove use of base64 library to prevent warning in Ruby >= 3.3 (#4561)

== 2.0.21 (2024-02-20) - @mojavelinux

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -22,6 +22,7 @@ group :development do
elsif (Gem::Version.new RUBY_VERSION) < (Gem::Version.new '2.6.0')
gem 'nokogiri', '~> 1.12.0'
end
gem 'minitest', '~> 5.14.0' if (Gem::Version.new RUBY_VERSION) < (Gem::Version.new '2.6.0')
end

group :docs do
Expand Down
2 changes: 1 addition & 1 deletion asciidoctor.gemspec
Expand Up @@ -39,7 +39,7 @@ Gem::Specification.new do |s|
# erubi is needed for testing alternate eRuby impls
s.add_development_dependency 'erubi', '~> 1.10.0'
s.add_development_dependency 'haml', '~> 6.1.0'
s.add_development_dependency 'minitest', '~> 5.14.0'
s.add_development_dependency 'minitest', '~> 5.22.0'
s.add_development_dependency 'nokogiri', '~> 1.13.0'
s.add_development_dependency 'rake', '~> 12.3.0'
s.add_development_dependency 'slim', '~> 4.1.0'
Expand Down
1 change: 0 additions & 1 deletion lib/asciidoctor.rb
Expand Up @@ -6,7 +6,6 @@
# this require is satisfied by the Asciidoctor.js build; it augments the Ruby environment for Asciidoctor.js
require 'asciidoctor/js'
else
autoload :Base64, 'base64'
require 'cgi/util'
autoload :OpenURI, 'open-uri'
autoload :Pathname, 'pathname'
Expand Down
12 changes: 6 additions & 6 deletions lib/asciidoctor/abstract_node.rb
Expand Up @@ -354,8 +354,8 @@ def media_uri(target, asset_dir_key = 'imagesdir')
#
# First, and foremost, the target image path is cleaned if the document safe mode level
# is set to at least SafeMode::SAFE (a condition which is true by default) to prevent access
# to ancestor paths in the filesystem. The image data is then read and converted to
# Base64. Finally, a data URI is built which can be used in an image tag.
# to ancestor paths in the filesystem. The image data is then read and converted to base64.
# Finally, a data URI is built which can be used in an image tag.
#
# target_image - A String path to the target image
# asset_dir_key - The String attribute key used to lookup the directory where
Expand All @@ -376,8 +376,8 @@ def generate_data_uri(target_image, asset_dir_key = nil)
end

if ::File.readable? image_path
# NOTE base64 is autoloaded by reference to ::Base64
%(data:#{mimetype};base64,#{::Base64.strict_encode64 ::File.binread image_path})
# NOTE pack 'm0' is equivalent to Base64.strict_encode64
%(data:#{mimetype};base64,#{[(::File.binread image_path)].pack 'm0'})
else
logger.warn %(image to embed not found or not readable: #{image_path})
%(data:#{mimetype};base64,)
Expand Down Expand Up @@ -410,8 +410,8 @@ def generate_data_uri_from_uri image_uri, cache_uri = false

begin
mimetype, bindata = ::OpenURI.open_uri(image_uri, URI_READ_MODE) {|f| [f.content_type, f.read] }
# NOTE base64 is autoloaded by reference to ::Base64
%(data:#{mimetype};base64,#{::Base64.strict_encode64 bindata})
# NOTE pack 'm0' is equivalent to Base64.strict_encode64
%(data:#{mimetype};base64,#{[bindata].pack 'm0'})
rescue
logger.warn %(could not retrieve image data from URI: #{image_uri})
image_uri
Expand Down

0 comments on commit 4fcef38

Please sign in to comment.