Skip to content

Commit

Permalink
resolves #3938 allow subs attribute value on Inline node returned by …
Browse files Browse the repository at this point in the history
…processor for custom inline macro to be a string
  • Loading branch information
mojavelinux committed Apr 19, 2021
1 parent d2bceef commit 3e8aacf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Expand Up @@ -28,6 +28,7 @@ Improvements::

* Increment counter (but not the corresponding attribute) if attribute is locked (#4013)
* Use attribute as seed value for counter even if not already registered as a counter (#4014)
* Allow subs attribute value on Inline node returned by processor for custom inline macro to be a string (#3938)

Build / Infrastructure::

Expand Down
12 changes: 7 additions & 5 deletions lib/asciidoctor/substitutors.rb
Expand Up @@ -331,7 +331,7 @@ def sub_macros text
target ||= ext_config[:format] == :short ? content : target
end
if (Inline === (replacement = extension.process_method[self, target, attributes]))
if (inline_subs = replacement.attributes.delete 'subs') && (inline_subs = expand_subs inline_subs)
if (inline_subs = replacement.attributes.delete 'subs') && (inline_subs = expand_subs inline_subs, 'custom inline macro')
replacement.text = apply_subs replacement.text, inline_subs
end
replacement.convert
Expand Down Expand Up @@ -1234,13 +1234,14 @@ def resolve_pass_subs subs

# Public: Expand all groups in the subs list and return. If no subs are resolved, return nil.
#
# subs - The substitutions to expand; can be a Symbol or Symbol Array
# subs - The substitutions to expand; can be a Symbol, Symbol Array or String
# subject - The String to use in log messages to communicate the subject for which subs are being resolved (default: nil)
#
# Returns a Symbol Array of substitutions to pass to apply_subs or nil if no substitutions were resolved.
def expand_subs subs
def expand_subs subs, subject = nil
if ::Symbol === subs
subs == :none ? nil : SUB_GROUPS[subs] || [subs]
else
elsif ::Array === subs
expanded_subs = []
subs.each do |key|
unless key == :none
Expand All @@ -1251,8 +1252,9 @@ def expand_subs subs
end
end
end

expanded_subs.empty? ? nil : expanded_subs
else
resolve_subs subs, :inline, nil, subject
end
end

Expand Down
38 changes: 37 additions & 1 deletion test/extensions_test.rb
Expand Up @@ -1412,7 +1412,7 @@ def process parent, reader, attributes
end
end

test 'should apply specified subs to inline node returned by process method' do
test 'should apply subs specified as symbol to inline node returned by process method' do
begin
Asciidoctor::Extensions.register do
inline_macro do
Expand All @@ -1430,6 +1430,42 @@ def process parent, reader, attributes
end
end

test 'should apply subs specified as array to inline node returned by process method' do
begin
Asciidoctor::Extensions.register do
inline_macro do
named :say
process do |parent, target, attrs|
create_inline_pass parent, %(*#{target}*), attributes: { 'subs' => [:specialchars, :quotes] }
end
end
end

output = convert_string_to_embedded 'say:{lt}message{gt}[]', doctype: :inline
assert_equal '<strong>&lt;message&gt;</strong>', output
ensure
Asciidoctor::Extensions.unregister_all
end
end

test 'should apply subs specified as string to inline node returned by process method' do
begin
Asciidoctor::Extensions.register do
inline_macro do
named :say
process do |parent, target, attrs|
create_inline_pass parent, %(*#{target}*), attributes: { 'subs' => 'specialchars,quotes' }
end
end
end

output = convert_string_to_embedded 'say:{lt}message{gt}[]', doctype: :inline
assert_equal '<strong>&lt;message&gt;</strong>', output
ensure
Asciidoctor::Extensions.unregister_all
end
end

test 'should prefer attributes parsed from inline macro over default attributes' do
begin
Asciidoctor::Extensions.register do
Expand Down

0 comments on commit 3e8aacf

Please sign in to comment.