From a9a72dfe962338f1360d8e801fc5cf1d53dd6d26 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Sat, 22 Jan 2011 18:55:52 +0530 Subject: [PATCH] Added syntax tag and filter --- hyde/ext/templates/jinja.py | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/hyde/ext/templates/jinja.py b/hyde/ext/templates/jinja.py index df1da83..22e5076 100644 --- a/hyde/ext/templates/jinja.py +++ b/hyde/ext/templates/jinja.py @@ -50,6 +50,7 @@ def markdown(env, value): try: import markdown except ImportError: + logger.error(u"Cannot load the markdown library.") raise TemplateError("Cannot load the markdown library") output = value d = {} @@ -59,6 +60,34 @@ def markdown(env, value): md = markdown.Markdown(**d) return md.convert(output) + +@environmentfilter +def syntax(env, value, lexer=None): + """ + Processes the contained block using `pygments` + """ + + try: + import pygments + from pygments import lexers + self.lexers = lexers + from pygments import formatters + except ImportError: + logger.error(u"pygments library is required to use syntax highlighting tags.") + raise TemplateError("Cannot load pygments") + + pyg = (self.lexers.get_lexer_by_name(lexer) + if lexer else + self.lexers.guess_lexer(value)) + settings = {} + if hasattr(env.config, 'syntax'): + settings = getattr(env.config.syntax, 'options', {}) + + formatter = formatters.HtmlFormatter(**settings) + code = pygments.highlight(value, lexer, formatter) + code = code.replace('\n\n', '\n \n').replace('\n', '
') + return safestring.mark_safe('\n\n
%s
\n\n' % code) + class Markdown(Extension): """ A wrapper around the markdown filter for syntactic sugar. @@ -85,6 +114,33 @@ class Markdown(Extension): output = caller().strip() return markdown(self.environment, output) +class Syntax(Extension): + """ + A wrapper around the syntax filter for syntactic sugar. + """ + tags = set(['syntax']) + + def parse(self, parser): + """ + Parses the statements and defers to the callback for pygments processing. + """ + lineno = parser.stream.next().lineno + lex = parser.stream.next_if('name') + body = parser.parse_statements(['name:endsyntax'], drop_needle=True) + + return nodes.CallBlock( + self.call_method('_render_syntax', nodes.Const(lex)), + [], [], body).set_lineno(lineno) + + def _render_syntax(self, lex, caller=None): + """ + Calls the syntax filter to transform the output. + """ + if not caller: + return '' + output = caller().strip() + return syntax(self.environment, output, lex) + class IncludeText(Extension): """ Automatically runs `markdown` and `typogrify` on included @@ -255,6 +311,7 @@ class Jinja2Template(Template): trim_blocks=True, extensions=[IncludeText, Markdown, + Syntax, Reference, Refer, 'jinja2.ext.do', @@ -264,6 +321,7 @@ class Jinja2Template(Template): self.env.globals['content_url'] = content_url self.env.globals['engine'] = engine self.env.filters['markdown'] = markdown + self.env.filters['syntax'] = syntax config = {} if hasattr(site, 'config'):