From 11d511f9ccfdc83927e7c8ed9917f7c195d2b295 Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Tue, 8 Jan 2013 22:39:02 -0500 Subject: [PATCH] Extract util for parsing common bits. In preparation for new thumbnail and placeholder tag syntaxes (#177 and #176) which share some (but not all) syntax with the generateimage tag. --- imagekit/templatetags/imagekit.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/imagekit/templatetags/imagekit.py b/imagekit/templatetags/imagekit.py index d5aa9c6..c2c6363 100644 --- a/imagekit/templatetags/imagekit.py +++ b/imagekit/templatetags/imagekit.py @@ -73,9 +73,15 @@ class GenerateImageTagNode(template.Node): return mark_safe(u'' % attr_str) -def _generateimage(parser, bits): +def parse_ik_tag_bits(parser, bits): + """ + Parses the tag name, html attributes and variable name (for assignment tags) + from the provided bits. The preceding bits may vary and are left to be + parsed by specific tags. + + """ varname = None - html_bits = [] + html_attrs = {} tag_name = bits.pop(0) if bits[-2] == ASSIGNMENT_DELIMETER: @@ -90,6 +96,19 @@ def _generateimage(parser, bits): raise template.TemplateSyntaxError('Don\'t use "%s" unless you\'re' ' setting html attributes.' % HTML_ATTRS_DELIMITER) + args, html_attrs = parse_bits(parser, html_bits, [], 'args', + 'kwargs', None, False, tag_name) + if len(args): + raise template.TemplateSyntaxError('All "%s" tag arguments after' + ' the "%s" token must be named.' % (tag_name, + HTML_ATTRS_DELIMITER)) + + return (tag_name, bits, html_attrs, varname) + + +def _generateimage(parser, bits): + tag_name, bits, html_attrs, varname = parse_ik_tag_bits(parser, bits) + args, kwargs = parse_bits(parser, bits, ['generator_id'], 'args', 'kwargs', None, False, tag_name) @@ -102,13 +121,7 @@ def _generateimage(parser, bits): if varname: return GenerateImageAssignmentNode(varname, generator_id, kwargs) else: - html_args, html_kwargs = parse_bits(parser, html_bits, [], 'args', - 'kwargs', None, False, tag_name) - if len(html_args): - raise template.TemplateSyntaxError('All "%s" tag arguments after' - ' the "%s" token must be named.' % (tag_name, - HTML_ATTRS_DELIMITER)) - return GenerateImageTagNode(generator_id, kwargs, html_kwargs) + return GenerateImageTagNode(generator_id, kwargs, html_attrs) #@register.tag