hy/site/app.hy

51 lines
1.1 KiB
Hy
Raw Normal View History

2013-03-10 05:15:22 +01:00
; Copyright (c) Paul R. Tagliamonte <tag@pault.ag>, 2013 under the terms of
; hy.
2013-03-10 15:58:31 +01:00
(import-from flask
Flask render-template request)
2013-03-10 05:15:22 +01:00
2013-03-10 15:58:31 +01:00
(import-from pygments highlight)
(import-from pygments.formatters HtmlFormatter)
2013-03-10 17:59:16 +01:00
(import-from pygments.lexers PythonLexer
ClojureLexer)
2013-03-10 15:58:31 +01:00
2013-03-10 17:59:16 +01:00
(import-from pygments-extension PygmentsExtension)
2013-03-10 15:58:31 +01:00
(import-from hy.importer import_string_to_ast)
(import codegen)
2013-03-10 17:59:16 +01:00
(def app (Flask "__main__")) ; long story, needed hack
(.add_extension app.jinja_env PygmentsExtension)
2013-03-10 15:58:31 +01:00
; pygments bits.
(def lexers {"python" (PythonLexer)
"lisp" (ClojureLexer)})
(defn colorize-python [x]
(highlight x (get lexers "python") (HtmlFormatter)))
2013-03-10 04:45:08 +01:00
2013-03-10 15:58:31 +01:00
(defn hy-to-py [hython]
(.to_source codegen
(import-string-to-ast hython)))
2013-03-10 05:15:22 +01:00
2013-03-10 04:45:08 +01:00
2013-03-10 15:58:31 +01:00
; view routes
2013-03-10 04:45:08 +01:00
(route "/" [] (render-template "index.html"))
2013-03-10 15:30:03 +01:00
2013-03-10 15:58:31 +01:00
(post-route "/format/<language>" [language]
(highlight
2013-03-10 17:59:16 +01:00
(get request.form "code")
(get lexers language)
(HtmlFormatter)))
2013-03-10 15:58:31 +01:00
(post-route "/hy2py" [] (hy-to-py (get request.form "code")))
(post-route "/hy2pycol" []
(colorize-python (hy-to-py (get request.form "code"))))