Fiddling with the site.
This commit is contained in:
parent
57bf7b5cba
commit
3f29114fe1
@ -1,8 +1,8 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
|
from hy.compiler import hy_compile
|
||||||
from hy.lex import tokenize
|
from hy.lex import tokenize
|
||||||
from hy.core import process
|
from hy.core import process
|
||||||
from hy.compiler import hy_compile
|
|
||||||
|
|
||||||
|
|
||||||
import imp
|
import imp
|
||||||
@ -10,18 +10,34 @@ import sys
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
def import_file_to_hst(fpath):
|
if sys.version_info[0] >= 3:
|
||||||
tree = tokenize(open(fpath, 'r').read())
|
from io import StringIO
|
||||||
|
else:
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
|
|
||||||
|
def import_buffer_to_hst(fd):
|
||||||
|
tree = tokenize(fd.read())
|
||||||
tree = process(tree)
|
tree = process(tree)
|
||||||
return tree
|
return tree
|
||||||
|
|
||||||
|
|
||||||
|
def import_file_to_hst(fpath):
|
||||||
|
return import_buffer_to_hst(open(fpath, 'r'))
|
||||||
|
|
||||||
|
|
||||||
def import_file_to_ast(fpath):
|
def import_file_to_ast(fpath):
|
||||||
tree = import_file_to_hst(fpath)
|
tree = import_file_to_hst(fpath)
|
||||||
ast = hy_compile(tree)
|
ast = hy_compile(tree)
|
||||||
return ast
|
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):
|
def import_file_to_module(name, fpath):
|
||||||
ast = import_file_to_ast(fpath)
|
ast = import_file_to_ast(fpath)
|
||||||
mod = imp.new_module(name)
|
mod = imp.new_module(name)
|
||||||
|
40
site/app.hy
40
site/app.hy
@ -1,15 +1,47 @@
|
|||||||
; Copyright (c) Paul R. Tagliamonte <tag@pault.ag>, 2013 under the terms of
|
; Copyright (c) Paul R. Tagliamonte <tag@pault.ag>, 2013 under the terms of
|
||||||
; hy.
|
; hy.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(import-from flask
|
(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
|
(def app (Flask "__main__")) ; long story, needed hack
|
||||||
|
|
||||||
|
|
||||||
|
; view routes
|
||||||
|
|
||||||
(route "/" [] (render-template "index.html"))
|
(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"))))
|
||||||
|
Loading…
Reference in New Issue
Block a user