pvande / differ Goto Github PK
View Code? Open in Web Editor NEWA simple gem for generating string diffs
A simple gem for generating string diffs
I don't know how to put it, but to be selfish, here's what i want and need to do:
I grab an html page with net/http, and i save it to the Database.
I then compare this page to another page i grabbed, say yesterday.
I only want the changes between the two pages to appear.
Is this gem right for that? I can see it noticing some changes in the shit i'm looking at, but I don't know how to get it out and use it. i can see something like this gist https://gist.github.com/caf0aff6c57f9ca011a9
I mean i see {+ where it should be, which is all i want to see printed out.
thoughts?
:html
formatter passes <
symbols as is to resulting html, which may result in XSS vulnerability.
[2] pry(main)> Differ.diff_by_line("foo", "<script>alert(1)</script>").format_as(:html)
=> "<del class=\"differ\"><script>alert(1)</script></del><ins class=\"differ\">foo</ins>"
As Differ operates on strings, not HTML fragments, IMHO, it should escape chunks of these strings in resulting html.
Using differ 0.1.2.
Hello sir. Please take a look at this:
The outputt:
How it looks when you write this content to a file:
The problem, found in ascii.rb:
The inspect is the problem. Is fine with none unicode strings, but when unicode appear it mess. The workaround we made:
How the file where string was written looks:
If this is okay for you, I can make a pull request, I'm not sure if fills expectations, probably it could be improved. Cheers.
I tested the following two strings:
@original = "The second aspect is that graphic design is a very inclusive subject, and enables me to explore various art fields. For instance, if I am going to create a poster, I could create it solely with a computer, or I can create a sculpture and then take a photo of it, or I can learn photography and create a photograph for it, or I can use traditional ways like drawing or painting, and so forth. The numerous possibilities and great potential of graphic design attract me as well."
@current = "The first aspect is that graphic design is a very inclusive subject. It enables me to explore various art fields, and incorporate a mixture of media in my works. For instance, if I am going to create a poster, I could create it solely by computer, or I can create a sculpture and then take a photo of it, then cut it and edit it digitally. I can learn photography and create a photograph for it. I can even use traditional methods like drawing or painting, and mix it with digital work. The numerous possibilities and great potential of graphic design attract me to this exciting art field."
Your Gem handled the first few phrases well, but then chunked together the remaining part. I've tried many different plugins and gems so far for this, and so far, the best algorithm for comparison is the JS plugin jsdiff. Perhaps you can incorporate some stuff from their algorithm for this gem? I would love it if a ruby gem can take care of this all.
jsdiff: github url
live demo
jsdiff was able to handle very well the complexity of large text revision (by_word).
Ruby 2.7.0 gives a deprecation warning:
vendor/bundle/ruby/2.7.0/gems/differ-0.1.2/lib/differ.rb:11: warning: `$;' is deprecated
Inline diff's, dont support by_word
. Only by line? For example:
a.diff_by_word(b).format_as(:html).html_safe
Something is wrong when there is bold text (text surrounded by the <strong>
element). Take for example:
a = "<p>adfadf<br>\n222<br>\nfoo <strong>bar</strong> was <code>here</code> 333<br>\nhere too<br>\n444<br>\n55 <strong>bold text added</strong><br>\n66</p>\n"
b = "<p>adfadf<br>\n222<br>\nfoo <strong>bar</strong> was <code>here</code> 333<br>\nhere too<br>\n444<br>\n55<br>\n66</p>\n"
Differ.diff_by_word(a, b).format_as(:html).html_safe
The following renders as:
<p>adfadf<br>
222<br>
foo <strong>bar</strong> was <code>here</code> 333<br>
here too<br>
444<br>
55<del class="differ"><</del><ins class="differ"> <strong>bold text added</strong><</ins>br>
66</p>
Notice the <
characters. Is this normal? Which renders in the browser inappropriately...
The difference between two strings is
Original -- Hello
Revision -- <strong>Hello</strong>
using diff_by_word and format :html
Result posted below.
Technically, Differ is doing it correctly. The delta change is the <strong> </strong>
tags.
What I would like to figure out is how to change the output
from:
In the
to:
In the
Any ideas of where to attack this would be appreciated. It is an awesome gem, just this one problem is driving me batty
thanks
zonker
DIFF:
<ins class="differ"><strong></ins>In<ins class="differ"></strong> the</ins>
<ins class="differ">early 1900s, Freud and a <strong>growing</strong> number
of followers formed a psychoanalytic movement. Carl Gustav Jung
(1875–1961) and Alfred Adler (1870–1937) were prominent in
</ins>the <del class="differ">early 1900s, Freud and a growing number of
followers formed a psychoanalytic </del>movement<del class="differ">. Carl
Gustav Jung (1875–1961) and Alfred Adler (1870–1937) were
prominent in the movement</del>, but both were independent thinkers, and Freud
apparently had little tolerance for individuals who challenged his ideas.
NEW:
<strong>In</strong> the early 1900s, Freud and a <strong>growing</strong>
number of followers formed a psychoanalytic movement. Carl Gustav Jung
(1875–1961) and Alfred Adler (1870–1937) were prominent in
the movement, but both were independent thinkers, and Freud apparently had
little tolerance for individuals who challenged his ideas.
ORIGINAL:
In the early 1900s, Freud and a growing number of followers formed a
psychoanalytic movement. Carl Gustav Jung (1875–1961) and
Alfred Adler (1870–1937) were prominent in the movement, but
both were independent thinkers, and Freud apparently had little tolerance
for individuals who challenged his ideas.
(Edited for formatting and clarity -- pvande)
@pvande Your Differ project is awesome, but it's sad to see that it's no being actively maintained. We @forgecrafted would love to pick up the torch and carry on your excellent ground work.
Would you be willing to make a maintainer transfer?
Hi.
Could you remove "protected" from raw_array? I need the diff output in machine readable format (rather than display to user), and raw_array seems the most usable format. My use case is that I'm developing a text editor (https://github.com/SamiSieranoja/vimamsa) and I want to process the buffer contents with external program and then include the diffs of before/after in undo history.
diff --git a/lib/differ/diff.rb b/lib/differ/diff.rb
index 76d3012..5f46cb6 100644
--- a/lib/differ/diff.rb
+++ b/lib/differ/diff.rb
@@ -82,7 +82,7 @@ module Differ
end
end
- protected
+# protected
def raw_array
@raw
end
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.