简体   繁体   中英

How to compare two models to be rendered with Markdown using Django?

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.

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:

原始并在IPython笔记本中呈现

Try DiffMerge:

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

It works great!

在此输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM