GithubHelp home page GithubHelp logo

differ's Introduction

Differ

  As streams of text swirled before the young man's eyes, his mind swam with
thoughts of many things. They would have to wait, however, as he focussed his
full concentration on the shifting patterns ahead of him. A glint of light
reflecting off a piece of buried code caught his eye and any hope he had was
lost. For the very moment he glanced aside, the landscape became Different.
  The young man gave a small sigh and trudged onward in solemn resignation,
fated to wander the desolate codebanks in perpetuity.

Differ is a flexible, pure-Ruby diff library, suitable for use in both command line scripts and web applications. The flexibility comes from the fact that diffs can be built at completely arbitrary levels of granularity (some common ones are built-in), and can be output in a variety of formats.

Installation

sudo gem install differ

Usage

There are a number of ways to use Differ, depending on your situation and needs.

@original = "Epic lolcat fail!"
@current  = "Epic wolfman fail!"

You can call the Differ module directly.

require 'differ'

There are a number of built-in diff methods to choose from…

@diff = Differ.diff_by_line(@current, @original)
  # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}"

@diff = Differ.diff_by_word(@current, @original)
  # => "Epic {"lolcat" >> "wolfman"} fail!"

@diff = Differ.diff_by_char(@current, @original)
  # => "Epic {+"wo"}l{-"olcat "}f{+"m"}a{+"n fa"}il!"

… or call #diff directly and supply your own boundary string!

@diff = Differ.diff(@current, @original)  # implicitly by line!
  # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}"

@diff = Differ.diff(@current, @original, 'i')
  # => "Epi{"c lolcat fa" >> "c wolfman fa"}il"

If you would like something a little more inline…

require 'differ/string'

@diff = @current.diff(@original)  # implicitly by line!
  # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}"

… or a lot more inline…

@diff = (@current - @original)    # implicitly by line!
  # => "{"Epic lolcat fail!" >> "Epic wolfman fail!"}"

$; = ' '
@diff = (@current - @original)
  # => "Epic {"lolcat" >> "wolfman"} fail!"

… we’ve pretty much got you covered.

Output Formatting

Need a different output format? We’ve got a few of those too.

Differ.format = :ascii  # <- Default
Differ.format = :color
Differ.format = :html

Differ.format = MyCustomFormatModule

Don’t want to change the system-wide default for only a single diff output? Yeah, me either.

@diff = (@current - @original)
@diff.format_as(:color)

Copyright © 2009 Pieter Vande Bruggen.

(The GIFT License, v1)

Permission is hereby granted to use this software and/or its source code for whatever purpose you should choose. Seriously, go nuts. Use it for your personal RSS feed reader, your wildly profitable social network, or your mission to Mars.

I don’t care, it’s yours. Change the name on it if you want – in fact, if you start significantly changing what it does, I’d rather you did! Make it your own little work of art, complete with a stylish flowing signature in the corner. All I really did was give you the canvas. And my blessing.

Know always right from wrong, and let others see your good works.

differ's People

Contributors

pvande avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

differ's Issues

By_Word not sensitive enough.

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).

Issue when outputting differences in ascii format with unicode chars

Hello sir. Please take a look at this:

image

The outputt:

image

How it looks when you write this content to a file:

image

The problem, found in ascii.rb:

image

The inspect is the problem. Is fine with none unicode strings, but when unicode appear it mess. The workaround we made:

image

How the file where string was written looks:

image

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.

Please remove protected from raw_array

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

Showing only changes?

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?

Something is wrong when there is bold text

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">&lt;</del><ins class="differ"> <strong>bold text added</strong>&lt;</ins>br&gt;
66</p>

Notice the &lt; characters. Is this normal? Which renders in the browser inappropriately...

HTML formatter passes '<' symbol and tags unescaped

: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.

Looking for new project maintainer?

@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?

Differ not formating strings correctly

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&ndash;1961) and Alfred Adler (1870&ndash;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&ndash;1961) and Alfred Adler (1870&ndash;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&ndash;1961) and Alfred Adler (1870&ndash;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&ndash;1961) and
Alfred Adler (1870&ndash;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)

$; is deprecated in Ruby 2.7.0

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.