问题
I'm looking for a way to produce HTML files from a git-diff output, preferably using python. I've been looking at http://docs.python.org/library/difflib.html without being able to figure out how to use the git-diff output as an input.
Any clue?
Many thanks
回答1:
You could use the pygments commandline script to get a syntax highligthed HTML output.
Installation:
$ easy_install Pygments
Example:
$ git diff HEAD^1 > last.diff
$ pygmentize -f html -O full,style=trac -l diff -o last.diff.html last.diff
$ # mac only
$ open last.diff.html
Or shorter:
$ git diff | pygmentize -f html -O full,style=emacs -l diff
P.S. To see all available styles, try:
$ pygmentize -L styles
P.P.S. To make the pipeline complete, you can use this trick:
$ git diff | pygmentize -f html -O full,style=emacs -l diff | browser
回答2:
Perhaps difr is you what you're looking for. It takes any kind git style diff as an input and produces an HTML version of it that is pretty to similar to what GitHub would give you. (And it embeds an editor for adding some small comments. That might be a little more than what you asked for, but I don't think it gets in the way.)
https://github.com/wspringer/difr
回答3:
I wrote a simple implementation for my maildiff
def getHtml(diffData):
""" This method convertes git diff data to html color code
"""
openTag = "<span style='font-size: .80em; color: "
openTagEnd = ";font-family: courier, arial, helvetica, sans-serif;'>"
nbsp = ' '
return ''.join([("%s%s%s%s%s</span><br>" % (openTag, '#ff0000' if line.startswith('-') else ('#007900' if line.startswith('+') else '#000000'), openTagEnd, nbsp*line.count('\t') ,line)) for line in diffData])
have a look at it.
来源:https://stackoverflow.com/questions/2053657/git-diff-to-html