Class: Coopy::DiffRender

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDiffRender

Returns a new instance of DiffRender.



7
8
9
10
11
# File 'lib/lib/coopy/diff_render.rb', line 7

def initialize
  @text_to_insert = Array.new
  @open = false
  @pretty_arrows = true
end

Instance Attribute Details

#openObject

Returns the value of attribute open.



18
19
20
# File 'lib/lib/coopy/diff_render.rb', line 18

def open
  @open
end

#pretty_arrowsObject

Returns the value of attribute pretty_arrows.



19
20
21
# File 'lib/lib/coopy/diff_render.rb', line 19

def pretty_arrows
  @pretty_arrows
end

#td_closeObject

Returns the value of attribute td_close.



17
18
19
# File 'lib/lib/coopy/diff_render.rb', line 17

def td_close
  @td_close
end

#td_openObject

Returns the value of attribute td_open.



16
17
18
# File 'lib/lib/coopy/diff_render.rb', line 16

def td_open
  @td_open
end

#text_to_insertObject

protected - in ruby this doesn’t play well with static/inline methods



15
16
17
# File 'lib/lib/coopy/diff_render.rb', line 15

def text_to_insert
  @text_to_insert
end

Class Method Details

.examine_cell(x, y, value, vcol, vrow, vcorner, cell) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/lib/coopy/diff_render.rb', line 135

def DiffRender.examine_cell(x,y,value,vcol,vrow,vcorner,cell)
  cell.category = ""
  cell.category_given_tr = ""
  cell.separator = ""
  cell.conflicted = false
  cell.updated = false
  cell.pvalue = cell.lvalue = cell.rvalue = nil
  cell.value = value
  cell.value = "" if cell.value == nil
  cell.pretty_value = cell.value
  vrow = "" if vrow == nil
  vcol = "" if vcol == nil
  removed_column = false
  cell.category = "move" if vrow == ":"
  if (vcol.index("+++",nil || 0) || -1) >= 0 
    cell.category_given_tr = cell.category = "add"
  elsif (vcol.index("---",nil || 0) || -1) >= 0 
    cell.category_given_tr = cell.category = "remove"
    removed_column = true
  end
  if vrow == "!" 
    cell.category = "spec"
  elsif vrow == "@@" 
    cell.category = "header"
  elsif vrow == "+++" 
    cell.category = "add" if !removed_column
  elsif vrow == "---" 
    cell.category = "remove"
  elsif (vrow.index("->",nil || 0) || -1) >= 0 
    if !removed_column 
      tokens = vrow.split("!")
      full = vrow
      part = tokens[1]
      part = full if part == nil
      if (cell.value.index(part,nil || 0) || -1) >= 0 
        cat = "modify"
        div = part
        if part != full 
          if (cell.value.index(full,nil || 0) || -1) >= 0 
            div = full
            cat = "conflict"
            cell.conflicted = true
          end
        end
        cell.updated = true
        cell.separator = div
        if cell.pretty_value == div 
          tokens = ["",""]
        else 
          tokens = cell.pretty_value.split(div)
        end
        pretty_tokens = tokens
        if tokens.length >= 2 
          pretty_tokens[0] = ::Coopy::DiffRender.mark_spaces(tokens[0],tokens[1])
          pretty_tokens[1] = ::Coopy::DiffRender.mark_spaces(tokens[1],tokens[0])
        end
        if tokens.length >= 3 
          ref = pretty_tokens[0]
          pretty_tokens[0] = ::Coopy::DiffRender.mark_spaces(ref,tokens[2])
          pretty_tokens[2] = ::Coopy::DiffRender.mark_spaces(tokens[2],ref)
        end
        cell.pretty_value = pretty_tokens.join([8594].pack("U"))
        cell.category_given_tr = cell.category = cat
        offset = nil
        if cell.conflicted 
          offset = 1
        else 
          offset = 0
        end
        cell.lvalue = tokens[offset]
        cell.rvalue = tokens[offset + 1]
        cell.pvalue = tokens[0] if cell.conflicted
      end
    end
  end
end

.mark_spaces(sl, sr) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/lib/coopy/diff_render.rb', line 212

def DiffRender.mark_spaces(sl,sr)
  return sl if sl == sr
  return sl if sl == nil || sr == nil
  slc = sl.gsub(" ","")
  src = sr.gsub(" ","")
  return sl if slc != src
  slo = String.new("")
  il = 0
  ir = 0
  while(il < sl.length) 
    cl = sl[il]
    cr = ""
    cr = sr[ir] if ir < sr.length
    if cl == cr 
      slo += cl
      il+=1
      ir+=1
    elsif cr == " " 
      ir+=1
    else 
      slo += [9251].pack("U")
      il+=1
    end
  end
  return slo
end

.render_cell(tt, x, y) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/lib/coopy/diff_render.rb', line 239

def DiffRender.render_cell(tt,x,y)
  cell = ::Coopy::CellInfo.new
  corner = tt.get_cell_text(0,0)
  off = nil
  if corner == "@:@" 
    off = 1
  else 
    off = 0
  end
  ::Coopy::DiffRender.examine_cell(x,y,tt.get_cell_text(x,y),tt.get_cell_text(x,off),tt.get_cell_text(off,y),corner,cell)
  return cell
end

Instance Method Details

#begin_row(mode) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lib/coopy/diff_render.rb', line 37

def begin_row(mode)
  @td_open = "<td"
  @td_close = "</td>"
  row_class = ""
  if mode == "header" 
    @td_open = "<th"
    @td_close = "</th>"
  else 
    row_class = mode
  end
  tr = "<tr>"
  tr = "<tr class=\"" + _hx_str(row_class) + "\">" if row_class != ""
  self.insert(tr)
end

#begin_tableObject



33
34
35
# File 'lib/lib/coopy/diff_render.rb', line 33

def begin_table 
  self.insert("<table>\n")
end

#complete_htmlObject



128
129
130
131
132
133
# File 'lib/lib/coopy/diff_render.rb', line 128

def complete_html 
  @text_to_insert.insert(0,"<html>\n<meta charset='utf-8'>\n<head>\n<style TYPE='text/css'>\n")
  @text_to_insert.insert(1,self.sample_css)
  @text_to_insert.insert(2,"</style>\n</head>\n<body>\n<div class='highlighter'>\n")
  @text_to_insert.push("</div>\n</body>\n</html>\n")
end

#end_rowObject



60
61
62
# File 'lib/lib/coopy/diff_render.rb', line 60

def end_row 
  self.insert("</tr>\n")
end

#end_tableObject



64
65
66
# File 'lib/lib/coopy/diff_render.rb', line 64

def end_table 
  self.insert("</table>\n")
end

#htmlObject



70
71
72
# File 'lib/lib/coopy/diff_render.rb', line 70

def html 
  return @text_to_insert.join("")
end

#insert(str) ⇒ Object

protected - in ruby this doesn’t play well with static/inline methods



29
30
31
# File 'lib/lib/coopy/diff_render.rb', line 29

def insert(str)
  @text_to_insert.push(str)
end

#insert_cell(txt, mode) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/lib/coopy/diff_render.rb', line 52

def insert_cell(txt,mode)
  cell_decorate = ""
  cell_decorate = " class=\"" + _hx_str(mode) + "\"" if mode != ""
  self.insert(_hx_str(@td_open) + _hx_str(cell_decorate) + ">")
  self.insert(txt)
  self.insert(@td_close)
end

#render(rows) ⇒ Object



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
120
121
122
# File 'lib/lib/coopy/diff_render.rb', line 78

def render(rows)
  return if rows.get_width == 0 || rows.get_height == 0
  render = self
  render.begin_table
  change_row = -1
  tt = ::Coopy::TableText.new(rows)
  cell = ::Coopy::CellInfo.new
  corner = tt.get_cell_text(0,0)
  off = nil
  if corner == "@:@" 
    off = 1
  else 
    off = 0
  end
  if off > 0 
    return if rows.get_width <= 1 || rows.get_height <= 1
  end
  begin
    _g1 = 0
    _g = rows.get_height
    while(_g1 < _g) 
      row = _g1
      _g1+=1
      open = false
      txt = tt.get_cell_text(off,row)
      txt = "" if txt == nil
      ::Coopy::DiffRender.examine_cell(0,row,txt,"",txt,corner,cell)
      row_mode = cell.category
      change_row = row if row_mode == "spec"
      render.begin_row(row_mode)
      begin
        _g3 = 0
        _g2 = rows.get_width
        while(_g3 < _g2) 
          c = _g3
          _g3+=1
          ::Coopy::DiffRender.examine_cell(c,row,tt.get_cell_text(c,row),((change_row >= 0) ? tt.get_cell_text(c,change_row) : ""),txt,corner,cell)
          render.insert_cell(((@pretty_arrows) ? cell.pretty_value : cell.value),cell.category_given_tr)
        end
      end
      render.end_row
    end
  end
  render.end_table
end

#sample_cssObject



124
125
126
# File 'lib/lib/coopy/diff_render.rb', line 124

def sample_css 
  return ".highlighter .add { \n  background-color: #7fff7f;\n}\n\n.highlighter .remove { \n  background-color: #ff7f7f;\n}\n\n.highlighter td.modify { \n  background-color: #7f7fff;\n}\n\n.highlighter td.conflict { \n  background-color: #f00;\n}\n\n.highlighter .spec { \n  background-color: #aaa;\n}\n\n.highlighter .move { \n  background-color: #ffa;\n}\n\n.highlighter .null { \n  color: #888;\n}\n\n.highlighter table { \n  border-collapse:collapse;\n}\n\n.highlighter td, .highlighter th {\n  border: 1px solid #2D4068;\n  padding: 3px 7px 2px;\n}\n\n.highlighter th, .highlighter .header { \n  background-color: #aaf;\n  font-weight: bold;\n  padding-bottom: 4px;\n  padding-top: 5px;\n  text-align:left;\n}\n\n.highlighter tr:first-child td {\n  border-top: 1px solid #2D4068;\n}\n\n.highlighter td:first-child { \n  border-left: 1px solid #2D4068;\n}\n\n.highlighter td {\n  empty-cells: show;\n}\n"
end

#to_sObject



74
75
76
# File 'lib/lib/coopy/diff_render.rb', line 74

def to_s 
  return self.html
end

#use_pretty_arrows(flag) ⇒ Object



23
24
25
# File 'lib/lib/coopy/diff_render.rb', line 23

def use_pretty_arrows(flag)
  @pretty_arrows = flag
end