Class: DiffRenderer
- Inherits:
-
Object
- Object
- DiffRenderer
- 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
-
#initialize(paragraph_list_a, paragraph_list_b) ⇒ DiffRenderer
constructor
A new instance of DiffRenderer.
- #to_html ⇒ Object
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_html ⇒ Object
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 |