Skip to content

Commit

Permalink
resolves #3966 account for preprocessor behavior when tracking lineno…
Browse files Browse the repository at this point in the history
… in reader
  • Loading branch information
mojavelinux committed Apr 11, 2021
1 parent 9bf9f8c commit a1df0b6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Expand Up @@ -28,6 +28,7 @@ Bug Fixes::
* Fix formatting of footnote text with URL in man page output (#3988)
* Remove redundant trailing space on URL followed by non-adjacent text in man page output (#4004)
* Use .bp macro at location of page break in man page output (#3992)
* Better account for preprocessor behavior when tracking lineno in reader (#3966)

Improvements::

Expand Down
18 changes: 13 additions & 5 deletions lib/asciidoctor/reader.rb
Expand Up @@ -59,6 +59,7 @@ def initialize data = nil, cursor = nil, opts = {}
end
@lineno = cursor.lineno || 1
end
@next_lineno_inc = 1
@lines = prepare_lines data, opts
@source_lines = @lines.drop 0
@mark = nil
Expand Down Expand Up @@ -154,8 +155,9 @@ def peek_line direct = false
# if there are no more lines in this Reader.
def peek_lines num = nil, direct = false
old_look_ahead = @look_ahead
start_lineno = @lineno
result = []
(num || MAX_INT).times do
(num ||= MAX_INT).times do
if (line = direct ? shift : read_line)
result << line
else
Expand All @@ -166,7 +168,12 @@ def peek_lines num = nil, direct = false

unless result.empty?
unshift_all result
@look_ahead = old_look_ahead if direct
if direct
@look_ahead = old_look_ahead
elsif @lineno > start_lineno
@next_lineno_inc += @lineno - start_lineno
@lineno = start_lineno
end
end

result
Expand Down Expand Up @@ -457,7 +464,8 @@ def read_lines_until options = {}
#
# Returns The String line at the top of the stack
def shift
@lineno += 1
@lineno += @next_lineno_inc
@next_lineno_inc = 1
@look_ahead -= 1 unless @look_ahead == 0
@lines.shift
end
Expand Down Expand Up @@ -675,7 +683,7 @@ def peek_line direct = false
#
# Returns this Reader object.
def push_include data, file = nil, path = nil, lineno = 1, attributes = {}
@include_stack << [@lines, @file, @dir, @path, @lineno, @maxdepth, @process_lines]
@include_stack << [@lines, @file, @dir, @path, @lineno, @next_lineno_inc, @maxdepth, @process_lines]
if (@file = file)
# NOTE if file is not a string, assume it's a URI
if ::String === file
Expand Down Expand Up @@ -1263,7 +1271,7 @@ def resolve_include_path target, attrlist, attributes

def pop_include
if @include_stack.size > 0
@lines, @file, @dir, @path, @lineno, @maxdepth, @process_lines = @include_stack.pop
@lines, @file, @dir, @path, @lineno, @next_lineno_inc, @maxdepth, @process_lines = @include_stack.pop
# FIXME kind of a hack
#Document::AttributeEntry.new('infile', @file).save_to_next_block @document
#Document::AttributeEntry.new('indir', ::File.dirname(@file)).save_to_next_block @document
Expand Down
6 changes: 2 additions & 4 deletions test/api_test.rb
Expand Up @@ -519,8 +519,7 @@ def [](key)
assert_equal 96, unordered_complex_items[4].lineno
end

# FIXME see #3966
test 'should assign incorrect lineno for single-line paragraph inside a conditional preprocessor directive' do
test 'should assign correct lineno for single-line paragraph inside a conditional preprocessor directive' do
input = <<~'EOS'
:conditional-attribute:
Expand All @@ -534,8 +533,7 @@ def [](key)
EOS

doc = document_from_string input, sourcemap: true
# FIXME the second line number should be 6 instead of 7
assert_equal [3, 7, 9], (doc.find_by context: :paragraph).map(&:lineno)
assert_equal [3, 6, 9], (doc.find_by context: :paragraph).map(&:lineno)
end

test 'should assign correct lineno for multi-line paragraph inside a conditional preprocessor directive' do
Expand Down

0 comments on commit a1df0b6

Please sign in to comment.