From 327035bb9a3393f355d22ad0a21c1a8cf22c6b3a Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Wed, 15 Apr 2020 23:48:35 +0200 Subject: Refactor and optimize getting Resolver from nested document --- .../interdoc_reftext/inline_node_mixin.rb | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/asciidoctor/interdoc_reftext/inline_node_mixin.rb b/lib/asciidoctor/interdoc_reftext/inline_node_mixin.rb index 248b697..cb43fba 100644 --- a/lib/asciidoctor/interdoc_reftext/inline_node_mixin.rb +++ b/lib/asciidoctor/interdoc_reftext/inline_node_mixin.rb @@ -39,15 +39,22 @@ module Asciidoctor::InterdocReftext # If this node is not an inter-document cross reference... return if @node_name != 'inline_anchor' || @attributes['path'].nil? - # Only the top most document has the `@_interdoc_reftext_resolver` variable. - doc = @document - while doc.parent_document do - doc = doc.parent_document + # interdoc_reftext_resolver may return nil when the extension was loaded, + # but is disabled for the current document. + if (resolver = interdoc_reftext_resolver) + @text = resolver.call(@attributes['refid']) end + end - # This variable is injected into the document by {Processor}. - if (resolver = doc.instance_variable_get(Processor::RESOLVER_VAR_NAME)) - @text = resolver.call(@attributes['refid']) + # @return [Asciidoctor::InterdocReftext::Resolver, nil] + def interdoc_reftext_resolver + # This variable is injected into the document by {Processor} or this method. + @document.instance_variable_get(Processor::RESOLVER_VAR_NAME) || begin + doc = @document + until (resolver = doc.instance_variable_get(Processor::RESOLVER_VAR_NAME)) + doc = doc.parent_document or return nil + end + doc.instance_variable_set(Processor::RESOLVER_VAR_NAME, resolver) end end end -- cgit v1.2.3