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

#sectionObject

Returns the value of attribute section.



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

def section
  @section
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, view, raw, vcol, vrow, vcorner, cell, offset = 0) ⇒ Object



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
211
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/lib/coopy/diff_render.rb', line 184

def DiffRender.examine_cell(x,y,view,raw,vcol,vrow,vcorner,cell,offset = 0)
  nested = view.is_hash(raw)
  cell.category = ""
  cell.category_given_tr = ""
  cell.separator = ""
  cell.pretty_separator = ""
  cell.conflicted = false
  cell.updated = false
  cell.meta = cell.pvalue = cell.lvalue = cell.rvalue = nil
  cell.value = raw
  cell.pretty_value = cell.value
  vrow = "" if vrow == nil
  vcol = "" if vcol == nil
  if vrow.length >= 3 && vrow[0] == "@" && vrow[1] != "@" 
    idx = vrow.index("@",1 || 0) || -1
    if idx >= 0 
      cell.meta = vrow[1,idx - 1]
      vrow = vrow[idx + 1,vrow.length]
      cell.category = "meta"
    end
  end
  removed_column = false
  cell.category = "move" if vrow == ":"
  cell.category = "index" if vrow == "" && offset == 1 && y == 0
  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 = "gap"
  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
      str = view.to_s(cell.value)
      str = "" if str == nil
      if nested || (str.index(part,nil || 0) || -1) >= 0 
        cat = "modify"
        div = part
        if part != full 
          if nested 
            cell.conflicted = view.hash_exists(raw,"theirs")
          else 
            cell.conflicted = (str.index(full,nil || 0) || -1) >= 0
          end
          if cell.conflicted 
            div = full
            cat = "conflict"
          end
        end
        cell.updated = true
        cell.separator = div
        cell.pretty_separator = div
        if nested 
          if cell.conflicted 
            tokens = [view.hash_get(raw,"before"),view.hash_get(raw,"ours"),view.hash_get(raw,"theirs")]
          else 
            tokens = [view.hash_get(raw,"before"),view.hash_get(raw,"after")]
          end
        else 
          cell.pretty_value = view.to_s(cell.pretty_value)
          cell.pretty_value = "" if cell.pretty_value == nil
          if cell.pretty_value == div 
            tokens = ["",""]
          else 
            tokens = cell.pretty_value.split(div)
          end
        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_separator = [8594].pack("U")
        cell.pretty_value = pretty_tokens.join(cell.pretty_separator)
        cell.category_given_tr = cell.category = cat
        offset1 = nil
        if cell.conflicted 
          offset1 = 1
        else 
          offset1 = 0
        end
        cell.lvalue = tokens[offset1]
        cell.rvalue = tokens[offset1 + 1]
        cell.pvalue = tokens[0] if cell.conflicted
      end
    end
  end
  cell.category_given_tr = cell.category = "index" if x == 0 && offset > 0
end

.mark_spaces(sl, sr) ⇒ Object

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



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/lib/coopy/diff_render.rb', line 294

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
  slo
end

.render_cell(tab, view, x, y) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/lib/coopy/diff_render.rb', line 323

def DiffRender.render_cell(tab,view,x,y)
  cell = ::Coopy::CellInfo.new
  corner = view.to_s(tab.get_cell(0,0))
  off = nil
  if corner == "@:@" 
    off = 1
  else 
    off = 0
  end
  ::Coopy::DiffRender.examine_cell(x,y,view,tab.get_cell(x,y),view.to_s(tab.get_cell(x,off)),view.to_s(tab.get_cell(off,y)),corner,cell,off)
  cell
end

Instance Method Details

#begin_row(mode) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lib/coopy/diff_render.rb', line 54

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

#begin_tableObject



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

def begin_table 
  self.insert("<table>\n")
  @section = nil
end

#complete_htmlObject



177
178
179
180
181
182
# File 'lib/lib/coopy/diff_render.rb', line 177

def complete_html 
  @text_to_insert.insert(0,"<!DOCTYPE html>\n<html>\n<head>\n<meta charset='utf-8'>\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



80
81
82
# File 'lib/lib/coopy/diff_render.rb', line 80

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

#end_tableObject



84
85
86
87
# File 'lib/lib/coopy/diff_render.rb', line 84

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

#htmlObject



91
92
93
# File 'lib/lib/coopy/diff_render.rb', line 91

def html 
  @text_to_insert.join("")
end

#insert(str) ⇒ Object

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



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

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

#insert_cell(txt, mode) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lib/coopy/diff_render.rb', line 68

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) + ">")
  if txt != nil 
    self.insert(txt)
  else 
    self.insert("null")
  end
  self.insert(@td_close)
end

#render(tab) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/lib/coopy/diff_render.rb', line 99

def render(tab)
  tab = ::Coopy::Coopy.tablify(tab)
  return self if tab.get_width == 0 || tab.get_height == 0
  render = self
  render.begin_table
  change_row = -1
  cell = ::Coopy::CellInfo.new
  view = tab.get_cell_view
  corner = view.to_s(tab.get_cell(0,0))
  off = nil
  if corner == "@:@" 
    off = 1
  else 
    off = 0
  end
  if off > 0 
    return self if tab.get_width <= 1 || tab.get_height <= 1
  end
  begin
    _g1 = 0
    _g = tab.get_height
    while(_g1 < _g) 
      row = _g1
      _g1+=1
      open = false
      txt = view.to_s(tab.get_cell(off,row))
      txt = "" if txt == nil
      ::Coopy::DiffRender.examine_cell(off,row,view,txt,"",txt,corner,cell,off)
      row_mode = cell.category
      change_row = row if row_mode == "spec"
      if row_mode == "header" || row_mode == "spec" || row_mode == "index" || row_mode == "meta" 
        self.set_section("head")
      else 
        self.set_section("body")
      end
      render.begin_row(row_mode)
      begin
        _g3 = 0
        _g2 = tab.get_width
        while(_g3 < _g2) 
          c = _g3
          _g3+=1
          ::Coopy::DiffRender.examine_cell(c,row,view,tab.get_cell(c,row),((change_row >= 0) ? view.to_s(tab.get_cell(c,change_row)) : ""),txt,corner,cell,off)
          render.insert_cell(((@pretty_arrows) ? cell.pretty_value : cell.value),cell.category_given_tr)
        end
      end
      render.end_row
    end
  end
  render.end_table
  self
end

#render_tables(tabs) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/lib/coopy/diff_render.rb', line 152

def render_tables(tabs)
  order = tabs.get_order
  self.render(tabs.one) if order.length == 0 || tabs.has_ins_del
  begin
    _g1 = 1
    _g = order.length
    while(_g1 < _g) 
      i = _g1
      _g1+=1
      name = order[i]
      tab = tabs.get(name)
      next if tab.get_height <= 1
      self.insert("<h3>")
      self.insert(name)
      self.insert("</h3>\n")
      self.render(tab)
    end
  end
  self
end

#sample_cssObject



173
174
175
# File 'lib/lib/coopy/diff_render.rb', line 173

def sample_css 
  ".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, .highlighter .meta {\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.header th {\n  border-bottom: 2px solid black;\n}\n\n.highlighter tr.index td, .highlighter .index, .highlighter tr.header th.index {\n  background-color: white;\n  border: none;\n}\n\n.highlighter .gap {\n  color: #888;\n}\n\n.highlighter td {\n  empty-cells: show;\n}\n"
end

#set_section(str) ⇒ Object



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

def set_section(str)
  return if str == @section
  if @section != nil 
    self.insert("</t")
    self.insert(@section)
    self.insert(">\n")
  end
  @section = str
  if @section != nil 
    self.insert("<t")
    self.insert(@section)
    self.insert(">\n")
  end
end

#to_sObject



95
96
97
# File 'lib/lib/coopy/diff_render.rb', line 95

def to_s 
  self.html
end

#use_pretty_arrows(flag) ⇒ Object



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

def use_pretty_arrows(flag)
  @pretty_arrows = flag
end