""" Django template tag for inserting Shrink The Web images into templates. Configuration: Save this file as shrinktheweb_tags.py in the project's templatetags directory. A variable named SHRINK_THE_WEB_KEY should be defined in settings.py set to the user's STW key. Usage: {% load shrinktheweb_tags %} {% shrinkthewebimage url size alt %} where: - url is expected to be a variable instantiated from the context - size is passed directly to shrinktheweb.com which currently accepts one of: "sm", "lg" or "xlg" (may be either a quoted string or not) - alt is either a quoted string or a variable instantiated from the context Example usage: Given a template context variable "author" with an attribute "url" the following are valid entries in a template file: {% load shrinktheweb_tags %} {% shrinkthewebimage author.url "sm" '' %} {% shrinkthewebimage author.url lg author.url %} {% shrinkthewebimage author.url 'xlg' "The author's website" %} Copyright (c) 2009 Steve Schwarz Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from django.utils.safestring import mark_safe from django.conf import settings from django import template class FormatSTWImageNode(template.Node): def __init__(self, url, size, alt): self.args = dict(url=template.Variable(url), size=size, alt=alt, key=settings.SHRINK_THE_WEB_KEY) def render(self, context): url = self.args['url'].resolve(context) alt = self.args['alt'] if alt[0] == alt[-1] and alt[0] in ('"', "'"): alt = alt[1:-1] # a string else: alt = template.Variable(alt).resolve(context) size = self.args['size'] if size[0] == size[-1] and size[0] in ('"', "'"): size = size[1:-1] # a string key = self.args['key'] result = '''%s''' % (key, size, url, alt) return result def do_shrinkthewebimage(parser, token): bits = token.split_contents() if len(bits) != 4: raise template.TemplateSyntaxError("'shrinkthewebimage' tag takes exactly 3 arguments") return FormatSTWImageNode(url=bits[1], size=bits[2], alt=bits[3]) register = template.Library() register.tag('shrinkthewebimage', do_shrinkthewebimage)