Mercurial > piecrust2
comparison piecrust/templating/jinja/extensions.py @ 855:448710d84121
refactor: Get the taxonomy support back to a functional state.
There's now a taxonomy content source that wraps another normal content source
like a blog posts' source. It works in tandem with a taxonomy content pipeline
that will do the heavy lifting of figuring out what kind of terms exist and
need to be baked.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 06 Jun 2017 00:26:21 -0700 |
parents | 2c7e57d80bba |
children | 58e28ba02fb7 |
comparison
equal
deleted
inserted
replaced
854:08e02c2a2a1a | 855:448710d84121 |
---|---|
1 from jinja2.ext import Extension, Markup | 1 from jinja2.ext import Extension, Markup |
2 from jinja2.lexer import Token, describe_token | 2 from jinja2.lexer import Token, describe_token |
3 from jinja2.nodes import CallBlock, Const | 3 from jinja2.nodes import CallBlock, Const |
4 from compressinja.html import HtmlCompressor, StreamProcessContext | 4 from compressinja.html import HtmlCompressor, StreamProcessContext |
5 from pygments import highlight | |
6 from pygments.formatters import HtmlFormatter | |
7 from pygments.lexers import get_lexer_by_name, guess_lexer | |
8 from piecrust.rendering import format_text | 5 from piecrust.rendering import format_text |
9 | 6 |
10 | 7 |
11 class PieCrustFormatExtension(Extension): | 8 class PieCrustFormatExtension(Extension): |
12 tags = set(['pcformat']) | 9 tags = set(['pcformat']) |
63 return CallBlock(self.call_method('_highlight', args, kwargs), | 60 return CallBlock(self.call_method('_highlight', args, kwargs), |
64 [], [], body).set_lineno(lineno) | 61 [], [], body).set_lineno(lineno) |
65 | 62 |
66 def _highlight(self, lang, line_numbers=False, use_classes=False, | 63 def _highlight(self, lang, line_numbers=False, use_classes=False, |
67 css_class=None, css_id=None, caller=None): | 64 css_class=None, css_id=None, caller=None): |
65 from pygments import highlight | |
66 from pygments.formatters import HtmlFormatter | |
67 from pygments.lexers import get_lexer_by_name, guess_lexer | |
68 | |
68 # Try to be mostly compatible with Jinja2-highlight's settings. | 69 # Try to be mostly compatible with Jinja2-highlight's settings. |
69 body = caller() | 70 body = caller() |
70 | 71 |
71 if lang is None: | 72 if lang is None: |
72 lexer = guess_lexer(body) | 73 lexer = guess_lexer(body) |
88 code = highlight(Markup(body.rstrip()).unescape(), lexer, formatter) | 89 code = highlight(Markup(body.rstrip()).unescape(), lexer, formatter) |
89 return code | 90 return code |
90 | 91 |
91 | 92 |
92 def get_highlight_css(style_name='default', class_name='.highlight'): | 93 def get_highlight_css(style_name='default', class_name='.highlight'): |
94 from pygments.formatters import HtmlFormatter | |
93 return HtmlFormatter(style=style_name).get_style_defs(class_name) | 95 return HtmlFormatter(style=style_name).get_style_defs(class_name) |
94 | 96 |
95 | 97 |
96 class PieCrustCacheExtension(Extension): | 98 class PieCrustCacheExtension(Extension): |
97 tags = set(['pccache', 'cache']) | 99 tags = set(['pccache', 'cache']) |