开发者

Git-diff to HTML

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/lib开发者_C百科rary/difflib.html without being able to figure out how to use the git-diff output as an input.

Any clue?

Many thanks


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


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


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 = '&nbsp;&nbsp;&nbsp;&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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜