Diffs with syntax highlight

--- example_a.py
+++ example_b.py
@@ -1,4 +1,5 @@
-def hello()
- print("Hello, World!")
+def hello(name="world")
+ name = name.upper()
+ print("Hello, {name}!".format(name))
hello()

Someone on the #pocoo channel asked if there is any way to highlight a diff between two files while keeping their original syntax highlighting as well (using Pygments).


The obvious option to just highlight the diff with the language's syntax has many flaws. Stuff would just break and look wrong. Multiline string, comments and C macros are just a few examples.

After some thinking I realized that the best way to do this is to highlight the two files individually and then diff them.

Because I had nothing better to do, I decided to deliver.


Anyway... if we try to feed Pygments-generated HTML to Pygments again, it will escape all the HTML. So instead of doing something dirty like unescaping, or writing custom formatters, I decided to just do the diff highliting by myself. There is definitely not much to the syntax...

Let's take these two files as examples:

wzxhzdk:1

wzxhzdk:2

You can see what their diff looks like at the beginning of the post.

And, without further ado, here is the code itself:

wzxhzdk:3

wzxhzdk:4

The result HTML:

wzxhzdk:0

What's nice is that Pygments colors each lines individually, so I didn't have to do anything special for the problem of multiline stuff discussed earlier.


The code snippets are also on Gist.

Created (last updated )
Comments powered by Disqus