summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2022-07-05 01:25:08 -0600
committerDan Allen <dan.j.allen@gmail.com>2022-07-05 01:25:42 -0600
commit3c575b9de21790d3b812c74948c42eb6470ec02b (patch)
treef3c886621895b9121534f8f30d9b62ff5a32d403
parentac0c752d8e67aab578f8f31512784f168b479998 (diff)
fix lineno on reader when skip-front-matter attribute is set but end of front matter is not found
-rw-r--r--CHANGELOG.adoc1
-rw-r--r--lib/asciidoctor/reader.rb33
-rw-r--r--test/reader_test.rb20
3 files changed, 35 insertions, 19 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index fa846fbf..707a6fb1 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -59,6 +59,7 @@ Bug Fixes::
* If path is included both partially and fully, store it with true value (included fully) in includes table of document catalog
* Reset registry if activate is called on it again (#4256)
* Format source location in exception message when extension code is malformed
+ * Fix lineno on reader when `skip-front-matter` attribute is set but end of front matter is not found
// tag::compact[]
== 2.0.17 (2022-01-05) - @mojavelinux
diff --git a/lib/asciidoctor/reader.rb b/lib/asciidoctor/reader.rb
index 257557e3..4f419d75 100644
--- a/lib/asciidoctor/reader.rb
+++ b/lib/asciidoctor/reader.rb
@@ -1267,27 +1267,22 @@ class PreprocessorReader < Reader
# Private: Ignore front-matter, commonly used in static site generators
def skip_front_matter! data, increment_linenos = true
- front_matter = nil
- if data[0] == '---'
- original_data = data.drop 0
- data.shift
- front_matter = []
+ return unless (delim = data[0]) == '---'
+ original_data = data.drop 0
+ data.shift
+ front_matter = []
+ @lineno += 1 if increment_linenos
+ until (eof = data.empty?) || data[0] == delim
+ front_matter << data.shift
@lineno += 1 if increment_linenos
- while !data.empty? && data[0] != '---'
- front_matter << data.shift
- @lineno += 1 if increment_linenos
- end
-
- if data.empty?
- data.unshift(*original_data)
- @lineno = 0 if increment_linenos
- front_matter = nil
- else
- data.shift
- @lineno += 1 if increment_linenos
- end
end
-
+ if eof
+ data.unshift(*original_data)
+ @lineno -= original_data.size if increment_linenos
+ return
+ end
+ data.shift
+ @lineno += 1 if increment_linenos
front_matter
end
diff --git a/test/reader_test.rb b/test/reader_test.rb
index 21269525..edd0aaa6 100644
--- a/test/reader_test.rb
+++ b/test/reader_test.rb
@@ -511,6 +511,25 @@ class ReaderTest < Minitest::Test
reader = doc.reader
refute doc.attributes.key?('front-matter')
assert_equal '---', reader.peek_line
+ assert_equal 1, reader.lineno
+ end
+
+ test 'should not skip front matter if ending delimiter is not found' do
+ input = <<~'EOS'
+ ---
+ title: Document Title
+ tags: [ first, second ]
+ = Document Title
+ Author Name
+
+ preamble
+ EOS
+
+ doc = Asciidoctor::Document.new input, attributes: { 'skip-front-matter' => '' }
+ reader = doc.reader
+ assert_equal '---', reader.peek_line
+ refute doc.attributes.key? 'front-matter'
+ assert_equal 1, reader.lineno
end
test 'should skip front matter if specified by skip-front-matter attribute' do
@@ -535,6 +554,7 @@ class ReaderTest < Minitest::Test
reader = doc.reader
assert_equal '= Document Title', reader.peek_line
assert_equal front_matter, doc.attributes['front-matter']
+ assert_equal 7, reader.lineno
end
end