from BeautifulSoup import BeautifulSoup from pygments import highlight from pygments.formatters import HtmlFormatter from pygments.lexers import guess_lexer, get_lexer_by_name import re def Parse ( content ): tree = BeautifulSoup ( content ) for code in tree.findAll ( 'code' ): if not code['class']: code['class'] = 'text' lexer = get_lexer_by_name(code['class']) new_content = highlight ( code.contents[0], lexer, HtmlFormatter() ) code.replaceWith ( "%s\n%s" % (info,new_content) ) content = str(tree) return content