cleanup code style, fix Python 3 issues on the way

I verified that the generated html files are identical before and after this patch as well as with Python 2 and 3.

@tfoote Please review.