hy/site/app.hy
2013-03-10 10:58:31 -04:00

48 lines
1.0 KiB
Hy

; Copyright (c) Paul R. Tagliamonte <tag@pault.ag>, 2013 under the terms of
; hy.
(import-from flask
Flask render-template request)
(import-from pygments highlight)
(import-from pygments.lexers PythonLexer ClojureLexer)
(import-from pygments.formatters HtmlFormatter)
(import-from hy.importer import_string_to_ast)
(import codegen)
; pygments bits.
(def lexers {"python" (PythonLexer)
"lisp" (ClojureLexer)})
; internal use fns
(defn colorize-python [x]
(highlight x (get lexers "python") (HtmlFormatter)))
(defn hy-to-py [hython]
(.to_source codegen
(import-string-to-ast hython)))
(def app (Flask "__main__")) ; long story, needed hack
; view routes
(route "/" [] (render-template "index.html"))
(post-route "/format/<language>" [language]
(highlight
(get request.form "code") (get lexers language) (HtmlFormatter)))
(post-route "/hy2py" [] (hy-to-py (get request.form "code")))
(post-route "/hy2pycol" []
(colorize-python (hy-to-py (get request.form "code"))))