Skip to content

Commit

Permalink
Moved parse_attrs into the base tag class and renamed BaseExtension t…
Browse files Browse the repository at this point in the history
…o BaseTag.
  • Loading branch information
axiak committed Dec 23, 2011
1 parent e360ec1 commit ea35020
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
27 changes: 20 additions & 7 deletions jinjatag/decorators.py
Expand Up @@ -45,7 +45,20 @@ def dec(func):
return outer_dec


class BaseExtension(Extension):
class BaseTag(Extension):
def parse_attrs(self, parser, add_id=True):
attrs = {}
while parser.stream.current.type != 'block_end':
node = parser.parse_assign_target(with_tuple=False)

if parser.stream.skip_if('assign'):
attrs[node.name] = parser.parse_expression()
else:
attrs[node.name] = nodes.Const(node.name)

return attrs


def call_tag_func(self, *args, **kwargs):
try:
return self.tag_func(*args, **kwargs)
Expand All @@ -65,11 +78,11 @@ def call_tag_func(self, *args, **kwargs):
', '.join([str(arg) for arg in args] + ['{}={}'.format(k, repr(v)) for k, v in kwargs.items()])))

@create_extension_decorator
class simple_tag(BaseExtension):
class simple_tag(BaseTag):
def parse(self, parser):
tag = parser.stream.next()

attrs = extension.JinjaTag._parse_attrs(parser)
attrs = self.parse_attrs(parser)
attrs = nodes.Dict([nodes.Pair(nodes.Const(k), v) for k,v in attrs.items()])

return nodes.Output([self.call_method('_call_simple_tag', args=[attrs])])
Expand All @@ -78,11 +91,11 @@ def _call_simple_tag(self, attrs):
return self.call_tag_func(**attrs)

@create_extension_decorator
class simple_block(BaseExtension):
class simple_block(BaseTag):
def parse(self, parser):
tag = parser.stream.next()

attrs = extension.JinjaTag._parse_attrs(parser)
attrs = self.parse_attrs(parser)
attrs = nodes.Dict([nodes.Pair(nodes.Const(k), v) for k,v in attrs.items()])

body = parser.parse_statements(['name:end'+tag.value], drop_needle=True)
Expand All @@ -95,7 +108,7 @@ def _call_simple_block(self, attrs, caller):


@create_extension_decorator
class multibody_block(BaseExtension):
class multibody_block(BaseTag):
def parse(self, parser):
INSIDE_BLOCK, OUTSIDE_BLOCK = 0, 1

Expand All @@ -116,7 +129,7 @@ def parse(self, parser):

state = OUTSIDE_BLOCK

attrs_ = extension.JinjaTag._parse_attrs(parser)
attrs_ = self.parse_attrs(parser)
body = parser.parse_statements(end_tags[state], drop_needle=False)

node_list = []
Expand Down
15 changes: 1 addition & 14 deletions jinjatag/extension.py
@@ -1,4 +1,4 @@
from jinja2 import Environment, environmentfunction, nodes
from jinja2 import Environment
from jinja2.ext import Extension

__all__ = ('TagRegistrar', 'JinjaTag',)
Expand All @@ -15,19 +15,6 @@ def __call__(self, environment):
def init(self):
_jinja_tags.set_base_ext(self)

@classmethod
def _parse_attrs(cls, parser, add_id=True):
attrs = {}
while parser.stream.current.type != 'block_end':
node = parser.parse_assign_target(with_tuple=False)

if parser.stream.skip_if('assign'):
attrs[node.name] = parser.parse_expression()
else:
attrs[node.name] = nodes.Const(node.name)

return attrs

class TagRegistrar(object):
def __init__(self):
self.ext = None
Expand Down

0 comments on commit ea35020

Please sign in to comment.