Class: Redmine::Helpers::Diff

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper, ERB::Util
Defined in:
lib/redmine/helpers/diff.rb

Direct Known Subclasses

WikiDiff

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_to, content_from) ⇒ Diff

Returns a new instance of Diff.


23
24
25
26
27
28
29
# File 'lib/redmine/helpers/diff.rb', line 23

def initialize(content_to, content_from)
  @words = content_to.to_s.split(/(\s+)/)
  @words = @words.select {|word| word != ' '}
  words_from = content_from.to_s.split(/(\s+)/)
  words_from = words_from.select {|word| word != ' '}
  @diff = words_from.diff @words
end

Instance Attribute Details

#diffObject (readonly)

Returns the value of attribute diff


21
22
23
# File 'lib/redmine/helpers/diff.rb', line 21

def diff
  @diff
end

#wordsObject (readonly)

Returns the value of attribute words


21
22
23
# File 'lib/redmine/helpers/diff.rb', line 21

def words
  @words
end

Instance Method Details

#to_htmlObject


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/redmine/helpers/diff.rb', line 31

def to_html
  words = self.words.collect{|word| h(word)}
  words_add = 0
  words_del = 0
  dels = 0
  del_off = 0
  diff.diffs.each do |diff|
    add_at = nil
    add_to = nil
    del_at = nil
    deleted = ""
    diff.each do |change|
      pos = change[1]
      if change[0] == "+"
        add_at = pos + dels unless add_at
        add_to = pos + dels
        words_add += 1
      else
        del_at = pos unless del_at
        deleted << ' ' + h(change[2])
        words_del  += 1
      end
    end
    if add_at
      words[add_at] = '<span class="diff_in">' + words[add_at]
      words[add_to] = words[add_to] + '</span>'
    end
    if del_at
      words.insert del_at - del_off + dels + words_add, '<span class="diff_out">' + deleted + '</span>'
      dels += 1
      del_off += words_del
      words_del = 0
    end
  end
  words.join(' ')
end