Adding in some Pygments code.
This commit is contained in:
parent
3f29114fe1
commit
ecca456bdf
@ -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
|
||||
|
19
site/app.hy
19
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>" [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")))
|
||||
|
81
site/pygments_extension.py
Normal file
81
site/pygments_extension.py
Normal file
@ -0,0 +1,81 @@
|
||||
# Copyright (c) 2011 Larry Myers <larry@larrymyers.com>
|
||||
#
|
||||
# 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)
|
@ -3,5 +3,9 @@
|
||||
{% block title %}Welcome!{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
Hello, World
|
||||
|
||||
{% autoescape off %}{% code "clojure" %}
|
||||
(print "foo bar")
|
||||
{% endcode %}{% endautoescape %}
|
||||
|
||||
{% endblock %}
|
||||
|
Loading…
x
Reference in New Issue
Block a user