~baztian/+junk/diff2wiki

« back to all changes in this revision

Viewing changes to diff2wiki.py

  • Committer: Bastian Bowe
  • Date: 2011-04-07 14:40:56 UTC
  • Revision ID: bastian.bowe@gmail.com-20110407144056-w5bnjhck853merpt
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
import difflib
 
3
import sys
 
4
 
 
5
def _equal(old, new):
 
6
    return new
 
7
 
 
8
def _replace(old, new):
 
9
    return "~~%s~~'''%s'''" % (old, new)
 
10
 
 
11
def _delete(old, new):
 
12
    return "~~%s~~" % old
 
13
 
 
14
def _insert(old, new):
 
15
    return "'''%s'''" % new
 
16
 
 
17
def diff2wiki(old, new):
 
18
    """Create a wiki formatting for the diff of two strings.
 
19
    >>> old = "I think I want spam."
 
20
    >>> new = "I don't want spam!"
 
21
    >>> diff2wiki(old, new)
 
22
    "I '''don''''t~~hink I~~ want spam~~.~~'''!'''"
 
23
    """
 
24
    s = difflib.SequenceMatcher(None, old, new)
 
25
    result = [globals()["_%s" % tag](old[i1:i2], new[j1:j2])
 
26
              for tag, i1, i2, j1, j2 in s.get_opcodes()]
 
27
    return "".join(result)
 
28
 
 
29
if __name__ == "__main__":
 
30
    if len(sys.argv) != 3:
 
31
        print 'diff2wiki "Old text" "New text"'
 
32
        sys.exit(1)
 
33
    print diff2wiki(*sys.argv[1:])