git-commit-notifier 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,126 +0,0 @@
1
- require 'cgi'
2
-
3
- class ResultProcessor
4
- # input (loaded in @diff) is an array having Hash elements:
5
- # { :action => action, :token => string }
6
- # action can be :discard_a, :discard_b or :match
7
-
8
- # output: two formatted html strings, one for the removals and one for the additions
9
-
10
- def results
11
- close_tags # close last tag
12
- [array_of_lines(@result[:removal]), array_of_lines(@result[:addition])]
13
- end
14
-
15
- def length_in_chars(diff)
16
- diff.inject(0) do |length, s|
17
- token = s[:token]
18
- token_length = token.respond_to?(:jlength) ? token.jlength : token.length
19
- length + token_length
20
- end
21
- end
22
-
23
- private
24
-
25
- def initialize(diff)
26
- @diff = diff
27
- init
28
- filter_replaced_lines
29
- process
30
- end
31
-
32
- def init
33
- @result = { :addition => [], :removal => [] }
34
- @tag_open = { :addition => false, :removal => false}
35
- end
36
-
37
- def process
38
- @highlight = !@diff.select { |d| d[:action] == :match}.empty? # highlight only if block contains both matches and differences
39
- @diff.each do |diff|
40
- case diff[:action]
41
- when :match
42
- match(diff)
43
- when :discard_a
44
- discard_a(diff)
45
- when :discard_b
46
- discard_b(diff)
47
- end
48
- end
49
- end
50
-
51
- def discard_match(position, token)
52
- # replace it with 2 changes
53
- @diff[position][:action] = :discard_a
54
- @diff.insert(position, { :action => :discard_b, :token => token} )
55
- end
56
-
57
- def filter_replaced_lines
58
- # if a block is replaced by an other one, lcs-diff will find even the single common word between the old and the new content
59
- # no need for intelligent diff in this case, simply show the removed and the added block with no highlighting
60
- # rule: if less than 33% of a block is not a match, we don't need intelligent diff for that block
61
- match_length = length_in_chars(@diff.select { |d| d[:action] == :match })
62
- total_length = length_in_chars(@diff)
63
-
64
- if total_length.to_f / match_length > 3.3
65
- @diff.each_with_index do |d, i|
66
- next if d[:action] != :match
67
- discard_match(i, d[:token])
68
- end
69
- end
70
- end
71
-
72
- def match(diff)
73
- close_tags
74
- all_actions do |action|
75
- close_last_tag(diff)
76
- @result[action] << escape_content(diff[:token])
77
- end
78
- end
79
-
80
- def discard_a(diff)
81
- open_tag(:removal, diff[:token])
82
- close_last_tag(diff)
83
- @result[:removal] << escape_content(diff[:token])
84
- end
85
-
86
- def discard_b(diff)
87
- open_tag(:addition, diff[:token])
88
- close_last_tag(diff)
89
- @result[:addition] << escape_content(diff[:token])
90
- end
91
-
92
- def all_actions
93
- [:addition, :removal].each do |action|
94
- yield(action)
95
- end
96
- end
97
-
98
- def open_tag(action, next_token)
99
- return if !@highlight || next_token.strip.empty? # don't open span tag if no highlighting is needed or the first token is empty
100
- unless @tag_open[action]
101
- klass = action == :addition ? 'aa' : 'rr'
102
- @result[action] << "<span class=\"#{klass}\">"
103
- @tag_open[action] = true
104
- end
105
- end
106
-
107
- def close_tags
108
- return unless @highlight
109
- all_actions do |action|
110
- if @tag_open[action]
111
- @result[action] << "</span>"
112
- @tag_open[action] = false
113
- end
114
- end
115
- end
116
-
117
- def close_last_tag(diff)
118
- return unless @highlight
119
- close_tags if diff[:token] == "\n"
120
- end
121
-
122
- def array_of_lines(tokens)
123
- tokens.join('').split("\n")
124
- end
125
-
126
- end