diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2013-08-21 18:41:50 -0600 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2013-08-21 18:41:50 -0600 |
| commit | 52fed16eb7dc3d7166fffa6a3ab0c732843cc27f (patch) | |
| tree | ea5bc961aa7b3c9d57d9527f6d7f4e6312f44d5b | |
| parent | 0cbab4713b4a8588068621d2f216d57ea3890530 (diff) | |
fix warnings, minor optimizations in Reader
| -rw-r--r-- | lib/asciidoctor/reader.rb | 28 | ||||
| -rw-r--r-- | test/reader_test.rb | 4 |
2 files changed, 22 insertions, 10 deletions
diff --git a/lib/asciidoctor/reader.rb b/lib/asciidoctor/reader.rb index 97c1f13a..b748e2a4 100644 --- a/lib/asciidoctor/reader.rb +++ b/lib/asciidoctor/reader.rb @@ -29,7 +29,7 @@ class Reader # Public: Initialize the Reader object def initialize data = nil, cursor = nil if cursor.nil? - @file = @dir + @file = @dir = nil @path = '<stdin>' @lineno = 1 # IMPORTANT lineno assignment must proceed prepare_lines call! elsif cursor.is_a? String @@ -128,7 +128,7 @@ class Reader # direct - A Boolean flag to bypasses the check for more lines and immediately # returns the first element of the internal @lines Array. (default: false) # - # Returns a the next line of the source data if data is present. + # Returns the next line of the source data as a String if there are lines remaining. # Returns nil if there is no more data. def peek_line direct = false if direct || @look_ahead > 0 @@ -568,8 +568,8 @@ class PreprocessorReader < Reader return '' end - found_colon = line.include?('::') - if found_colon && line.include?('if') && (match = line.match(REGEXP[:ifdef_macro])) + macroish = line.include?('::') && line.include?('[') + if macroish && line.include?('if') && (match = line.match(REGEXP[:ifdef_macro])) # if escaped, mark as processed and return line unescaped if line.start_with? '\\' @unescape_next_line = true @@ -591,7 +591,7 @@ class PreprocessorReader < Reader elsif @skipping advance nil - elsif found_colon && line.include?('include::') && (match = line.match(REGEXP[:include_macro])) + elsif macroish && line.include?('include::') && (match = line.match(REGEXP[:include_macro])) # if escaped, mark as processed and return line unescaped if line.start_with? '\\' @unescape_next_line = true @@ -610,18 +610,28 @@ class PreprocessorReader < Reader end end else - super + # optimization to inline super + #super + @look_ahead += 1 + line end end + # Public: Override the Reader#peek_line method to pop the include + # stack if the last line has been reached and there's at least + # one include on the stack. + # + # Returns the next line of the source data as a String if there are lines remaining + # in the current include context or a parent include context. + # Returns nil if there are no more lines remaining and the include stack is empty. def peek_line direct = false if (line = super) line - elsif !@include_stack.empty? + elsif @include_stack.empty? + nil + else pop_include peek_line direct - else - nil end end diff --git a/test/reader_test.rb b/test/reader_test.rb index 62daa4d1..e0b4d36e 100644 --- a/test/reader_test.rb +++ b/test/reader_test.rb @@ -708,7 +708,8 @@ include::fixtures/parent-include.adoc[depth=1] end context 'Conditional Inclusions' do - test 'preprocess_line returns nil if cursor advanced' do + #test 'process_line returns next line of content' do + test 'process_line returns nil if cursor advanced' do input = <<-EOS ifdef::asciidoctor[] Asciidoctor! @@ -716,6 +717,7 @@ endif::asciidoctor[] EOS reader = Asciidoctor::PreprocessorReader.new empty_document, input + #assert_equal "Asciidoctor!\n", reader.process_line(reader.lines.first) assert_nil reader.process_line(reader.lines.first) end |
