Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
# encoding: utf-8
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
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>hg diff</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="Preston Holmes">
<!-- Date: 2009-11-20 -->
<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()
file_to_diff = os.path.abspath(args[0])
diff_str = sh ('hg diff "%s"' % file_to_diff)
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')
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment