Skip to content

Commit 7d030e9

Browse files
committed
A bit of refactoring and added a check to make sure that any line number token must between one and the length of the source block.
1 parent 3dd80e9 commit 7d030e9

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

lib/asciidoctor-external-callout.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ def process_callouts(list, owner_block)
146146

147147
if location.is_numeric?
148148

149-
numbers = location.to_i
149+
line_number = location.to_i
150150

151-
if numbers <= owner_block.lines.length
152-
line_numbers << (numbers - 1)
151+
if line_number.between?(1, owner_block.lines.length)
152+
line_numbers << (line_number - 1) # Because the line number is now an array index, so -1
153153
else
154-
warn "Out of range ==> #{numbers}"
154+
warn "Line number out of range ==> #{line_number}"
155155
end
156156

157157
else

test/asciidoctor-external-callout_test.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_basic_conversion
1818
assert document.instance_of? Document
1919
assert_equal 5, document.blocks.length
2020
assert_equal document.blocks[1].context, :paragraph
21-
assert_equal 4, document.blocks[4].items.length
21+
assert_equal 6, document.blocks[4].items.length
2222
assert_equal 'The `use_dsl` line', document.blocks[4].items[0].instance_variable_get(:@text)
2323
assert_equal true, document.blocks[4].has_role?('external-callout-list')
2424
end
@@ -73,5 +73,22 @@ def test_case_insensitive_search
7373

7474
end
7575

76+
def test_for_line_zero
77+
78+
document = Asciidoctor.convert_file File.join(File.dirname(__FILE__), 'sample_global_search.adoc'),
79+
safe: :unsafe, backend: :html5,
80+
attributes: {'stylesheet' => './callout.css'}
81+
82+
assert document.blocks[0].context == :listing
83+
assert document.blocks[0].lines.length == 30
84+
85+
# No line should contain a callout for 5, because the
86+
# token is set for line 0
87+
# No line should have a callout for 6, because the
88+
# token is set for line 500.
89+
90+
assert_equal false, document.blocks[0].lines.any? {|line| line.include? '<5>' or line.include? '<6>'}
91+
92+
end
7693
end
7794

test/sample.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,6 @@ How about a table?
5555
. The process def at @5
5656
. Document object used at @5 @7
5757
. Result first used @/result/
58+
. There is no line zero @0
59+
. There is no line five hundred @500
5860

0 commit comments

Comments
 (0)