Last active
October 22, 2023 01:34
-
-
Save oprypin/9668969 to your computer and use it in GitHub Desktop.
Diffs with syntax highlight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import difflib | |
import pygments | |
import pygments.lexers | |
import pygments.formatters | |
def read_file(fn): | |
with open(fn) as f: | |
return f.read() | |
file_names = ['example_a.py', 'example_b.py'] | |
file_contents = [read_file(fn) for fn in file_names] | |
# Override HtmlFormatter's wrap function so it | |
# doesn't wrap our result in excess HTML containers | |
class Formatter(pygments.formatters.HtmlFormatter): | |
def wrap(self, source, outfile): | |
return source | |
# Highlight both files with Pygments (guessing lexer by filename and code) | |
colored = [pygments.highlight(fc, | |
pygments.lexers.guess_lexer_for_filename(fn, fc), | |
Formatter() | |
).splitlines(keepends=True) for fn, fc in zip(file_names, file_contents)] | |
# Also, difflib wants the inputs as separate lines with newlines at the end | |
with open('out.html', 'w') as out_file: | |
# Write the needed HTML to enable styles | |
out_file.write('<head><link rel="stylesheet" type="text/css" href="style.css"></head>\n') | |
out_file.write('<pre class="code">') | |
# Get the diff line by line | |
# The 4 arguments are 2 files and their 2 names | |
for line in difflib.unified_diff(*(colored+file_names)): | |
# For each line we output a <div> with the original content, | |
# but if it starts with a special diff symbol, we apply a class to it | |
cls = {'+': 'diff_plus', '-': 'diff_minus', '@': 'diff_special'}.get(line[:1], '') | |
if cls: | |
cls = ' class="{}"'.format(cls) | |
line = '<div{}>{}</div>'.format(cls, line.rstrip('\n')) | |
out_file.write(line) | |
out_file.write('</pre>') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hello() | |
print("Hello, World!") | |
hello() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hello(name="world") | |
name = name.upper() | |
print("Hello, {name}!".format(name)) | |
hello() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.code .diff_minus { background-color: rgba(255, 0, 0, 0.3) } | |
.code .diff_plus { background-color: rgba(0, 255, 0, 0.3) } | |
.code .diff_special { background-color: rgba(128, 128, 128, 0.3) } | |
.code .hll { background-color: #ffffcc } | |
.code .c { color: #408080; font-style: italic } | |
.code .err { border: 1px solid #FF0000 } | |
.code .k { color: #008000; font-weight: bold } | |
.code .o { color: #666666 } | |
.code .cm { color: #408080; font-style: italic } | |
.code .cp { color: #BC7A00 } | |
.code .c1 { color: #408080; font-style: italic } | |
.code .cs { color: #408080; font-style: italic } | |
.code .gd { color: #A00000 } | |
.code .ge { font-style: italic } | |
.code .gr { color: #FF0000 } | |
.code .gh { color: #000080; font-weight: bold } | |
.code .gi { color: #00A000 } | |
.code .go { color: #808080 } | |
.code .gp { color: #000080; font-weight: bold } | |
.code .gs { font-weight: bold } | |
.code .gu { color: #800080; font-weight: bold } | |
.code .gt { color: #0040D0 } | |
.code .kc { color: #008000; font-weight: bold } | |
.code .kd { color: #008000; font-weight: bold } | |
.code .kn { color: #008000; font-weight: bold } | |
.code .kp { color: #008000 } | |
.code .kr { color: #008000; font-weight: bold } | |
.code .kt { color: #B00040 } | |
.code .m { color: #666666 } | |
.code .s { color: #BA2121 } | |
.code .na { color: #7D9029 } | |
.code .nb { color: #008000 } | |
.code .nc { color: #0000FF; font-weight: bold } | |
.code .no { color: #880000 } | |
.code .nd { color: #AA22FF } | |
.code .ni { color: #999999; font-weight: bold } | |
.code .ne { color: #D2413A; font-weight: bold } | |
.code .nf { color: #0000FF } | |
.code .nl { color: #A0A000 } | |
.code .nn { color: #0000FF; font-weight: bold } | |
.code .nt { color: #008000; font-weight: bold } | |
.code .nv { color: #19177C } | |
.code .ow { color: #AA22FF; font-weight: bold } | |
.code .w { color: #bbbbbb } | |
.code .mf { color: #666666 } | |
.code .mh { color: #666666 } | |
.code .mi { color: #666666 } | |
.code .mo { color: #666666 } | |
.code .sb { color: #BA2121 } | |
.code .sc { color: #BA2121 } | |
.code .sd { color: #BA2121; font-style: italic } | |
.code .s2 { color: #BA2121 } | |
.code .se { color: #BB6622; font-weight: bold } | |
.code .sh { color: #BA2121 } | |
.code .si { color: #BB6688; font-weight: bold } | |
.code .sx { color: #008000 } | |
.code .sr { color: #BB6688 } | |
.code .s1 { color: #BA2121 } | |
.code .ss { color: #19177C } | |
.code .bp { color: #008000 } | |
.code .vc { color: #19177C } | |
.code .vg { color: #19177C } | |
.code .vi { color: #19177C } | |
.code .il { color: #666666 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a variant of this for output in a terminal: