forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Diffy as DiffEngine implementation
- Loading branch information
1 parent
19860bd
commit 204dcc4
Showing
5 changed files
with
74 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
# This class is used to generate diffs, it will be consumed by the UI on | ||
# on the client the displays diffs. | ||
# | ||
# Ruby has the diff/lcs engine that can do some of the work, the devil | ||
# is in the details | ||
# 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 | ||
|
||
class DiffEngine | ||
|
||
# generate an html friendly diff similar to the way Stack Exchange generate | ||
# html diffs | ||
# generate an html friendly diff similar to the way Stack Exchange generates | ||
# html diffs | ||
# | ||
# returns: html containing decorations indicating the changes | ||
def self.html_diff(html_before, html_after) | ||
Diffy::Diff.new(html_before, html_after).to_s(:html) | ||
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) | ||
Diffy::Diff.new(markdown_before, markdown_after).to_s(:html) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'spec_helper' | ||
require 'diff_engine' | ||
|
||
describe DiffEngine do | ||
|
||
let(:html_before) do | ||
<<-HTML.strip_heredoc | ||
<context> | ||
<original>text</original> | ||
</context> | ||
HTML | ||
end | ||
|
||
let(:markdown_special_characters) do | ||
"=\`*_{}[]()#+-.!" | ||
end | ||
|
||
it "escapes input html to markup with diff html" do | ||
diff = DiffEngine.html_diff("<html>", "") | ||
|
||
diff.should include("<html>") | ||
end | ||
|
||
it "generates an html diff with ins and dels for changed" do | ||
html_after = html_before | ||
.gsub(/original/, "changed") | ||
|
||
diff = DiffEngine.html_diff(html_before, html_after) | ||
|
||
diff.should match(/del.*?original.*?del/) | ||
diff.should match(/ins.*?changed.*?ins/) | ||
end | ||
|
||
it "generates an html diff with only ins for inserted" do | ||
html_after = "#{html_before}\nnew" | ||
|
||
diff = DiffEngine.html_diff(html_before, html_after) | ||
|
||
diff.should include("ins") | ||
diff.should_not include("del") | ||
end | ||
|
||
it "generates an html diff with only unchanged for unchanged" do | ||
html_after = html_before | ||
|
||
diff = DiffEngine.html_diff(html_before, html_after) | ||
|
||
diff.should include("unchanged") | ||
diff.should_not include("del", "ins") | ||
end | ||
|
||
it "handles markdown special characters" do | ||
diff = DiffEngine.markdown_diff(markdown_special_characters, "") | ||
|
||
diff.should include(markdown_special_characters) | ||
end | ||
|
||
end |