From 6c1cc63417e4cdee865a52baf999b482bf9bff5f Mon Sep 17 00:00:00 2001 From: Guillaume Grossetie Date: Wed, 19 Jan 2022 15:41:52 +0100 Subject: [PATCH] resolves #4244 extract the require logic of the OpenURI library --- lib/asciidoctor/abstract_node.rb | 10 +--------- lib/asciidoctor/helpers.rb | 16 ++++++++++++++++ lib/asciidoctor/reader.rb | 9 +-------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/asciidoctor/abstract_node.rb b/lib/asciidoctor/abstract_node.rb index a60ab7956c..29ef14da54 100644 --- a/lib/asciidoctor/abstract_node.rb +++ b/lib/asciidoctor/abstract_node.rb @@ -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 diff --git a/lib/asciidoctor/helpers.rb b/lib/asciidoctor/helpers.rb index 33b1a24732..8333e1c242 100644 --- a/lib/asciidoctor/helpers.rb +++ b/lib/asciidoctor/helpers.rb @@ -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 diff --git a/lib/asciidoctor/reader.rb b/lib/asciidoctor/reader.rb index f953011250..d563d9abdd 100644 --- a/lib/asciidoctor/reader.rb +++ b/lib/asciidoctor/reader.rb @@ -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