Fiddling with the site.

This commit is contained in:
Paul R. Tagliamonte 2013-03-10 10:58:31 -04:00
parent 57bf7b5cba
commit 3f29114fe1
2 changed files with 55 additions and 7 deletions

View File

@ -1,8 +1,8 @@
#
from hy.compiler import hy_compile
from hy.lex import tokenize
from hy.core import process
from hy.compiler import hy_compile
import imp
@ -10,18 +10,34 @@ import sys
import os
def import_file_to_hst(fpath):
tree = tokenize(open(fpath, 'r').read())
if sys.version_info[0] >= 3:
from io import StringIO
else:
from StringIO import StringIO
def import_buffer_to_hst(fd):
tree = tokenize(fd.read())
tree = process(tree)
return tree
def import_file_to_hst(fpath):
return import_buffer_to_hst(open(fpath, 'r'))
def import_file_to_ast(fpath):
tree = import_file_to_hst(fpath)
ast = hy_compile(tree)
return ast
def import_string_to_ast(buff):
tree = import_buffer_to_hst(StringIO(buff))
ast = hy_compile(tree)
return ast
def import_file_to_module(name, fpath):
ast = import_file_to_ast(fpath)
mod = imp.new_module(name)

View File

@ -1,15 +1,47 @@
; Copyright (c) Paul R. Tagliamonte <tag@pault.ag>, 2013 under the terms of
; hy.
(import-from flask
Flask render-template)
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 "/test" [] (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"))))