wcc 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,21 +32,19 @@
32
32
 
33
33
  .diff .ins {
34
34
  background: #dfd;
35
- /* color: #080;*/
36
35
  }
37
36
 
38
- .diff .ins .hilite {
39
- background: #afa;
37
+ .diff .ins .x {
38
+ background: #8f8;
40
39
  margin: 0;
41
40
  padding: 0;
42
41
  }
43
42
 
44
43
  .diff .del {
45
44
  background: #fdd;
46
- /* color: #b00;*/
47
45
  }
48
46
 
49
- .diff .del .hilite {
47
+ .diff .del .x {
50
48
  background: #faa;
51
49
  margin: 0;
52
50
  padding: 0;
@@ -80,11 +78,11 @@
80
78
  <% elsif o.status == :range %>
81
79
  <li class="range">@@<%= o.text %></li>
82
80
  <% elsif o.status == :ins %>
83
- <li class="ins">+<%= o.html_hilite_text %></li>
81
+ <li class="ins">+<%= o.html_hilite_text('x') %></li>
84
82
  <% elsif o.status == :del %>
85
- <li class="del">-<%= o.html_hilite_text %></li>
83
+ <li class="del">-<%= o.html_hilite_text('x') %></li>
86
84
  <% else %>
87
- <li class="other"><%= o.text.gsub(/^ /, "&nbsp;") %></li>
85
+ <li class="other"><%= o.text == ' ' ? '&nbsp;' : o.text %></li>
88
86
  <% end %>
89
87
  <% end %>
90
88
  </ul>
data/bin/wcc-upgrade CHANGED
@@ -18,6 +18,8 @@ def traverse(root, dst_root, path)
18
18
  rel = p.relative_path_from(root)
19
19
  src = root + rel
20
20
  dst = dst_root + rel
21
+ # make sure the dst directory exists
22
+ FileUtils.mkdir_p dst.dirname
21
23
  if dst.exist?
22
24
  # do compare
23
25
  same = FileUtils.compare_file(src, dst)
data/lib/wcc/diff.rb CHANGED
@@ -1,9 +1,12 @@
1
1
 
2
2
  module WCC
3
+ # TODO: Handle tabs/trailing whitespace in output
4
+
3
5
  class DiffItem
4
6
  attr_accessor :status, :text, :hilite
5
7
 
6
8
  def initialize(line)
9
+ # parse line
7
10
  if line.start_with?('+++')
8
11
  @status = :new
9
12
  @text = line.substring(3)
@@ -15,13 +18,14 @@ module WCC
15
18
  @text = line.substring(2)
16
19
  elsif line.start_with?('+')
17
20
  @status = :ins
18
- @text = line.substring(1)
21
+ @text = line.substring(1).rstrip
19
22
  elsif line.start_with?('-')
20
23
  @status = :del
21
- @text = line.substring(1)
24
+ @text = line.substring(1).rstrip
22
25
  else
23
26
  @status = :other
24
- @text = line
27
+ @text = line.rstrip
28
+ @text = ' ' if @text.empty?
25
29
  end
26
30
  @text.gsub!(/\n/, '')
27
31
  @hilite = nil
@@ -30,22 +34,22 @@ module WCC
30
34
  def html_hilite_text(css_klass = 'hilite')
31
35
  return @text if @hilite.nil?
32
36
 
33
- i = 1
37
+ i = 0
34
38
  new_text = ''
35
39
  in_span = false
36
40
  @text.chars.to_a.each do |c|
37
41
  if @hilite.include?(i)
38
42
  if not in_span
39
43
  new_text += "<span class=\"#{css_klass}\">"
44
+ in_span = true
40
45
  end
41
- new_text += c
42
- in_span = true
46
+ new_text += (c == ' ' ? '&nbsp;' : c)
43
47
  else
44
48
  if in_span
45
49
  new_text += "</span>"
50
+ in_span = false
46
51
  end
47
52
  new_text += c
48
- in_span = false
49
53
  end
50
54
  i += 1
51
55
  end
@@ -53,6 +57,8 @@ module WCC
53
57
  new_text
54
58
  end
55
59
 
60
+ # Returns a representing character for the kind of this diff item.
61
+ # @return [String] single rep char
56
62
  def rchar
57
63
  case status
58
64
  when :new
@@ -70,6 +76,8 @@ module WCC
70
76
  end
71
77
  end
72
78
 
79
+ # Returns an unified diff line without trailing newline.
80
+ # @return [String] unified diff line
73
81
  def to_s
74
82
  case status
75
83
  when :new
@@ -92,18 +100,14 @@ module WCC
92
100
  attr_reader :di
93
101
 
94
102
  def initialize(dstring)
95
- @di = []
96
- dstring.lines.each do |line|
97
- # parse line
98
- @di << DiffItem.new(line)
99
- end
100
- # TODO: compute_hilite, wrong +/- detection
103
+ @di = dstring.lines.map { |line| DiffItem.new(line) }
104
+ compute_hilite
101
105
  end
102
106
 
103
107
  def compute_hilite
108
+ # get representional string for the whole diff
104
109
  s = rchar
105
- puts s
106
-
110
+ #puts s
107
111
  mds = []
108
112
  md = s.match(/(@|_)di(@|_)/)
109
113
  while not md.nil?
@@ -116,19 +120,8 @@ module WCC
116
120
  mds.each do |md|
117
121
  i = offset+md.begin(1)+1
118
122
  offset = md.begin(2)+1
119
- ranges = Diff::LCS.diff(@di[i].text, @di[i+1].text)
120
- @di[i].hilite = []
121
- @di[i+1].hilite = []
122
- ranges.each do |chg|
123
- chg.each do |c|
124
- if c.action == '-' and c.element != ''
125
- @di[i].hilite << c.position
126
- end
127
- if c.action == '+' and c.element != ''
128
- @di[i+1].hilite << c.position
129
- end
130
- end
131
- end
123
+ # found a single insertion/deletion pair
124
+ InLineDiffer.new(@di[i], @di[i+1]).compute_hilite
132
125
  end
133
126
  end
134
127
 
@@ -140,4 +133,38 @@ module WCC
140
133
  @di.map { |o| o.to_s }.join("\n")
141
134
  end
142
135
  end
136
+
137
+ # Calculates hilite based on per char side-by-side diff for two DiffItems.
138
+ class InLineDiffer
139
+ def initialize(a, b)
140
+ @a = a
141
+ @b = b
142
+ @a.hilite = []
143
+ @b.hilite = []
144
+ end
145
+
146
+ def compute_hilite
147
+ #puts @a.text.chars.to_a.inspect
148
+ #puts @b.text.chars.to_a.inspect
149
+ # HACK: Diff::LCS with plain strings fails on Ruby 1.8 even with -Ku flag but not: <string>.chars.to_a
150
+ Diff::LCS.traverse_balanced(@a.text.chars.to_a, @b.text.chars.to_a, self)
151
+ end
152
+
153
+ def match(e)
154
+ # don't care - this is called "diff" ;-)
155
+ end
156
+
157
+ def discard_a(e)
158
+ @a.hilite << e.old_position if not @a.hilite.include?(e.old_position)
159
+ end
160
+
161
+ def discard_b(e)
162
+ @b.hilite << e.new_position if not @b.hilite.include?(e.new_position)
163
+ end
164
+
165
+ def change(e)
166
+ @a.hilite << e.old_position if not @a.hilite.include?(e.old_position)
167
+ @b.hilite << e.new_position if not @b.hilite.include?(e.new_position)
168
+ end
169
+ end
143
170
  end
data/lib/wcc.rb CHANGED
@@ -16,7 +16,7 @@ require 'uri'
16
16
  require 'yaml'
17
17
 
18
18
  # ruby gem dependencies
19
- #require 'diff-lcs'
19
+ require 'diff-lcs'
20
20
  require 'htmlentities'
21
21
 
22
22
  # wcc
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wcc
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Christian Nicolai
@@ -31,6 +31,20 @@ dependencies:
31
31
  version: "0"
32
32
  type: :runtime
33
33
  version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: diff-lcs
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
34
48
  description: wcc tracks changes of websites and notifies you by email.
35
49
  email: chrnicolai@gmail.com
36
50
  executables: