4

What is the best way to check for changes (edited/added/deleted text) in a post between two post's versions (original and edited one)?

I am using Markdown so I am not sure if using difflib.HtmlDiff is a good idea. My goal is to mark with a green background the added text and with a red background the deleted ones, something similar like what github does.

Pompeyo
  • 1,319
  • 3
  • 17
  • 40

3 Answers3

5

Try out ghdiff, GitHub style HTML for unified diffs. It's on PyPI, but there are better docs on GitHub currently. You'll need to pip install ghdiff.

Simple usage, assuming the markdown is in markdown1 and markdown2.

import ghdiff
diff_as_html = ghdiff.diff(markdown1,markdown2)

Here's a more explicit demonstration though. Let's say you have these two markdown strings:

md1 = '''
# Hello world

I am text

* No really

'''

md2 = '''
# Hi world

I am text

* No really

'''

We can perform a diff on these

import ghdiff
diff_as_html = ghdiff.diff(md1,md2)

When rendered, it looks like this:

ghdiff

Here's the raw and the rendered in an IPython Notebook:

raw and rendered in IPython notebook

Kyle Kelley
  • 12,557
  • 4
  • 45
  • 75
  • 2
    Thanks, applied to [my solution](https://github.com/ctrl-alt-d/fpuf/blob/master/fpuf/apps/material/business_rules/material.py#L151) sample here: http://uf.ctrl-alt-d.net/material/revisions/16/ – dani herrera Sep 12 '13 at 22:20
  • Also, reading project code, it is not so far from my dirty solution ;) – dani herrera Sep 12 '13 at 22:25
  • @danihp - That's funny. Yeah, difflib (at least in Python 2.7) isn't very featureful. I've used [get_close_matches](http://docs.python.org/2/library/difflib.html#difflib.get%5Fclose%5Fmatches) for some interesting scraper logic, but nothing as fancy as what's needed for beautified diffs. – Kyle Kelley Sep 13 '13 at 01:28
0

Try DiffMerge:

http://www.sourcegear.com/diffmerge/

It works great!

enter image description here

Mingyu
  • 26,145
  • 13
  • 50
  • 58
0

I use this dirty code wrote by myself in few minutes as first approach. I also compare markdown itself.

I'm also looking for a best way.

def canvis_html( before, after ):
    d = ''.join(
                  ndiff(
                    before.splitlines(1),
                    after.splitlines(1)
                        )
                     )

    estils = {'+':'color:green;', '-':'color:red;text-decoration:line-through;',}
    new = u""
    previous_zone = 'new'    
    for l in d.splitlines(1):
        l=l+u"  "
        zona = l[0]
        if zona in ( '+', '-'):
            if zona != previous_zone:
                #si havia obert una zona la tanco
                if previous_zone != 'new': new += u"</span>"
                #colorejo segons la zona  
                new += u"<span style='{estil};'>".format( estil = estils[zona])
                previous_zone = zona
            new += l[1:] 
        elif l.startswith( '?' ):
            pass
        else:
            new += l[1:]

    return new

Here you can see edit results, as you can see, it is just a starting point if you should code yourself.

dani herrera
  • 39,746
  • 4
  • 87
  • 153