Class: DiffRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/diffrenderer.rb,
lib/diffrenderer/paragraph_block.rb

Defined Under Namespace

Classes: ParagraphBlock

Constant Summary collapse

UNWANTED_TAGS =
%w[ a ul ol ].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paragraph_list_a, paragraph_list_b) ⇒ DiffRenderer

Returns a new instance of DiffRenderer.



17
18
19
# File 'lib/diffrenderer.rb', line 17

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

Class Method Details

.process_html(h) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/diffrenderer.rb', line 26

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

Instance Method Details

#to_htmlObject



21
22
23
24
# File 'lib/diffrenderer.rb', line 21

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