From 68c8725c1643319754191795426ebdbcf5e5f7c0 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 6 Mar 2007 17:35:54 +0100 Subject: [PATCH] [svn] added support for comments and some doc changes --HG-- branch : trunk --- TODO | 8 ++++++++ docs/src/frameworks.txt | 33 +++++++++++++++++++++++++++++++++ docs/src/index.txt | 2 ++ jinja/datastructure.py | 8 +++++++- jinja/filters.py | 8 ++++---- jinja/parser.py | 12 ++++++++++-- jinja/translators/python.py | 2 ++ 7 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 TODO create mode 100644 docs/src/frameworks.txt diff --git a/TODO b/TODO new file mode 100644 index 0000000..e77d7c8 --- /dev/null +++ b/TODO @@ -0,0 +1,8 @@ +=================== +TODO List for Jinja +=================== + +- Requirements in Jinja (blocks and set directives) outside of renderable + blocks should become part of the module not the generate function. + +- Improve the context lookup (maybe with an optional C extension) diff --git a/docs/src/frameworks.txt b/docs/src/frameworks.txt new file mode 100644 index 0000000..7a389b2 --- /dev/null +++ b/docs/src/frameworks.txt @@ -0,0 +1,33 @@ +===================== +Framework Integration +===================== + +Jinja registers itself in the baker template plugin system. If your framework +supports baker (currently `TurboGears`_ and `pylons`_) you can add it very +easy. + +Pylons +------ + +Edit ``yourproject/config/middleware.py`` and add the following lines to +`config.init_app...`: + +.. sourcecode:: python + + from jinja import Environment, FileSystemLoader + + config.add_template_engine('jinja', { + 'jinja.environment': Environment(loader=FileSystemLoader('path/to/templates')) + }) + +To make Jinja the default template engine change the `init_app` call to +something like this: + +.. sourcecode:: python + + config.init_app(global_conf, app_conf, package='yourproject', + template_engine='jinja') + + +.. _TurboGears: http://www.turbogears.org/ +.. _pylons: http://www.pylonshq.com/ diff --git a/docs/src/index.txt b/docs/src/index.txt index 250a5a3..a826c2b 100644 --- a/docs/src/index.txt +++ b/docs/src/index.txt @@ -18,6 +18,8 @@ Welcome in the Jinja documentation. - `Translators `_ + - `Framework Integration `_ + - Template Designer Documentation: - `Syntax Reference `_ diff --git a/jinja/datastructure.py b/jinja/datastructure.py index 8d2be5c..73d789d 100644 --- a/jinja/datastructure.py +++ b/jinja/datastructure.py @@ -286,7 +286,7 @@ class TokenStream(object): return True eos = property(lambda x: not x.__nonzero__(), doc=__nonzero__.__doc__) - + def next(self): """Return the next token from the stream.""" if self._pushed: @@ -316,6 +316,12 @@ class TokenStream(object): except StopIteration: raise IndexError('end of stream reached') + def drop_until(self, test, drop_needle=False): + """Fetch tokens until a function matches and drop all + tokens.""" + for token in self.fetch_until(test, drop_needle): + pass + def push(self, lineno, token, data): """Push an yielded token back to the stream.""" self._pushed.append((lineno, token, data)) diff --git a/jinja/filters.py b/jinja/filters.py index 6a0bf33..8d758b2 100644 --- a/jinja/filters.py +++ b/jinja/filters.py @@ -169,8 +169,7 @@ def do_join(d=u''): -> 123 """ def wrapped(env, context, value): - d = env.to_unicode(d) - return d.join([env.to_unicode(x) for x in value]) + return env.to_unicode(d).join([env.to_unicode(x) for x in value]) return wrapped @@ -183,8 +182,8 @@ def do_count(): def wrapped(env, context, value): try: if type(value) in (int, float, long): - return len(str(var)) - return len(var) + return len(str(value)) + return len(value) except TypeError: return 0 return wrapped @@ -307,6 +306,7 @@ FILTERS = { 'default': do_default, 'join': do_join, 'count': do_count, + 'length': do_count, 'reverse': do_reverse, 'center': do_center, 'title': do_title, diff --git a/jinja/parser.py b/jinja/parser.py index 33c8aed..a257985 100644 --- a/jinja/parser.py +++ b/jinja/parser.py @@ -19,6 +19,7 @@ from jinja.exceptions import TemplateSyntaxError # callback functions for the subparse method end_of_block = lambda p, t, d: t == 'block_end' end_of_variable = lambda p, t, d: t == 'variable_end' +end_of_comment = lambda p, t, d: t == 'comment_end' switch_for = lambda p, t, d: t == 'name' and d in ('else', 'endfor') end_of_for = lambda p, t, d: t == 'name' and d == 'endfor' switch_if = lambda p, t, d: t == 'name' and d in ('else', 'elif', 'endif') @@ -299,8 +300,11 @@ class Parser(object): while True: lineno, token, data = self.tokenstream.next() + # comments + if token == 'comment_begin': + self.tokenstream.drop_until(end_of_comment, True) # nested variables - if token == 'variable_begin': + elif token == 'variable_begin': _, variable_token, variable_name = self.tokenstream.next() if variable_token != 'name' or variable_name not in replacements: raise TemplateSyntaxError('unregistered translation ' @@ -410,9 +414,13 @@ class Parser(object): lineno = self.tokenstream.last[0] result = nodes.NodeList(lineno) for lineno, token, data in self.tokenstream: + # comments + if token == 'comment_begin': + self.tokenstream.drop_until(end_of_comment, True) + # this token marks the begin or a variable section. # parse everything till the end of it. - if token == 'variable_begin': + elif token == 'variable_begin': gen = self.tokenstream.fetch_until(end_of_variable, True) result.append(self.directives['print'](lineno, gen)) diff --git a/jinja/translators/python.py b/jinja/translators/python.py index a423820..3128822 100644 --- a/jinja/translators/python.py +++ b/jinja/translators/python.py @@ -415,10 +415,12 @@ class PythonTranslator(Translator): write('def macro(*args):') self.indention += 1 + write('context.push()') write('%s = (args + %s[len(args):])' % (_to_tuple(args), _to_tuple(defaults))) write('macrobuffer = []') write('write = macrobuffer.append') buf.append(self.handle_node(node.body)) + write('context.pop()') write('return u\'\'.join(macrobuffer)') self.indention -= 1 buf.append(self.indent('context[%r] = macro' % node.name)) -- 2.26.2