-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change file name and use module to organize
- Loading branch information
Showing
4 changed files
with
55 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require "diff/lcs" | ||
|
||
module DiffTools | ||
class SimpleDiff | ||
def initialize(old_value, new_value) | ||
old_value ||= "" | ||
new_value ||= "" | ||
@changes = ::Diff::LCS.sdiff(old_value, new_value).chunk(&:action).map do |action, changes| | ||
{ | ||
action: action, | ||
old: changes.map(&:old_element).join, | ||
new: changes.map(&:new_element).join | ||
} | ||
end | ||
end | ||
|
||
def to_html | ||
@changes.map do |chunk| | ||
old_html = DiffTools.value_to_html(chunk[:old]) | ||
new_html = DiffTools.value_to_html(chunk[:new]) | ||
if chunk[:action] == "=" | ||
new_html | ||
else | ||
(old_html.empty? ? "" : "<del>#{old_html}</del>") + \ | ||
(new_html.empty? ? "" : "<ins>#{new_html}</ins>") | ||
end | ||
end.join | ||
end | ||
end | ||
|
||
def self.value_to_html(value) | ||
too_many_words_re = / | ||
((?:\S+\s+){3}) # group 1: Three words, including trailing space | ||
(.+) # group 2: Drop this | ||
((?:\s+\S+){3}) # group 3: Three words, including leading space | ||
/x | ||
ellipsis = value.gsub(too_many_words_re, '\1...\3') | ||
CGI.escapeHTML(ellipsis) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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