Skip to content

Commit

Permalink
resolves asciidoctor#4244 extract the require logic of the OpenURI li…
Browse files Browse the repository at this point in the history
…brary
  • Loading branch information
ggrossetie committed Jan 19, 2022
1 parent 75d88d3 commit 6c1cc63
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
10 changes: 1 addition & 9 deletions lib/asciidoctor/abstract_node.rb
Expand Up @@ -400,15 +400,7 @@ def generate_data_uri target_image, asset_dir_key = nil
# Returns A data URI string built from Base64 encoded data read from the URI
# and the mime type specified in the Content Type header.
def generate_data_uri_from_uri image_uri, cache_uri = false
if cache_uri
# caching requires the open-uri-cached gem to be installed
# processing will be automatically aborted if these libraries can't be opened
Helpers.require_library 'open-uri/cached', 'open-uri-cached'
elsif !RUBY_ENGINE_OPAL
# autoload open-uri
::OpenURI
end

Helpers.require_open_uri cache_uri
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
Expand Down
16 changes: 16 additions & 0 deletions lib/asciidoctor/helpers.rb
Expand Up @@ -300,5 +300,21 @@ def class_for_name qualified_name
rescue
raise ::NameError, %(Could not resolve class for name: #{qualified_name})
end

# Internal: Require the OpenURI library.
#
# Attempts to load `open-uri-cached` if the cache argument is true otherwise autoload `open-uri`.
#
# cache - A Boolean flag indicating whether to activate content cache URI
def require_open_uri cache = false
if cache
# caching requires the open-uri-cached gem to be installed
# processing will be automatically aborted if these libraries can't be opened
require_library 'open-uri/cached', 'open-uri-cached' unless defined? ::OpenURI::Cache
elsif !RUBY_ENGINE_OPAL
# autoload open-uri
::OpenURI
end
end
end
end
9 changes: 1 addition & 8 deletions lib/asciidoctor/reader.rb
Expand Up @@ -1221,14 +1221,7 @@ def resolve_include_path target, attrlist, attributes
doc = @document
if (Helpers.uriish? target) || (::String === @dir ? nil : (target = %(#{@dir}/#{target})))
return replace_next_line %(link:#{target}[#{attrlist}]) unless doc.attr? 'allow-uri-read'
if doc.attr? 'cache-uri'
# caching requires the open-uri-cached gem to be installed
# processing will be automatically aborted if these libraries can't be opened
Helpers.require_library 'open-uri/cached', 'open-uri-cached' unless defined? ::OpenURI::Cache
elsif !RUBY_ENGINE_OPAL
# autoload open-uri
::OpenURI
end
Helpers.require_open_uri doc.attr?('cache-uri')
[(::URI.parse target), :uri, target]
else
# include file is resolved relative to dir of current include, or base_dir if within original docfile
Expand Down

0 comments on commit 6c1cc63

Please sign in to comment.