Class: DamageControl::DiffHtmlizer

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

Overview

Visitor that can visit an array of diffs and produce nice HTML TODO: add line numbers.

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ DiffHtmlizer

Creates a new DiffHtmlizer that will write HTML to the IO object io when visiting an array of diffs.



8
9
10
# File 'lib/damagecontrol/diff_htmlizer.rb', line 8

def initialize(io)
  @io = io
end

Instance Method Details

#visitDiff(diff) ⇒ Object



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

def visitDiff(diff)
  @io << "<div>\n"
end

#visitDiffEnd(diff) ⇒ Object



16
17
18
# File 'lib/damagecontrol/diff_htmlizer.rb', line 16

def visitDiffEnd(diff)
  @io << "</div>\n"
end

#visitLine(line) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/damagecontrol/diff_htmlizer.rb', line 20

def visitLine(line)
  if(line.removed?)
    @io << "<pre class='diff' id='removed'>"
    if(line.removed)
      @io << line.prefix.html_encoded
      @io << "<span id='removedchars'>"
      @io << line.removed.html_encoded
      @io << "</span>"
      @io << line.suffix.html_encoded
    else
      @io << line.html_encoded
    end
    @io << "</pre>"
  elsif(line.added?)
    @io << "<pre class='diff' id='added'>"
    if(line.added)
      @io << line.prefix.html_encoded
      @io << "<span id='addedchars'>"
      @io << line.added.html_encoded
      @io << "</span>"
      @io << line.suffix.html_encoded
    else
      @io << line.html_encoded
    end
    @io << "</pre>"
  else
    @io << "<pre class='diff' id='context'>"
    @io << line.html_encoded
    @io << "</pre>"
  end
end