Class: DiffToHtml::Converter

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

Direct Known Subclasses

GitConverter, SvnConverter

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_prefixObject

Returns the value of attribute file_prefix.


6
7
8
# File 'lib/diff_to_html/converter.rb', line 6

def file_prefix
  @file_prefix
end

Instance Method Details

#composite_to_html(composite_diff) ⇒ Object


8
9
10
# File 'lib/diff_to_html/converter.rb', line 8

def composite_to_html(composite_diff)
  diffs_to_html get_diffs(composite_diff)
end

#diffs_to_html(diffs) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/diff_to_html/converter.rb', line 12

def diffs_to_html(diffs)
  result = '<ul class="diff">'
  @filenum = 0
  
  diffs.each do |file_map|
    result << get_single_file_diff(file_map[:filename], file_map[:file])
    @filenum += 1
  end
  
  result << '</ul>'
  result    
end

#file_header_patternObject


106
107
108
# File 'lib/diff_to_html/converter.rb', line 106

def file_header_pattern
  raise "Method to be implemented in VCS-specific class"
end

#get_single_file_diff(file_name, diff_file) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/diff_to_html/converter.rb', line 25

def get_single_file_diff(file_name, diff_file)
  result = ""
  diff = diff_file.split("\n")
  diff, line = shift_until_first_line(diff)
  
  if line =~ /^---/
    result << begin_file(file_name)
    result << get_single_file_diff_body(diff)
    result << "</li>"      
  else
    #"<div class='error'>#{line}</div>"
    result =%Q{<li><h2><a name="F#{@filenum}" href="#F#{@filenum}">#{file_name}</a></h2>#{line}</li>}
  end
  
  result
end

#get_single_file_diff_body(diff) ⇒ Object


42
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
# File 'lib/diff_to_html/converter.rb', line 42

def get_single_file_diff_body(diff)
  @last_op, @left, @right = ' ', [], []
  
  if diff.is_a? String
    diff = diff.split("\n") 
    diff, line = shift_until_first_line(diff)
  end
  
  diff.shift #+++
  
  result = %Q{
    <table class='diff'>
      <colgroup>
      <col class="lineno"/>
      <col class="lineno"/>
      <col class="content"/>
    </colgroup>   
  }
  
  range = diff.shift
  left_ln, right_ln = range_info(range)
  result << range_row(range)
  
  diff.each do |line|
    op = line[0,1]
    line = line[1..-1] || ''
    
    if op == '\\'
      line = op + line
      op = ' '
    end
    
    if ((@last_op != ' ' and op == ' ') or (@last_op == ' ' and op != ' '))
      left_ln, right_ln = flush_changes(result, left_ln, right_ln)
    end
    
    # truncate and escape
    line = CGI.escapeHTML(line)

    case op
    when ' '
      @left.push(line)
      @right.push(line)
    when '-' then @left.push(line)
    when '+' then @right.push(line)
    when '@' 
      range = '@' + line
      flush_changes(result, left_ln, right_ln)
      left_ln, right_ln = range_info(range)
      result << range_row(range)
    else
      flush_changes(result, left_ln, right_ln)
      result << "</table></li>"
      break
    end
    @last_op = op
  end

  flush_changes(result, left_ln, right_ln)
  result << "</table>" 
  
  result
end