summaryrefslogtreecommitdiff
path: root/lib/coderay/scanners/diff.rb
blob: 417985aa9ded111a1149958385483b49c9ae0d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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 encoder, options
      
      line_kind = nil
      state = :initial
      # TODO: Cache scanners
      content_lang = nil
      
      until eos?
        
        if match = scan(/\n/)
          if line_kind
            encoder.end_line line_kind
            line_kind = nil
          end
          encoder.text_token match, :space
          next
        end
        
        case state
        
        when :initial
          if match = scan(/--- |\+\+\+ |=+|_+/)
            encoder.begin_line line_kind = :head
            encoder.text_token match, :head
            if match = scan(/.*?(?=$|[\t\n\x00]|  \(revision)/)
              encoder.text_token match, :filename
              content_lang = FileType.fetch match, :plaintext
            end
            next unless match = scan(/.+/)
            encoder.text_token match, :plain
          elsif match = scan(/Index: |Property changes on: /)
            encoder.begin_line line_kind = :head
            encoder.text_token match, :head
            next unless match = scan(/.+/)
            encoder.text_token match, :plain
          elsif match = scan(/Added: /)
            encoder.begin_line line_kind = :head
            encoder.text_token match, :head
            next unless match = scan(/.+/)
            encoder.text_token match, :plain
            state = :added
          elsif match = scan(/\\ /)
            encoder.begin_line line_kind = :change
            encoder.text_token match, :change
            next unless match = scan(/.+/)
            encoder.text_token match, :plain
          elsif match = scan(/@@(?>[^@\n]*)@@/)
            if check(/\n|$/)
              encoder.begin_line line_kind = :change
            else
              encoder.begin_group :change
            end
            encoder.text_token match[0,2], :change
            encoder.text_token match[2...-2], :plain if match.size > 4
            encoder.text_token match[-2,2], :change
            encoder.end_group :change unless line_kind
            next unless match = scan(/.+/)
            CodeRay.scan match, content_lang, :tokens => encoder
            next
          elsif match = scan(/\+/)
            encoder.begin_line line_kind = :insert
            encoder.text_token match, :insert
            next unless match = scan(/.+/)
            CodeRay.scan match, content_lang, :tokens => encoder
            next
          elsif match = scan(/-/)
            encoder.begin_line line_kind = :delete
            encoder.text_token match, :delete
            next unless match = scan(/.+/)
            CodeRay.scan match, content_lang, :tokens => encoder
            next
          elsif match = scan(/ .*/)
            CodeRay.scan match, content_lang, :tokens => encoder
            next
          elsif match = scan(/.+/)
            encoder.begin_line line_kind = :comment
            encoder.text_token match, :plain
          else
            raise_inspect 'else case rached'
          end
        
        when :added
          if match = scan(/   \+/)
            encoder.begin_line line_kind = :insert
            encoder.text_token match, :insert
            next unless match = scan(/.+/)
            encoder.text_token match, :plain
          else
            state = :initial
            next
          end
        end
        
      end
      
      encoder.end_line line_kind if line_kind
      
      encoder
    end
    
  end
  
end
end