Skip to content

Commit

Permalink
don't apply passthrough substitution inside stem macros
Browse files Browse the repository at this point in the history
I just moved processing of the stem substitutions before processing of
the passthrough substitutions. It seems that it works well and the side
effects of this change are acceptable, i.e. they will probably affect
much less users than the current undesirable behaviour of + signs inside
the stem macros.

Fixes asciidoctor#3409
  • Loading branch information
jirutka committed Mar 6, 2020
1 parent 88bc891 commit b43b337
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
33 changes: 16 additions & 17 deletions lib/asciidoctor/substitutors.rb
Expand Up @@ -1053,6 +1053,22 @@ def extract_passthroughs text
%(#{preceding || ''}#{PASS_START}#{passthru_key}#{PASS_END})
end if (text.include? '++') || (text.include? '$$') || (text.include? 'ss:')

text = text.gsub InlineStemMacroRx do
# honor the escape
next $&.slice 1, $&.length if $&.start_with? RS

if (type = $1.to_sym) == :stem
type = STEM_TYPE_ALIASES[@document.attributes['stem']].to_sym
end
subs = $2
content = normalize_text $3, nil, true
# NOTE drop enclosing $ signs around latexmath for backwards compatibility with AsciiDoc Python
content = content.slice 1, content.length - 2 if type == :latexmath && (content.start_with? '$') && (content.end_with? '$')
subs = subs ? (resolve_pass_subs subs) : ((@document.basebackend? 'html') ? BASIC_SUBS : nil)
passthrus[passthru_key = passthrus.size] = { text: content, subs: subs, type: type }
%(#{PASS_START}#{passthru_key}#{PASS_END})
end if (text.include? ':') && ((text.include? 'stem:') || (text.include? 'math:'))

pass_inline_char1, pass_inline_char2, pass_inline_rx = InlinePassRx[compat_mode]
text = text.gsub pass_inline_rx do
preceding = $1
Expand Down Expand Up @@ -1102,23 +1118,6 @@ def extract_passthroughs text
%(#{preceding}#{PASS_START}#{passthru_key}#{PASS_END})
end if (text.include? pass_inline_char1) || (pass_inline_char2 && (text.include? pass_inline_char2))

# NOTE we need to do the stem in a subsequent step to allow it to be escaped by the former
text = text.gsub InlineStemMacroRx do
# honor the escape
next $&.slice 1, $&.length if $&.start_with? RS

if (type = $1.to_sym) == :stem
type = STEM_TYPE_ALIASES[@document.attributes['stem']].to_sym
end
subs = $2
content = normalize_text $3, nil, true
# NOTE drop enclosing $ signs around latexmath for backwards compatibility with AsciiDoc Python
content = content.slice 1, content.length - 2 if type == :latexmath && (content.start_with? '$') && (content.end_with? '$')
subs = subs ? (resolve_pass_subs subs) : ((@document.basebackend? 'html') ? BASIC_SUBS : nil)
passthrus[passthru_key = passthrus.size] = { text: content, subs: subs, type: type }
%(#{PASS_START}#{passthru_key}#{PASS_END})
end if (text.include? ':') && ((text.include? 'stem:') || (text.include? 'math:'))

text
end

Expand Down
10 changes: 1 addition & 9 deletions test/substitutions_test.rb
Expand Up @@ -1955,15 +1955,7 @@
end

test 'should passthrough math macro inside another passthrough' do
input = 'the text `asciimath:[x = y]` should be passed through as +literal+ text'
para = block_from_string input, attributes: { 'compat-mode' => '' }
assert_equal 'the text <code>asciimath:[x = y]</code> should be passed through as <code>literal</code> text', para.content

input = 'the text [x-]`asciimath:[x = y]` should be passed through as `literal` text'
para = block_from_string input
assert_equal 'the text <code>asciimath:[x = y]</code> should be passed through as <code>literal</code> text', para.content

input = 'the text `+asciimath:[x = y]+` should be passed through as `literal` text'
input = 'the text `++asciimath:[x = y]++` should be passed through as `literal` text'
para = block_from_string input
assert_equal 'the text <code>asciimath:[x = y]</code> should be passed through as <code>literal</code> text', para.content
end
Expand Down

0 comments on commit b43b337

Please sign in to comment.