summaryrefslogtreecommitdiff
path: root/lib/coderay/scanners
diff options
context:
space:
mode:
Diffstat (limited to 'lib/coderay/scanners')
-rw-r--r--lib/coderay/scanners/diff.rb32
-rw-r--r--lib/coderay/scanners/ruby.rb15
-rw-r--r--lib/coderay/scanners/ruby/string_state.rb2
3 files changed, 30 insertions, 19 deletions
diff --git a/lib/coderay/scanners/diff.rb b/lib/coderay/scanners/diff.rb
index a8a63e9..fd12922 100644
--- a/lib/coderay/scanners/diff.rb
+++ b/lib/coderay/scanners/diff.rb
@@ -11,7 +11,7 @@ module Scanners
DEFAULT_OPTIONS = {
:highlight_code => true,
- :inline_diff => true,
+ :inline_diff => true,
}
protected
@@ -73,7 +73,7 @@ module Scanners
next unless match = scan(/.+/)
encoder.text_token match, :plain
elsif match = scan(/@@(?>[^@\n]*)@@/)
- content_scanner.instance_variable_set(:@state, :initial) unless match?(/\n\+/)
+ content_scanner.state = :initial unless match?(/\n\+/)
content_scanner_entry_state = nil
if check(/\n|$/)
encoder.begin_line line_kind = :change
@@ -106,37 +106,39 @@ module Scanners
encoder.begin_line line_kind = :delete
encoder.text_token match, :delete
if options[:inline_diff] && deleted_lines == 1 && check(/(?>.*)\n\+(?>.*)$(?!\n\+)/)
- if content_scanner.instance_variable_defined?(:@state)
- content_scanner_entry_state = content_scanner.instance_variable_get(:@state)
- end
+ content_scanner_entry_state = content_scanner.state
skip(/(.*)\n\+(.*)$/)
head, deletion, insertion, tail = diff self[1], self[2]
pre, deleted, post = content_scanner.tokenize [head, deletion, tail], :tokens => Tokens.new
encoder.tokens pre
- encoder.begin_group :eyecatcher
- encoder.tokens deleted
- encoder.end_group :eyecatcher
+ unless deleted.empty?
+ encoder.begin_group :eyecatcher
+ encoder.tokens deleted
+ encoder.end_group :eyecatcher
+ end
encoder.tokens post
encoder.end_line line_kind
encoder.text_token "\n", :space
encoder.begin_line line_kind = :insert
encoder.text_token '+', :insert
- content_scanner.instance_variable_set(:@state, content_scanner_entry_state || :initial)
+ content_scanner.state = content_scanner_entry_state || :initial
pre, inserted, post = content_scanner.tokenize [head, insertion, tail], :tokens => Tokens.new
encoder.tokens pre
- encoder.begin_group :eyecatcher
- encoder.tokens inserted
- encoder.end_group :eyecatcher
+ unless inserted.empty?
+ encoder.begin_group :eyecatcher
+ encoder.tokens inserted
+ encoder.end_group :eyecatcher
+ end
encoder.tokens post
elsif match = scan(/.*/)
if options[:highlight_code]
- if deleted_lines == 1 && content_scanner.instance_variable_defined?(:@state)
- content_scanner_entry_state = content_scanner.instance_variable_get(:@state)
+ if deleted_lines == 1
+ content_scanner_entry_state = content_scanner.state
end
content_scanner.tokenize match, :tokens => encoder unless match.empty?
if !match?(/\n-/)
if match?(/\n\+/)
- content_scanner.instance_variable_set(:@state, content_scanner_entry_state || :initial)
+ content_scanner.state = content_scanner_entry_state || :initial
end
content_scanner_entry_state = nil
end
diff --git a/lib/coderay/scanners/ruby.rb b/lib/coderay/scanners/ruby.rb
index 299accc..b3c7de1 100644
--- a/lib/coderay/scanners/ruby.rb
+++ b/lib/coderay/scanners/ruby.rb
@@ -23,8 +23,9 @@ module Scanners
end
def scan_tokens encoder, options
+ state, heredocs = @state
+ heredocs = heredocs.dup if heredocs.is_a?(Array)
- state = @state
if state && state.instance_of?(self.class::StringState)
encoder.begin_group state.type
end
@@ -34,10 +35,15 @@ module Scanners
method_call_expected = false
value_expected = true
- heredocs = nil
inline_block_stack = nil
inline_block_curly_depth = 0
+ if heredocs
+ state = heredocs.shift
+ encoder.begin_group state.type
+ heredocs = nil if heredocs.empty?
+ end
+
# def_object_stack = nil
# def_object_paren_depth = 0
@@ -421,11 +427,14 @@ module Scanners
# cleaning up
if options[:keep_state]
- @state = state
+ heredocs = nil if heredocs && heredocs.empty?
+ @state = state, heredocs
end
+
if state.is_a? self.class::StringState
encoder.end_group state.type
end
+
if inline_block_stack
until inline_block_stack.empty?
state, = *inline_block_stack.pop
diff --git a/lib/coderay/scanners/ruby/string_state.rb b/lib/coderay/scanners/ruby/string_state.rb
index 14127c6..2f398d1 100644
--- a/lib/coderay/scanners/ruby/string_state.rb
+++ b/lib/coderay/scanners/ruby/string_state.rb
@@ -55,7 +55,7 @@ module Scanners
def heredoc_pattern delim, interpreted, indented
# delim = delim.dup # workaround for old Ruby
delim_pattern = Regexp.escape(delim)
- delim_pattern = / \n #{ '(?>[ \t]*)' if indented } #{ Regexp.new delim_pattern } $ /x
+ delim_pattern = / (?:\A|\n) #{ '(?>[ \t]*)' if indented } #{ Regexp.new delim_pattern } $ /x
if interpreted
/ (?= #{delim_pattern}() | \\ | \# [{$@] ) /mx # $1 set == end of heredoc
else