-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjinja2_chakert.py
37 lines (27 loc) · 1.45 KB
/
jinja2_chakert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'''Example implementation of typography tag and filter for jinja2'''
from jinja2 import nodes, contextfilter
from jinja2.ext import Extension
from chakert import Typograph
class TypographExtension(Extension):
# a set of names that trigger the extension.
tags = set(['typograph'])
def parse(self, parser):
# the first token is the token that started the tag. In our case
# we only listen to ``'typograph'`` so this will be a name token with
# `typograph` as value. We get the line number so that we can give
# that line number to the nodes we create by hand.
lineno = next(parser.stream).lineno
# now we parse the body of the block up to `endtypograph` and
# drop the needle (which would always be `endtypograph` in that case)
body = parser.parse_statements(['name:endtypograph'], drop_needle=True)
# pass the context as an argument to called method
ctx_ref = nodes.ContextReference()
# now return a `CallBlock` node that calls our _typograph_support
# helper method on this extension.
node = self.call_method('_typograph_support', [ctx_ref], lineno=lineno)
return nodes.CallBlock(node, [], [], body, lineno=lineno)
def _typograph_support(self, context, caller=None):
return Typograph.typograph_html(caller(), context['lang'])
@contextfilter
def do_typograph(context, value):
return Typograph.typograph_text(value, context['lang'])