Generate pretty Github-style diffs
Project description
ghdiff
Generate Github-style HTML for unified diffs.
Changes
0.2
Detect character encoding when reading files (Nyoroon)
PEP-8 clean up (laulaz)
Fix display problem when text line is too long (laulaz)
0.1
initial release.
diff
Generate a diff and output Github-style HTML for it.
>>> import ghdiff
>>> from six import print_
>>> print_(ghdiff.diff("a\nb", "b\nb"))
<style type="text/css">
...
</style>
<div class="diff">
<div class="control">@@ -1,2 +1,2 @@
</div>
<div class="delete">-a</div>
<div class=""> b</div>
<div class="insert">+b</div>
</div>
The css option controls whether or not the output includes CSS.
>>> print_(ghdiff.diff("blah blah blah\nb", "blah zxqq blah\nb", css=False))
<div class="diff">
<div class="control">@@ -1,2 +1,2 @@
</div>
<div class="delete">-blah <span class="highlight">blah</span> blah</div>
<div class="insert">+blah <span class="highlight">zxqq</span> blah</div>
<div class=""> b</div>
</div>
diff accepts lists of strings representing lines as well.
>>> print_(ghdiff.diff(["blah blah blah", "b"], ["blah zxqq blah", "b"]))
<style type="text/css">
...
</style>
<div class="diff">
<div class="control">@@ -1,2 +1,2 @@
</div>
<div class="delete">-blah <span class="highlight">blah</span> blah</div>
<div class="insert">+blah <span class="highlight">zxqq</span> blah</div>
<div class=""> b</div>
</div>
colorize
colorize takes an existing unified diff and outputs Github-style markup.
>>> print_(ghdiff.colorize("""\
... index 921100e..8b177e1 100755
... --- a/src/ghdiff.py
... +++ b/src/ghdiff.py
... @@ -10,20 +10,24 @@ def escape(text):
... default_css = \"\"\"\
... <style type="text/css">
... %s
... -</style>\"\"\" % (open(os.path.join(os.path.dirname(__file__), "default.css")).read(),)
... +</style>
... +\"\"\" % (open(os.path.join(os.path.dirname(__file__), "default.css")).read(),)
... +"""))
<style type="text/css">
...
</style>
<div class="diff">
<div class="control">@@ -10,20 +10,24 @@ def escape(text):</div>
<div class=""> default_css = """ <style type="text/css"></div>
<div class=""> %s</div>
<div class="delete">-</style>""" % (open(os.path.join(os.path.dirname(__file__), "default.css")).read(),)</div>
<div class="insert">+</style></div>
<div class="insert">+""" % (open(os.path.join(os.path.dirname(__file__), "default.css")).read(),)</div>
<div class="insert">+</div>
</div>
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ghdiff-0.2.tar.gz
(4.5 kB
view hashes)