Class: Redmine::Helpers::Diff
- Inherits:
-
Object
- Object
- Redmine::Helpers::Diff
- Includes:
- ActionView::Helpers::OutputSafetyHelper, ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper, ERB::Util
- Defined in:
- lib/redmine/helpers/diff.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#diff ⇒ Object
readonly
Returns the value of attribute diff.
-
#words ⇒ Object
readonly
Returns the value of attribute words.
Instance Method Summary collapse
-
#initialize(content_to, content_from) ⇒ Diff
constructor
A new instance of Diff.
- #to_html ⇒ Object
Constructor Details
#initialize(content_to, content_from) ⇒ Diff
Returns a new instance of Diff
29 30 31 32 33 34 35 |
# File 'lib/redmine/helpers/diff.rb', line 29 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
#diff ⇒ Object (readonly)
Returns the value of attribute diff
27 28 29 |
# File 'lib/redmine/helpers/diff.rb', line 27 def diff @diff end |
#words ⇒ Object (readonly)
Returns the value of attribute words
27 28 29 |
# File 'lib/redmine/helpers/diff.rb', line 27 def words @words end |
Instance Method Details
#to_html ⇒ Object
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 67 68 69 70 71 72 73 74 |
# File 'lib/redmine/helpers/diff.rb', line 37 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 |