Created
October 12, 2011 17:24
-
-
Save ptone/1281887 to your computer and use it in GitHub Desktop.
display diff output in webpage
This file contains hidden or 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
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
Usage: git diff | webdiff | |
Created by Preston Holmes on 2009-11-20. | |
Copyright (c) 2009 __MyCompanyName__. All rights reserved. | |
""" | |
import sys | |
import os | |
from optparse import OptionParser,OptionGroup | |
from pygments import highlight | |
from pygments.lexers import DiffLexer | |
from pygments.formatters import HtmlFormatter | |
from subprocess import Popen, call, STDOUT, PIPE | |
# import webbrowser | |
html_shell = """ | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | |
"http://www.w3.org/TR/html4/loose.dtd"> | |
<html lang="en"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<title>diff</title> | |
<style type="text/css" media="screen"> | |
%s | |
</style> | |
</head> | |
<body> | |
%s | |
</body> | |
</html> | |
""" | |
def sh(cmd): | |
return Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE).communicate()[0] | |
def main(argv=None): | |
parser = OptionParser() | |
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", | |
help="display all verbose output",default=False) | |
# parse options: metavar, default action: store | |
parser.usage = """ | |
help message | |
""" | |
(options, args) = parser.parse_args() | |
diff_str = sys.stdin.read() | |
css = HtmlFormatter().get_style_defs() | |
html = highlight(diff_str, DiffLexer(), HtmlFormatter()) | |
s = html_shell % (css,html) | |
open('/tmp/diff.html','w').write(s) | |
sh ('open -a safari /tmp/diff.html') | |
# webbrowser.open('/tmp/diff.html') | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment