Class: Water

Inherits:
Object
  • Object
show all
Defined in:
lib/water.rb

Constant Summary collapse

DIFF_DIR_NAME =
Pathname.new('~/.water').expand_path
CSS =
File.read(File.expand_path('../../assets/water.css', __FILE__))

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runObject



12
13
14
# File 'lib/water.rb', line 12

def self.run
  new.run
end

Instance Method Details

#get_file_pathObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/water.rb', line 27

def get_file_path
  home = Pathname.new('~').expand_path
  pwd  = Pathname.pwd
  relative_path = pwd.relative_path_from(home)
  
  name = relative_path.to_s.gsub('../', '').gsub('/', '-')
  name << '.diff.html'
  
  DIFF_DIR_NAME + name
end

#open_diff_file(file_path) ⇒ Object



94
95
96
# File 'lib/water.rb', line 94

def open_diff_file file_path
  Launchy.open "file://#{file_path.expand_path}"
end

#runObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/water.rb', line 16

def run
  diff = ARGF.read
  
  if diff.chomp.empty?
    puts 'Your diff is empty.'
  else
    DIFF_DIR_NAME.mkpath
    open_diff_file write_diff_file(diff)
  end
end

#water(diff) ⇒ Object



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
# File 'lib/water.rb', line 48

def water diff
  output = diff.gsub(/\r\n?/, "\n").scan(/ (?> ^(?!-(?!--\ )|\+(?!\+\+)|[\\ ]|$|@@) .*\n)* (?> ^(?=-(?!--\ )|\+(?!\+\+)|[\\ ]|$|@@) .*(?:\n|\z))+ /x).map do |block|
    head_ray, content_ray = CodeRay.scanner(:diff).tokenize(block.split("\n", 2))
    content_ray ||= ''
    
    <<-HTML % [head_ray.div(:css => :class), content_ray.div(:css => :class)]
<div class="diff-block">
<div class="diff-block-head">%s</div>
<div class="diff-block-content">%s</div>
</div>
    HTML
  end.join("\n")
  
  output.extend(CodeRay::Encoders::HTML::Output)
  output.css = CodeRay::Encoders::HTML::CSS.new(:alpha)
  output.css.stylesheet << Water::CSS
  
  output.wrap_in! CodeRay::Encoders::HTML::Output.page_template_for_css(output.css)
  output.apply_title! "diff #{Dir.pwd} | water"
  
  output[/<\/head>\s*<body[^>]*>?/] = <<-JS
  <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  <script>
    $(function () {
      $(document).on('click', '.diff-block', function () {
        $(this).toggleClass('closed').find('.diff-block-content').slideToggle('fast');
        $('html, body').animate({ scrollTop: $(this).offset().top }, 'fast');
      });
      $(document).on('touchend', '.diff-block', function () {
        $(this).toggleClass('closed').find('.diff-block-content').slideToggle('fast');
        $('html, body').animate({ scrollTop: $(this).offset().top }, 'fast');
      });
      $(document).on('click', 'a.toggle-all', function () {
        $('.diff-block').toggleClass('closed').find('.diff-block-content').toggle();
      });
    });
  </script>
  </head>
  
  <body>
    <a href="#" class="toggle-all">toggle all</a>
  JS
  
  output
end

#write_diff_file(diff) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/water.rb', line 38

def write_diff_file diff
  file_path = get_file_path
  
  File.open file_path, 'w' do |file|
    file.write water(diff)
  end
  
  file_path
end