#!/usr/bin/python2 # -*- coding: utf-8 -*- from pygments import highlight from pygments.lexers import get_lexer_by_name, guess_lexer from pygments.formatters import HtmlFormatter from pygments.lexers import (get_lexer_by_name, get_lexer_for_filename, get_lexer_for_mimetype) import re, os import time def code2html(path): """ load code from given path and return highlighted HTML code """ print "code2html", path t0 = time.time() if not os.path.exists(path) or os.path.isdir(path): return code = open(path).read() ending = re.sub("(.+).(.+)$", r"\2", path) if ending == "template": lexer = get_lexer_by_name("python", stripall=True) else: lexer = guess_lexer(code) #print "code2html guess", time.time() - t0 formatter = HtmlFormatter(linenos=True, cssclass="highlight") #print "code2html format", time.time() - t0 result = '[Return]' # return in history result += highlight(code, lexer, formatter) #print "code2html highlight", time.time() - t0 return result