summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jirutka <jakub@jirutka.cz>2020-04-15 23:48:35 +0200
committerJakub Jirutka <jakub@jirutka.cz>2020-04-16 00:35:41 +0200
commit327035bb9a3393f355d22ad0a21c1a8cf22c6b3a (patch)
treec902ea36d9c8837e98931317a826f56a276505ff
parent45c3b1eb9e028198bc91f8d02a8a07c0a676d69b (diff)
Refactor and optimize getting Resolver from nested document
-rw-r--r--lib/asciidoctor/interdoc_reftext/inline_node_mixin.rb21
1 files changed, 14 insertions, 7 deletions
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