Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolves #3966 more accurately account for preprocessor behavior when tracking lineno in reader #4010

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Expand Up @@ -23,6 +23,7 @@ Bug Fixes::
* Ensure mtime of input file honors TZ environment variable on JRuby for Windows (affects value of docdatetime attribute) (#3550)
* Honor caption attribute on blocks that support captioned title even if corresponding *-caption document attribute is not set (#4023)
* Suppress missing attribute warning when applying substitutions to implicit document title to assign to doctitle attribute (#4024)
* More accurately 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 @@ -520,8 +520,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 @@ -535,8 +534,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