Source

highlight / highlighter.py

Full commit
#!/usr/bin/env python

from cherrypy import expose, quickstart
from pygments import highlight
from pygments.lexers import get_lexer_by_name, guess_lexer
from pygments.formatters import HtmlFormatter


class Highlighter:
    @expose
    def index(self):
        return open("index.html").read()

    @expose
    def highlight(self, **kw):
        code = kw.get("code", "")
        if not code:
            return ""

        lang = kw.get("lang", "")
        if not lang:
            lexer = guess_lexer(code)
        else:
            lexer = get_lexer_by_name(lang)

        linenos = kw.get("linenos", 0)

        formatter = HtmlFormatter(linenos=linenos, cssclass="source")
        return highlight(code, lexer, formatter)

if __name__ == "__main__":
    quickstart(Highlighter())