water 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/water.rb +127 -11
  2. data/water.gemspec +1 -1
  3. metadata +7 -7
@@ -1,32 +1,148 @@
1
+ require 'fileutils'
2
+ require 'pathname'
3
+
1
4
  require 'coderay'
2
5
  require 'launchy'
3
6
 
4
7
  class Water
5
- DIFF_FILE_NAME = 'diff.html'
8
+ DIFF_DIR_NAME = Pathname.new('~/.water').expand_path
6
9
 
7
10
  def self.run
8
11
  new.run
9
12
  end
10
13
 
11
14
  def run
12
- @diff = ARGF.read
15
+ diff = ARGF.read
13
16
 
14
- if @diff.chomp.empty?
17
+ if diff.chomp.empty?
15
18
  puts 'Your diff is empty.'
16
19
  else
17
- write_diff_file
18
- open_diff_file
20
+ DIFF_DIR_NAME.mkpath
21
+ open_diff_file write_diff_file(diff)
19
22
  end
20
23
  end
21
24
 
22
- def write_diff_file
23
- # FIXME: Don't just write into the current folder.
24
- File.open DIFF_FILE_NAME, 'w' do |file|
25
- file.write CodeRay.scan(@diff, :diff).page(:title => 'diff | water')
25
+ def get_file_path
26
+ home = Pathname.new('~').expand_path
27
+ pwd = Pathname.pwd
28
+ relative_path = pwd.relative_path_from(home)
29
+
30
+ name = relative_path.to_s.gsub('../', '').gsub('/', '-')
31
+ name << '.diff.html'
32
+
33
+ DIFF_DIR_NAME + name
34
+ end
35
+
36
+ def write_diff_file diff
37
+ file_path = get_file_path
38
+
39
+ File.open file_path, 'w' do |file|
40
+ file.write water(diff)
26
41
  end
42
+
43
+ file_path
44
+ end
45
+
46
+ def water diff
47
+ output = diff.gsub(/\r\n?/, "\n").scan(/ (?> ^(?!-(?!--)|\+(?!\+\+)|[\\ ]|$|@@) .*\n)* (?> ^(?=-(?!--)|\+(?!\+\+)|[\\ ]|$|@@) .*(?:\n|\z))+ /x).map do |block|
48
+ head_ray, content_ray = CodeRay.scanner(:diff).tokenize(block.split("\n", 2))
49
+ content_ray ||= ''
50
+
51
+ <<-HTML % [head_ray.div(:css => :class), content_ray.div(:css => :class)]
52
+ <div class="diff-block">
53
+ <div class="diff-block-head">%s</div>
54
+ <div class="diff-block-content">%s</div>
55
+ </div>
56
+ HTML
57
+ end.join("\n")
58
+
59
+ output.extend(CodeRay::Encoders::HTML::Output)
60
+ output.css = CodeRay::Encoders::HTML::CSS.new(:alpha)
61
+
62
+ output.css.stylesheet << <<-CSS
63
+
64
+ body {
65
+ background: gray;
66
+ padding-bottom: 1000px;
67
+ }
68
+ body > a {
69
+ display: block;
70
+ position: fixed;
71
+ top: 0;
72
+ right: 0;
73
+ background: gray;
74
+ color: white;
75
+ padding: 5px 10px;
76
+ font-family: monospace;
77
+ font-weight: bold;
78
+ text-decoration: none;
79
+ }
80
+ body > a:hover {
81
+ text-decoration: underline;
82
+ }
83
+
84
+ .diff-block {
85
+ background: hsl(0,0%,95%);
86
+ margin: 5px;
87
+ margin-bottom: 0;
88
+ padding: 3px 6px;
89
+ overflow-y: visible;
90
+ overflow-x: auto;
91
+ -webkit-transition: 0.3s;
92
+ -moz-transition: 0.3s;
93
+ transition: 0.3s;
94
+ }
95
+ .diff-block.closed {
96
+ margin-top: -5px;
97
+ overflow: hidden;
98
+ }
99
+ .diff-block:first-of-type.closed {
100
+ margin-top: 0;
101
+ }
102
+
103
+ .CodeRay pre {
104
+ width: -moz-fit-content;
105
+ line-height: 15px;
106
+ }
107
+ .CodeRay .line {
108
+ float: none;
109
+ height: 15px;
110
+ }
111
+ .diff-block-content .CodeRay .line {
112
+ margin-bottom: -15px;
113
+ }
114
+ CSS
115
+
116
+ output.wrap_in! CodeRay::Encoders::HTML::Output.page_template_for_css(output.css)
117
+ output.apply_title! "diff #{Dir.pwd} | water"
118
+
119
+ output[/<\/head>\s*<body[^>]*>?/] = <<-JS
120
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
121
+ <script>
122
+ $(function () {
123
+ $('.diff-block').live('click', function () {
124
+ $(this).toggleClass('closed').find('.diff-block-content').slideToggle('fast');
125
+ $('html, body').animate({ scrollTop: $(this).offset().top }, 'fast');
126
+ });
127
+ $('.diff-block').live('touchend', function () {
128
+ $(this).toggleClass('closed').find('.diff-block-content').slideToggle('fast');
129
+ $('html, body').animate({ scrollTop: $(this).offset().top }, 'fast');
130
+ });
131
+ $('a.toggle-all').click(function () {
132
+ $('.diff-block').toggleClass('closed').find('.diff-block-content').toggle();
133
+ });
134
+ })
135
+ </script>
136
+ </head>
137
+
138
+ <body>
139
+ <a href="#" class="toggle-all">toggle all</a>
140
+ JS
141
+
142
+ output
27
143
  end
28
144
 
29
- def open_diff_file
30
- Launchy.open "file://#{File.join Dir.pwd, DIFF_FILE_NAME}"
145
+ def open_diff_file file_path
146
+ Launchy.open "file://#{file_path.expand_path}"
31
147
  end
32
148
  end
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "water"
6
- s.version = '0.5.0'
6
+ s.version = '0.6.0'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Kornelius Kalnbach"]
9
9
  s.email = ["murphy@rubychan.de"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: water
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-22 00:00:00.000000000Z
12
+ date: 2011-12-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: coderay
16
- requirement: &70258250363440 !ruby/object:Gem::Requirement
16
+ requirement: &70345625837780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70258250363440
24
+ version_requirements: *70345625837780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: launchy
27
- requirement: &70258250362680 !ruby/object:Gem::Requirement
27
+ requirement: &70345625836900 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 2.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70258250362680
35
+ version_requirements: *70345625836900
36
36
  description: The diff washing machine. See your code changes clearly.
37
37
  email:
38
38
  - murphy@rubychan.de
@@ -70,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  version: '0'
71
71
  requirements: []
72
72
  rubyforge_project:
73
- rubygems_version: 1.8.10
73
+ rubygems_version: 1.8.12
74
74
  signing_key:
75
75
  specification_version: 3
76
76
  summary: diff viewer with code highlighting