Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Short python script to php
Old 09-16-2007, 09:47 PM Short python script to php
Junior Talker

Posts: 1
Trades: 0
Hi All. Never registered, but been browsing around in here for a while now.

I've been charged with getting a visual html diff. The closest I've found is a python script. Unfortunately our host will not install mod_python and we have limited control over .ht* files. Could someone familiar with both python and php possibly convert this script? I've been working on this myself but am totally confused and lost...

Code:
import difflib, string

def isTag(x): return x[0] == "<" and x[-1] == ">"

def textDiff(a, b):
    """Takes in strings a and b and returns a human-readable HTML diff."""

    out = []
    a, b = html2list(a), html2list(b)
    s = difflib.SequenceMatcher(None, a, b)
    for e in s.get_opcodes():
        if e[0] == "replace":
            # @@ need to do something more complicated here
            # call textDiff but not for html, but for some html... ugh
            # gonna cop-out for now
            out.append('<del class="diff modified">'+''.join(a[e[1]:e[2]]) + '</del><ins class="diff modified">'+''.join(b[e[3]:e[4]])+"</ins>")
        elif e[0] == "delete":
            out.append('<del class="diff">'+ ''.join(a[e[1]:e[2]]) + "</del>")
        elif e[0] == "insert":
            out.append('<ins class="diff">'+''.join(b[e[3]:e[4]]) + "</ins>")
        elif e[0] == "equal":
            out.append(''.join(b[e[3]:e[4]]))
        else: 
            raise "Um, something's broken. I didn't expect a '" + `e[0]` + "'."
    return ''.join(out)

def html2list(x, b=0):
    mode = 'char'
    cur = ''
    out = []
    for c in x:
        if mode == 'tag':
            if c == '>': 
                if b: cur += ']'
                else: cur += c
                out.append(cur); cur = ''; mode = 'char'
            else: cur += c
        elif mode == 'char':
            if c == '<': 
                out.append(cur)
                if b: cur = '['
                else: cur = c
                mode = 'tag'
            elif c in string.whitespace: out.append(cur+c); cur = ''
            else: cur += c
    out.append(cur)
    return filter(lambda x: x is not '', out)

if __name__ == '__main__':
    import sys
    try:
        a, b = sys.argv[1:3]
    except ValueError:
        print "htmldiff: highlight the differences between two html files"
        print "usage: " + sys.argv[0] + " a b"
        sys.exit(1)
    print textDiff(open(a).read(), open(b).read())

Last edited by snookums; 09-16-2007 at 09:55 PM..
snookums is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-17-2007, 05:23 AM Re: Short python script to php
Skilled Talker

Posts: 62
Name: Tom Wright
Location: Brighton, UK
Trades: 0
Hokay... The real crux of this script is a library called difflib. In fact, all the hard comparison work between the two strings is done in just one line:
PHP Code:
difflib.SequenceMatcher(Noneab
All the rest of that is just telling the script how to properly conduct itself and how to handle the results of what difflib does.
Building an equivalent in php would therefore be very simple - if you can find an equivalent library for php. Otherwise there'd be some pretty hardcore scripting ahead.
Hope this helps, Tom
PS If you do find a library in php, I'll happily build up the rest of the script around it.
__________________
My site:
Please login or register to view this content. Registration is FREE
tomythius is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Short python script to php
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22927 seconds with 12 queries