def make_html(sourcecode, source_type, django_escape=False): code_html, lexer_name = pygmentize(sourcecode, source_type) code = HTML % {"lexer_name": lexer_name, "code_html": code_html} if django_escape: code = escape_django_tags(code) return mark_safe(code)
formatter = get_formatter() out_object = SimpleStringIO() try: highlight(sourcecode, lexer, formatter, out_object) except Exception, err: if settings.DEBUG: raise html = no_hightlight(sourcecode) lexer_name += " (Error: %s)" % err else: html = out_object.getvalue() # If there is e.g. html code with django tags, we must escape this: html = escape_django_tags(html) html = html.decode("utf-8") return html, lexer_name def get_pygmentize_diff(content1, content2): """ returns the HTML-Diff hightlighted with Pygments Note: the complete content will be returned and not only the "diff-lines". """ diff = make_diff(content1, content2, mode="Differ") diff = "\n".join(diff) # hightlight with Pygments diff_html = make_html(diff, source_type="diff", django_escape=True)