Sha256: 84f75411ececa38c4f6f023abd101bbaf0383fcf1922aa58b9838ba7e789df35

Contents?: true

Size: 1.81 KB

Versions: 5

Compression:

Stored size: 1.81 KB

Contents

module DamageControl

  # Visitor that can visit an array of diffs and produce nice HTML
  # TODO: add line numbers.
  class DiffHtmlizer
    # Creates a new DiffHtmlizer that will write HTML
    # to the IO object +io+ when visiting an array of diffs.
    def initialize(io)
      @io = io
    end
    
    def visitDiff(diff)
      @io << "<div>\n"
    end

    def visitDiffEnd(diff)
      @io << "</div>\n"
    end
    
    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
  end

  # Not used
  class Plain
    def initialize(io)
      @io = io
    end
    
    def visitDiff(diff)
    end

    def visitDiffEnd(diff)
    end
    
    def visitLine(line)
      @io << line.html_encoded
    end
  end
end

class String
  def html_encoded
    self.gsub(/./) do
      case $&
        when "&" then "&amp;"
        when "<" then "&lt;"
        when ">" then "&gt;"
        else $&
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
damagecontrol-0.5.0.1393 lib/damagecontrol/diff_htmlizer.rb
damagecontrol-0.5.0.1391 lib/damagecontrol/diff_htmlizer.rb
damagecontrol-0.5.0.1392 lib/damagecontrol/diff_htmlizer.rb
damagecontrol-0.5.0 lib/damagecontrol/diff_htmlizer.rb
damagecontrol-0.5.0.1404 lib/damagecontrol/diff_htmlizer.rb