Class: Redmine::Helpers::Diff

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::OutputSafetyHelper, 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.



31
32
33
34
35
36
37
# File 'lib/redmine/helpers/diff.rb', line 31

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.



29
30
31
# File 'lib/redmine/helpers/diff.rb', line 29

def diff
  @diff
end

#wordsObject (readonly)

Returns the value of attribute words.



29
30
31
# File 'lib/redmine/helpers/diff.rb', line 29

def words
  @words
end

Instance Method Details

#to_htmlObject



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
67
68
69
70
71
72
73
74
75
76
# File 'lib/redmine/helpers/diff.rb', line 39

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 << ' ' unless deleted.empty?
        deleted << change[2]
        words_del  += 1
      end
    end
    if add_at
      words[add_at] = '<span class="diff_in">'.html_safe + words[add_at]
      words[add_to] = words[add_to] + '</span>'.html_safe
    end
    if del_at
      # deleted is not safe html at this point
      words.insert del_at - del_off + dels + words_add, '<span class="diff_out">'.html_safe + h(deleted) + '</span>'.html_safe
      dels += 1
      del_off += words_del
      words_del = 0
    end
  end
  safe_join(words, ' ')
end