diff --git a/hy/compiler.py b/hy/compiler.py index 3ae31c8..1bd65c4 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -169,6 +169,26 @@ class HyASTCompiler(object): slice=ast.Index(value=sli), ctx=ast.Load()) + @builds("assoc") + def compile_index_expression(self, expr): + expr.pop(0) # assoc + # (assoc foo bar baz) => foo[bar] = baz + target = expr.pop(0) + key = expr.pop(0) + val = expr.pop(0) + + return ast.Assign( + lineno=expr.start_line, + col_offset=expr.start_column, + targets=[ + ast.Subscript( + lineno=expr.start_line, + col_offset=expr.start_column, + value=self.compile(target), + slice=ast.Index(value=self.compile(key)), + ctx=ast.Store())], + value=self.compile(val)) + @builds("decorate_with") def compile_decorate_expression(self, expr): expr.pop(0) # decorate-with diff --git a/site/app.hy b/site/app.hy index f4b9fa4..46377cf 100644 --- a/site/app.hy +++ b/site/app.hy @@ -5,19 +5,24 @@ Flask render-template request) (import-from pygments highlight) -(import-from pygments.lexers PythonLexer ClojureLexer) (import-from pygments.formatters HtmlFormatter) +(import-from pygments.lexers PythonLexer + ClojureLexer) +(import-from pygments-extension PygmentsExtension) (import-from hy.importer import_string_to_ast) - (import codegen) + +(def app (Flask "__main__")) ; long story, needed hack +(.add_extension app.jinja_env PygmentsExtension) + + ; pygments bits. (def lexers {"python" (PythonLexer) "lisp" (ClojureLexer)}) -; internal use fns (defn colorize-python [x] (highlight x (get lexers "python") (HtmlFormatter))) @@ -27,17 +32,15 @@ (import-string-to-ast hython))) -(def app (Flask "__main__")) ; long story, needed hack - - ; view routes - (route "/" [] (render-template "index.html")) (post-route "/format/" [language] (highlight - (get request.form "code") (get lexers language) (HtmlFormatter))) + (get request.form "code") + (get lexers language) + (HtmlFormatter))) (post-route "/hy2py" [] (hy-to-py (get request.form "code"))) diff --git a/site/pygments_extension.py b/site/pygments_extension.py new file mode 100644 index 0000000..9c5e08c --- /dev/null +++ b/site/pygments_extension.py @@ -0,0 +1,81 @@ +# Copyright (c) 2011 Larry Myers +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. + + +from jinja2 import nodes +from jinja2.ext import Extension + +from pygments import highlight +from pygments.formatters import HtmlFormatter +from pygments.lexers import guess_lexer, get_lexer_by_name + + +class PygmentsExtension(Extension): + """ + A Pygments extension for use with the Jinja2 template language. + + Setup: + + import PygmentsExtension + from jinja2 import Environment + + jinja2_env = Environment(extensions=[PygmentsExtension]) + + Usage: + + {% code 'javascript' %} + function foo() { console.log('bar'); } + {% endcode %} + """ + tags = set(['code']) + + def __init__(self, environment): + super(PygmentsExtension, self).__init__(environment) + + # add the defaults to the environment + environment.extend( + pygments=self + ) + + def parse(self, parser): + lineno = parser.stream.next().lineno + + args = [] + lang_type = parser.parse_expression() + + if lang_type is not None: + args.append(lang_type) + + body = parser.parse_statements(['name:endcode'], drop_needle=True) + + return nodes.CallBlock(self.call_method('_pygmentize', args), + [], [], body).set_lineno(lineno) + + def _pygmentize(self, lang_type, caller): + lexer = None + formatter = HtmlFormatter() + content = caller() + + if lang_type is None: + lexer = guess_lexer(content) + else: + lexer = get_lexer_by_name(lang_type) + + return highlight(content, lexer, formatter) diff --git a/site/templates/index.html b/site/templates/index.html index 83ac5ac..7ba9d95 100644 --- a/site/templates/index.html +++ b/site/templates/index.html @@ -3,5 +3,9 @@ {% block title %}Welcome!{% endblock %} {% block content %} -Hello, World + +{% autoescape off %}{% code "clojure" %} +(print "foo bar") +{% endcode %}{% endautoescape %} + {% endblock %}