|
10 | 10 | This is a simple diff utility based upon pygments' lexer token streams. |
11 | 11 | """ |
12 | 12 |
|
13 | | -parser = argparse.ArgumentParser(description=__file__.__doc__) |
| 13 | +parser = argparse.ArgumentParser( |
| 14 | + description="Generates tokenized diffs using Pygments") |
14 | 15 | parser.add_argument('lexername', help="Pygments lexer to utilize") |
15 | 16 | parser.add_argument('file1', type=argparse.FileType('r')) |
16 | 17 | parser.add_argument('file2', type=argparse.FileType('r')) |
17 | 18 | parser.add_argument('-o', '--out', type=argparse.FileType('w'), |
18 | 19 | default=sys.stdout) |
19 | 20 | group = parser.add_mutually_exclusive_group() |
20 | | -group.add_argument('-v', '--verbose', action='store_true') |
21 | | -group.add_argument('-u', '--unidiff', action='store_true') |
| 21 | +group.add_argument('-v', '--verbose', action='store_true', |
| 22 | + help='Verbose tokenization diff') |
| 23 | +group.add_argument('-u', '--unidiff', action='store_true', |
| 24 | + help='Unidiff-like character-based diff (default)') |
| 25 | +group.add_argument('-d', '--delta', action='store_true', |
| 26 | + help='Simplified intermediate delta') |
| 27 | +group.add_argument('-c', '--compare', action='store_true', |
| 28 | + help='HTML comparison of tokenized diff to char diffs') |
22 | 29 | data = parser.parse_args() |
23 | 30 |
|
24 | 31 | lexer = pygments.lexers.get_lexer_by_name(data.lexername) |
|
28 | 35 |
|
29 | 36 | sm = SequenceMatcher(None, a, b) |
30 | 37 |
|
31 | | -if data.verbose or not data.unidiff: |
| 38 | +data.unidiff = not data.verbose and not data.delta and not data.compare |
| 39 | + |
| 40 | +if data.verbose: |
32 | 41 | for op, a1, a2, b1, b2 in sm.get_opcodes(): |
33 | 42 | if op == 'equal': |
34 | 43 | for item in a[a1:a2]: |
|
48 | 57 | data.out.write("- %s: %s\n" % item) |
49 | 58 | else: |
50 | 59 | data.out.write("<<%s>>\n" % op) |
51 | | -elif data.unidiff: |
| 60 | +else: |
52 | 61 | dmp = diff_match_patch() |
53 | 62 | diffs = [] |
54 | 63 | for op, a1, a2, b1, b2 in sm.get_opcodes(): |
|
66 | 75 | elif op == 'delete': |
67 | 76 | for line in ''.join(val for type, val in b[a1:a2]).splitlines(True): |
68 | 77 | diffs.append((dmp.DIFF_DELETE, line)) |
69 | | - patches = dmp.patch_make(diffs) |
70 | | - data.out.write(dmp.patch_toText(patches)) |
| 78 | + if data.unidiff: |
| 79 | + patches = dmp.patch_make(diffs) |
| 80 | + data.out.write(dmp.patch_toText(patches)) |
| 81 | + elif data.delta: |
| 82 | + data.out.write(dmp.diff_toDelta(diffs)) |
71 | 83 |
|
72 | 84 | # vim: ai et ts=4 sts=4 sw=4 |
0 commit comments