Class: RevisionSan::Diff::SimpleHTMLFormatter::LineDiffHTMLFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/revision_san/diff.rb

Direct Known Subclasses

WordDiffHTMLFormatter

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ LineDiffHTMLFormatter

Returns a new instance of LineDiffHTMLFormatter.



60
61
62
63
64
# File 'lib/revision_san/diff.rb', line 60

def initialize(from, to)
  @output = []
  @went_deep = false
  ::Diff::LCS.traverse_sequences(from, to, self)
end

Instance Method Details

#discard_a(change) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/revision_san/diff.rb', line 66

def discard_a(change)
  from_words, to_words = [change.old_element, change.new_element].map { |text| text.to_s.scan(/\w+|\W|\s/) }
  changes = ::Diff::LCS.diff(from_words, to_words).length
  if changes > 1 && changes > (from_words.length / 5)
    @output << "<del>#{change.old_element}</del>"
  else
    @output << WordDiffHTMLFormatter.new(from_words, to_words).output
    @went_deep = true
  end
end

#discard_b(change) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/revision_san/diff.rb', line 77

def discard_b(change)
  if @went_deep
    @went_deep = false
  else
    @output << "<ins>#{change.new_element}</ins>"
  end
end

#match(match) ⇒ Object



85
86
87
# File 'lib/revision_san/diff.rb', line 85

def match(match)
  @output << match.new_element
end

#outputObject



56
57
58
# File 'lib/revision_san/diff.rb', line 56

def output
  @output.join.gsub(%r{</ins><ins>|</del><del>|<del></del>}, '')
end