Class: HtmlDiff

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ HtmlDiff

Returns a new instance of HtmlDiff.



4
5
6
7
# File 'lib/html_diff.rb', line 4

def initialize(argv=[])
  @wd = `pwd`.gsub("\n", "")
  @argv = argv
end

Instance Attribute Details

#argvObject

Returns the value of attribute argv.



2
3
4
# File 'lib/html_diff.rb', line 2

def argv
  @argv
end

#wdObject

Returns the value of attribute wd.



2
3
4
# File 'lib/html_diff.rb', line 2

def wd
  @wd
end

Instance Method Details

#branchObject



23
24
25
26
# File 'lib/html_diff.rb', line 23

def branch
  status=`git status`
  status.scan(/On branch (.*)\n/).last.last
end

#contentObject



121
122
123
124
125
# File 'lib/html_diff.rb', line 121

def content
  content = initial_content
  content += "<h2>" + branch + "<br>" + stats_line + "</h2>"
  content += html_diff+"</body></html>"
end

#html_diffObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/html_diff.rb', line 28

def html_diff
  diff=`git diff #{@argv.join(" ")}`
  diff.gsub!("<", "&lt;")
  diff.gsub!(">", "&gt;")
  diff.gsub!(/^index [a-z0-9]{7}\.\.[a-z0-9]{7}$/, "")
  diff.gsub!(/^diff \-\-git a\/([^\s]*).*\n.*$/) { "<div class='diff-file'>"+$1+"</div>" }
  diff.gsub!(/^(\-\-\-\s)((?:a.*)|(?:\/dev\/null.*))$/) { "<pre class='code'><span class='delete'>"+$1+"&nbsp;"+$2+"</span>" }
  diff.gsub!(/(\n\<div class\=\'diff-file\'\>)/) { "</pre>"+$1}
  diff.gsub!(/^(\@\@.*)$/) { "<br><span class='at-linenums'>"+$1+"</span>"}

  diff.gsub!(/^(\++)(.*)$/) { "<span class='add'>"+$1+"&nbsp;"+$2+"</span>" }
  diff.gsub!(/^(\-+)(.*)$/) { "<span class='delete'>"+$1+"&nbsp;"+$2+"</span>" }
  diff
end

#initial_contentObject



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

def initial_content
  '''
  <!DOCTYPE html>

  <html>
  <head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
  <style>
  body {
    font-family: "Lucida Console", Monaco, monospace;
    font-size: 13px;
    padding-bottom: 50px;
  }
  h2 {
    text-align: center;
    font-size: 1.4em;
  }
  .diff-file {
    background: -webkit-linear-gradient(#fafafa, #eaeaea);
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=\'#fafafa\',endColorstr=\'#eaeaea\')";
    border: 1px solid #d8d8d8;
    border-bottom: 0;
    color: #555;
    font: 14px sans-serif;
    overflow: hidden;
    padding: 12px 0px 12px 25px;
    text-shadow: 0 1px 0 white;
    width: 80%;
    margin: 50px auto 17px;
    float: none;
  }
  .code .add {
    background-color: #DDFFDD;
    padding: 2px 2% 2px 10px;
    margin-left: -25px;
    display:inline-block;
    width: 105%;
  }
  .code .delete {
    background-color: #FFDDDD;
    padding: 2px 2% 2px 10px;
    margin-left: -25px;
    display:inline-block;
    width: 105%;
  }
  .code .at-linenums {
    color: #9B9B99;
  }
  .tab {
    width: 50px;
    height: 13px;
    display: inline-block;
  }
  .code {
    border: 1px solid #cacaca;
    line-height: 1.7em;
    overflow: hidden;
    -webkit-border-radius: 0 0 3px 3px;
    -moz-border-radius: 0 0 3px 3px;
    border-radius: 0 0 3px 3px;
    -moz-background-clip: padding;
    -webkit-background-clip: padding-box;
    background-clip: padding-box;
    background-color: #FAFAFB;
    color: #393939;
    padding: 5px 0px 5px 25px;
    width: 80%;
    margin: -18px auto;
    overflow: scroll;
    float: none;
  }
  </style>
  </head>
  <body>
  '''
end

#make_diffObject



127
128
129
130
131
132
133
134
# File 'lib/html_diff.rb', line 127

def make_diff
  File.open(@wd+"/mydiff.html", "wb+") do |f|
    f.write(content)
  end

  `open #{@wd}/mydiff.html`
  `sleep 5 && rm #{@wd}/mydiff.html`
end

#stats_lineObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/html_diff.rb', line 9

def stats_line
  stats=`git diff --stat #{@argv.join(" ")}`
  if stats == ""
    return "no changes"
  end
  out_stats=[]
  out_stats << stats.scan(/\d+ files? changed/).last
  out_stats << stats.scan(/\d+ insertions?\(\+\)/).last
  out_stats << stats.scan(/\d+ deletions?\(\-\)/).last
  out_stats.reject!(&:nil?)
  
  out_stats.join(", ")
end