Skip to content

Commit

Permalink
resolves asciidoctor#3114 log warning if footnoteref macro is found a…
Browse files Browse the repository at this point in the history
…nd compat mode is not enabled
  • Loading branch information
mojavelinux committed Mar 1, 2019
1 parent ed09399 commit 3d970ae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Expand Up @@ -60,6 +60,7 @@ Enhancements / Compliance::
* add Document#parsed? method to check whether document has been parsed
* modify Cell class to extend from AbstractBlock instead of AbstractNode (#2963)
* implement block? and inline? methods on Column, both which return false (#2963)
* log warning if footnoteref macro is found and compat mode is not enabled (#3114)

Improvements::

Expand Down
15 changes: 13 additions & 2 deletions lib/asciidoctor/substitutors.rb
Expand Up @@ -859,8 +859,19 @@ def sub_macros(text)
# honor the escape
next $&.slice 1, $&.length if $&.start_with? RS

# $1 is footnoteref (legacy)
id, text = $1 ? ($3 || '').split(',', 2) : [$2, $3]
# footnoteref
if $1
if $3
id, text = $3.split ',', 2
logger.warn %(found deprecated footnoteref macro: #{$&}; use footnote macro with target instead) unless doc.compat_mode
else
next $&
end
# footnote
else
id = $2
text = $3
end

if id
if text
Expand Down
4 changes: 2 additions & 2 deletions test/document_test.rb
Expand Up @@ -1104,8 +1104,8 @@
test 'outputs footnotes in footer' do
input = <<~'EOS'
A footnote footnote:[An example footnote.];
a second footnote with a reference ID footnoteref:[note2,Second footnote.];
finally a reference to the second footnote footnoteref:[note2].
a second footnote with a reference ID footnote:note2[Second footnote.];
and finally a reference to the second footnote footnote:note2[].
EOS

output = convert_string input
Expand Down
26 changes: 19 additions & 7 deletions test/substitutions_test.rb
Expand Up @@ -993,7 +993,7 @@
end

test 'a footnoteref macro with id and single-line text should be registered and output as a footnote' do
para = block_from_string('Sentence text footnoteref:[ex1, An example footnote.].')
para = block_from_string 'Sentence text footnoteref:[ex1, An example footnote.].', attributes: { 'compat-mode' => '' }
assert_equal %(Sentence text <sup class="footnote" id="_footnote_ex1">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>.), para.sub_macros(para.source)
assert_equal 1, para.document.catalog[:footnotes].size
footnote = para.document.catalog[:footnotes].first
Expand All @@ -1003,7 +1003,7 @@
end

test 'a footnoteref macro with id and multi-line text should be registered and output as a footnote without newlines' do
para = block_from_string("Sentence text footnoteref:[ex1, An example footnote\nwith wrapped text.].")
para = block_from_string "Sentence text footnoteref:[ex1, An example footnote\nwith wrapped text.].", attributes: { 'compat-mode' => '' }
assert_equal %(Sentence text <sup class="footnote" id="_footnote_ex1">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>.), para.sub_macros(para.source)
assert_equal 1, para.document.catalog[:footnotes].size
footnote = para.document.catalog[:footnotes].first
Expand All @@ -1013,7 +1013,7 @@
end

test 'a footnoteref macro with id should refer to footnoteref with same id' do
para = block_from_string('Sentence text footnoteref:[ex1, An example footnote.]. Sentence text footnoteref:[ex1].')
para = block_from_string 'Sentence text footnoteref:[ex1, An example footnote.]. Sentence text footnoteref:[ex1].', attributes: { 'compat-mode' => '' }
assert_equal %(Sentence text <sup class="footnote" id="_footnote_ex1">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>. Sentence text <sup class="footnoteref">[<a class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>.), para.sub_macros(para.source)
assert_equal 1, para.document.catalog[:footnotes].size
footnote = para.document.catalog[:footnotes].first
Expand All @@ -1031,6 +1031,15 @@
end
end

test 'using a footnoteref macro should generate a warning when compat mode is not enabled' do
input = 'Sentence text.footnoteref:[fn1,Commentary on this sentence.]'
using_memory_logger do |logger|
para = block_from_string input
para.sub_macros para.source
assert_message logger, :WARN, 'found deprecated footnoteref macro: footnoteref:[fn1,Commentary on this sentence.]; use footnote macro with target instead'
end
end

test 'inline footnote macro can be used to define and reference a footnote reference' do
input = <<~'EOS'
You can download the software from the product page.footnote:sub[Option only available if you have an active subscription.]
Expand All @@ -1040,10 +1049,13 @@
If all else fails, you can give us a call.footnoteref:[sub]
EOS

output = convert_string_to_embedded input
assert_css '#_footnotedef_1', output, 1
assert_css 'p a[href="#_footnotedef_1"]', output, 3
assert_css '#footnotes .footnote', output, 1
using_memory_logger do |logger|
output = convert_string_to_embedded input, attributes: { 'compat-mode' => '' }
assert_css '#_footnotedef_1', output, 1
assert_css 'p a[href="#_footnotedef_1"]', output, 3
assert_css '#footnotes .footnote', output, 1
assert logger.empty?
end
end

test 'should parse multiple footnote references in a single line' do
Expand Down

0 comments on commit 3d970ae

Please sign in to comment.