1. Miki Tebeka
  2. highlight

Commits

Miki Tebeka  committed c2579c1

minimal server

  • Participants
  • Branches default

Comments (0)

Files changed (1)

File highlighter.py

View file
  • Ignore whitespace
+#!/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 "TBD"
+
+    @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())