Skip to content

Commit

Permalink
resolves #3178 apply subs to Inline node returned by inline macro pro…
Browse files Browse the repository at this point in the history
…cessor if specified (PR #3179)
  • Loading branch information
mojavelinux committed Mar 21, 2019
1 parent 3b4ee19 commit fd3f9ce
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Enhancements / Compliance::
* allow negated subs to be specified on inline pass macro (#2191)
* log warning if footnoteref macro is found and compat mode is not enabled (#3114)
* log info message if inline macro processor returns a String value (#3176)
* apply subs to Inline node returned by inline macro processor if subs attribute is specified (#3178)
* add create_inline_pass helper method to base extension processor class (#3178)
* log debug message instead of warning if block style is unknown (#3092)
* allow backend to delegate to a registered backend using the syntax synthetic:delegate when using custom templates (e.g., slides:html) (#891)
* AbstractBlock#find_by looks inside AsciiDoc table cells if traverse_documents selector option is true (#3101)
Expand Down
1 change: 1 addition & 0 deletions lib/asciidoctor/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ def parse_content parent, content, attributes = nil
[:create_listing_block, :create_block, :listing],
[:create_literal_block, :create_block, :literal],
[:create_anchor, :create_inline, :anchor],
[:create_inline_pass, :create_inline, :quoted],
].each do |method_name, delegate_method_name, context|
define_method method_name do |*args|
args.unshift args.shift, context
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor/inline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Asciidoctor
# Public: Methods for managing inline elements in AsciiDoc block
class Inline < AbstractNode
# Public: Get the text of this inline element
attr_reader :text
attr_accessor :text

# Public: Get the type (qualifier) of this inline element
attr_reader :type
Expand Down
3 changes: 3 additions & 0 deletions lib/asciidoctor/substitutors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,9 @@ def sub_macros(text)
# NOTE for convenience, map content (unparsed attrlist) to target when format is short
target ||= extconf[:format] == :short ? content : target
if (Inline === (replacement = extension.process_method[self, target, attributes]))
if (inline_subs = replacement.attributes.delete 'subs')
replacement.text = apply_subs replacement.text, (expand_subs inline_subs)
end
replacement.convert
elsif replacement
logger.info %(expected substitution value for custom inline macro to be of type Inline; got #{replacement.class}: #{$&})
Expand Down
54 changes: 45 additions & 9 deletions test/extensions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ def process parent, target, attributes
c = target.to_f
case units
when 'C'
create_inline parent, :quoted, %(#{c.round precision} &#176;C)
create_inline parent, :quoted, %(#{c.round precision} &#176;C), type: :unquoted
when 'F'
create_inline parent, :quoted, %(#{(c * 1.8 + 32).round precision} &#176;F)
create_inline parent, :quoted, %(#{(c * 1.8 + 32).round precision} &#176;F), type: :unquoted
else
raise ::ArgumentError, %(Unknown temperature units: #{units})
end
Expand Down Expand Up @@ -1136,7 +1136,7 @@ def process parent, reader, attributes
match_format :short
parse_content_as :text
process do |parent, _, attrs|
create_inline parent, :quoted, %(<label>#{attrs['text']}</label>)
create_inline_pass parent, %(<label>#{attrs['text']}</label>)
end
end
end
Expand All @@ -1155,7 +1155,7 @@ def process parent, reader, attributes
named :label
match_format :short
process do |parent, target|
create_inline parent, :quoted, %(<label>#{target}</label>)
create_inline_pass parent, %(<label>#{target}</label>)
end
end
end
Expand All @@ -1175,7 +1175,7 @@ def process parent, reader, attributes
match_format :short
resolve_attributes '1:name'
process do |parent, target, attrs|
create_inline parent, :quoted, %(target=#{target.inspect}, attributes=#{attrs.sort_by {|(k)| k.to_s }.to_h})
create_inline_pass parent, %(target=#{target.inspect}, attributes=#{attrs.sort_by {|(k)| k.to_s }.to_h})
end
end

Expand All @@ -1184,23 +1184,23 @@ def process parent, reader, attributes
match_format :short
resolve_attributes false
process do |parent, target, attrs|
create_inline parent, :quoted, %(target=#{target.inspect}, attributes=#{attrs.sort_by {|(k)| k.to_s }.to_h})
create_inline_pass parent, %(target=#{target.inspect}, attributes=#{attrs.sort_by {|(k)| k.to_s }.to_h})
end
end

inline_macro do
named :'full-attributes'
resolve_attributes '1:name' => nil
process do |parent, target, attrs|
create_inline parent, :quoted, %(target=#{target.inspect}, attributes=#{attrs.sort_by {|(k)| k.to_s }.to_h})
create_inline_pass parent, %(target=#{target.inspect}, attributes=#{attrs.sort_by {|(k)| k.to_s }.to_h})
end
end

inline_macro do
named :'full-text'
resolve_attributes false
process do |parent, target, attrs|
create_inline parent, :quoted, %(target=#{target.inspect}, attributes=#{attrs.sort_by {|(k)| k.to_s }.to_h})
create_inline_pass parent, %(target=#{target.inspect}, attributes=#{attrs.sort_by {|(k)| k.to_s }.to_h})
end
end

Expand All @@ -1209,7 +1209,7 @@ def process parent, reader, attributes
match %r/@(\w+)/
resolve_attributes false
process do |parent, target, attrs|
create_inline parent, :quoted, %(target=#{target.inspect}, attributes=#{attrs.sort_by {|(k)| k.to_s }.to_h})
create_inline_pass parent, %(target=#{target.inspect}, attributes=#{attrs.sort_by {|(k)| k.to_s }.to_h})
end
end
end
Expand Down Expand Up @@ -1311,6 +1311,42 @@ def process parent, reader, attributes
end
end

test 'should not apply subs to inline node returned by process method by default' do
begin
Asciidoctor::Extensions.register do
inline_macro do
named :say
process do |parent, target, attrs|
create_inline parent, :quoted, %(*#{target}*), type: :emphasis
end
end
end

output = convert_string_to_embedded 'say:yo[]', doctype: :inline
assert_equal '<em>*yo*</em>', output
ensure
Asciidoctor::Extensions.unregister_all
end
end

test 'should apply specified subs 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' => :normal }
end
end
end

output = convert_string_to_embedded 'say:yo[]', doctype: :inline
assert_equal '<strong>yo</strong>', output
ensure
Asciidoctor::Extensions.unregister_all
end
end

test 'should not carry over attributes if block processor returns nil' do
begin
Asciidoctor::Extensions.register do
Expand Down

0 comments on commit fd3f9ce

Please sign in to comment.