7

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

3 Answers3

9

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
miku
  • 181,842
  • 47
  • 306
  • 310
  • Thanks, however this is not exactly what I'm looking for. I'm sorry my question wasn't clear enough, and maybe it is indeed something to look at, but I'm afraid pygment is limited to syntax highlighting. Regular diff outputs use "ascii" to graphically show the differences between two files, what'd like to do is to show thoses differences graphically, either by displaying the two files side by side (like wikipedia) or for instance by using css attributes like 'overline' to show deleted chunks. –  Jan 14 '10 at 11:16
1

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

Wilfred Springer
  • 10,869
  • 4
  • 55
  • 69
1

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.

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197