summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Changes.textile19
-rw-r--r--lib/coderay/scanners/diff.rb48
2 files changed, 50 insertions, 17 deletions
diff --git a/Changes.textile b/Changes.textile
index ce89a51..0625234 100644
--- a/Changes.textile
+++ b/Changes.textile
@@ -40,10 +40,21 @@ h3. @Encoders::TokenKindFilter@
h3. @Scanners@
-* *FIXED* @Scanner.normify@ doesn't output errors in @$DEBUG@ mode any more.
-* *FIXED*: @scan_tokens@ and @setup@ methods are protected now.
-* *NEW* Mapped @:patch@ to @:diff@.
-* *REMOVED* @:ecma@ and @:h@ - not language names
+* *NEW*: Highlighting of code based on file names.
+ See ticket "#52":http://redmine.rubychan.de/issues/52.
+
+ This is a very original feature. It enables multi-language highlighting for
+ diff files, which is especially helpful for CodeRay development itself. The
+ updated version of the scanner test suite generated .debug.diff.html files
+ using this.
+
+ Note: This is still buggy for multi-line tokens.
+* *NEW*: Highlighting of the file name in the change headers as @:filename@.
+
+h3. @Scanners::Diff@
+
+* *FIXED* Ruby 1.9.2 compatibility problem (see revision 463).
+* *REMOVED* @String#to_unix@.
h3. @Scanners::Scanner@
diff --git a/lib/coderay/scanners/diff.rb b/lib/coderay/scanners/diff.rb
index bfb400b..6ba50ef 100644
--- a/lib/coderay/scanners/diff.rb
+++ b/lib/coderay/scanners/diff.rb
@@ -1,15 +1,24 @@
module CodeRay
module Scanners
+ # Scanner for output of the diff command.
+ #
+ # Alias: +patch+
class Diff < Scanner
register_for :diff
title 'diff output'
+ protected
+
+ require 'coderay/helpers/file_type'
+
def scan_tokens tokens, options
line_kind = nil
state = :initial
+ # TODO: Cache scanners
+ content_lang = nil
until eos?
kind = match = nil
@@ -29,6 +38,10 @@ module Scanners
if match = scan(/--- |\+\+\+ |=+|_+/)
tokens << [:begin_line, line_kind = :head]
tokens << [match, :head]
+ if filename = scan(/.*?(?=$|[\t\n\x00]| \(revision)/)
+ tokens << [filename, :filename]
+ content_lang = FileType.fetch filename, :plaintext
+ end
next unless match = scan(/.+/)
kind = :plain
elsif match = scan(/Index: |Property changes on: /)
@@ -47,25 +60,34 @@ module Scanners
tokens << [match, :change]
next unless match = scan(/.+/)
kind = :plain
- elsif scan(/(@@)((?>[^@\n]*))(@@)/)
- tokens << [:begin_line, line_kind = :change]
- tokens << [self[1], :change]
- tokens << [self[2], :plain]
- tokens << [self[3], :change]
- next unless match = scan(/.+/)
- kind = :plain
+ elsif match = scan(/@@(?>[^@\n]*)@@/)
+ if check(/\n|$/)
+ tokens << [:begin_line, line_kind = :change]
+ else
+ tokens << [:open, :change]
+ end
+ tokens << [match[0,2], :change]
+ tokens << [match[2...-2], :plain] if match.size > 4
+ tokens << [match[-2,2], :change]
+ tokens << [:close, :change] unless line_kind
+ next unless code = scan(/.+/)
+ CodeRay.scan code, content_lang, :tokens => tokens
+ next
elsif match = scan(/\+/)
tokens << [:begin_line, line_kind = :insert]
tokens << [match, :insert]
next unless match = scan(/.+/)
- kind = :plain
+ CodeRay.scan match, content_lang, :tokens => tokens
+ next
elsif match = scan(/-/)
tokens << [:begin_line, line_kind = :delete]
tokens << [match, :delete]
- next unless match = scan(/.+/)
- kind = :plain
- elsif scan(/ .*/)
- kind = :comment
+ next unless code = scan(/.+/)
+ CodeRay.scan code, content_lang, :tokens => tokens
+ next
+ elsif code = scan(/ .*/)
+ CodeRay.scan code, content_lang, :tokens => tokens
+ next
elsif scan(/.+/)
tokens << [:begin_line, line_kind = :head]
kind = :plain
@@ -86,7 +108,7 @@ module Scanners
end
match ||= matched
- if $DEBUG and not kind
+ if $CODERAY_DEBUG and not kind
raise_inspect 'Error token %p in line %d' %
[[match, kind], line], tokens
end