Sha256: 8aa32b55fd2e6ddb6d07e7916ca52421ff391b70a5e94145624369bb4332a83f

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

require 'rubygems'
gem 'diff-lcs', '~> 1.1', '>= 1.1.2'
require 'diff/lcs'
require 'hpricot'
require 'pp'

require 'word_run_diff_lcs'

%w(paragraph_block).each do |file|
  require File.join(File.dirname(__FILE__), 'diffrenderer', file)
end

class DiffRenderer

  UNWANTED_TAGS = %w[ a ul ol ].freeze

  def initialize(paragraph_list_a, paragraph_list_b)
    @paragraph_list_a, @paragraph_list_b = paragraph_list_a, paragraph_list_b
  end

  def to_html
    diff = Diff::LCS.sdiff(@paragraph_list_a,@paragraph_list_b)
    diff.map { |context_change| ParagraphBlock.new(context_change) }.join
  end

  def self.process_html(h)
    doc = Hpricot(h)

    UNWANTED_TAGS.each { |tag| (doc / tag).remove }
    (doc / "p").map { |elem|
      # Silence "nested" <p> tags as Hpricot does not implicitly close <p> tags
      (elem / "p").each do |e|
        class << e; def inner_text; nil; end; end
      end

      text = elem.inner_text.strip

      (elem / "p").each do |e|
        class << e; remove_method :inner_text; end
      end
      text
    }.reject { |text|
      text == ""
    }
  end

end


Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
monkeyhelper-diffrenderer-0.0.0 lib/diffrenderer.rb
diffrenderer-0.0.3 lib/diffrenderer.rb