From 3f29114fe175679d6d01b774292f1523c5540893 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Sun, 10 Mar 2013 10:58:31 -0400 Subject: [PATCH] Fiddling with the site. --- hy/importer.py | 22 +++++++++++++++++++--- site/app.hy | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/hy/importer.py b/hy/importer.py index f6c0402..1858ab5 100644 --- a/hy/importer.py +++ b/hy/importer.py @@ -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) diff --git a/site/app.hy b/site/app.hy index d59fea9..f4b9fa4 100644 --- a/site/app.hy +++ b/site/app.hy @@ -1,15 +1,47 @@ ; Copyright (c) Paul R. Tagliamonte , 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] + (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"))))