discourse/lib/diff_engine.rb

26 lines
973 B
Ruby
Raw Normal View History

require 'diffy'
2013-02-25 10:42:20 -06:00
# This class is used to generate diffs, it will be consumed by the UI on
2013-02-22 23:30:02 -06:00
# on the client the displays diffs.
#
2013-03-15 19:31:51 -05:00
# There are potential performance issues associated with diffing large amounts of completely
# different text, see answer here for optimization if needed
# http://meta.stackoverflow.com/questions/127497/suggested-edit-diff-shows-different-results-depending-upon-mode
2013-02-22 23:30:02 -06:00
class DiffEngine
2013-02-25 10:42:20 -06:00
2013-03-15 19:31:51 -05:00
# generate an html friendly diff similar to the way Stack Exchange generates
# html diffs
2013-02-22 23:30:02 -06:00
#
2013-03-06 01:52:24 -06:00
# returns: html containing decorations indicating the changes
2013-02-22 23:30:02 -06:00
def self.html_diff(html_before, html_after)
2013-07-23 13:42:52 -05:00
Diffy::Diff.new(html_before, html_after, {allow_empty_diff: false}).to_s(:html)
2013-02-22 23:30:02 -06:00
end
# same as html diff, except that it operates on markdown
#
# returns html containing decorated areas where diff happened
def self.markdown_diff(markdown_before, markdown_after)
2013-03-15 19:31:51 -05:00
Diffy::Diff.new(markdown_before, markdown_after).to_s(:html)
2013-02-22 23:30:02 -06:00
end
end