<s> from __future__ import unicode_literals <EOL> from django import template <EOL> from django . utils . html import escape <EOL> from django . utils . safestring import mark_safe <EOL> from . . compat import parse_bits <EOL> from . . cachefiles import ImageCacheFile <EOL> from . . registry import generator_registry <EOL> from . . lib import force_text <EOL> register = template . Library ( ) <EOL> ASSIGNMENT_DELIMETER = '<STR_LIT>' <EOL> HTML_ATTRS_DELIMITER = '<STR_LIT>' <EOL> DEFAULT_THUMBNAIL_GENERATOR = '<STR_LIT>' <EOL> def get_cachefile ( context , generator_id , generator_kwargs , source = None ) : <EOL> generator_id = generator_id . resolve ( context ) <EOL> kwargs = dict ( ( k , v . resolve ( context ) ) for k , v in generator_kwargs . items ( ) ) <EOL> generator = generator_registry . get ( generator_id , ** kwargs ) <EOL> return ImageCacheFile ( generator ) <EOL> def parse_dimensions ( dimensions ) : <EOL> """<STR_LIT>""" <EOL> width , height = [ d . strip ( ) and int ( d ) or None for d in dimensions . split ( '<STR_LIT:x>' ) ] <EOL> return dict ( width = width , height = height ) <EOL> class GenerateImageAssignmentNode ( template . Node ) : <EOL> def __init__ ( self , variable_name , generator_id , generator_kwargs ) : <EOL> self . _generator_id = generator_id <EOL> self . _generator_kwargs = generator_kwargs <EOL> self . _variable_name = variable_name <EOL> def get_variable_name ( self , context ) : <EOL> return force_text ( self . _variable_name ) <EOL> def render ( self , context ) : <EOL> variable_name = self . get_variable_name ( context ) <EOL> context [ variable_name ] = get_cachefile ( context , self . _generator_id , <EOL> self . _generator_kwargs ) <EOL> return '<STR_LIT>' <EOL> class GenerateImageTagNode ( template . Node ) : <EOL> def __init__ ( self , generator_id , generator_kwargs , html_attrs ) : <EOL> self . _generator_id = generator_id <EOL> self . _generator_kwargs = generator_kwargs <EOL> self . _html_attrs = html_attrs <EOL> def render ( self , context ) : <EOL> file = get_cachefile ( context , self . _generator_id , <EOL> self . _generator_kwargs ) <EOL> attrs = dict ( ( k , v . resolve ( context ) ) for k , v in <EOL> self . _html_attrs . items ( ) ) <EOL> if not '<STR_LIT:width>' in attrs and not '<STR_LIT>' in attrs : <EOL> attrs . update ( width = file . width , height = file . height ) <EOL> attrs [ '<STR_LIT:src>' ] = file . url <EOL> attr_str = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % ( escape ( k ) , escape ( v ) ) for k , v in <EOL> attrs . items ( ) ) <EOL> return mark_safe ( '<STR_LIT>' % attr_str ) <EOL> class ThumbnailAssignmentNode ( template . Node ) : <EOL> def __init__ ( self , variable_name , generator_id , dimensions , source , generator_kwargs ) : <EOL> self . _variable_name = variable_name <EOL> self . _generator_id = generator_id <EOL> self . _dimensions = dimensions <EOL> self . _source = source <EOL> self . _generator_kwargs = generator_kwargs <EOL> def get_variable_name ( self , context ) : <EOL> return force_text ( self . _variable_name ) <EOL> def render ( self , context ) : <EOL> variable_name = self . get_variable_name ( context ) <EOL> generator_id = self . _generator_id . resolve ( context ) if self . _generator_id else DEFAULT_THUMBNAIL_GENERATOR <EOL> kwargs = dict ( ( k , v . resolve ( context ) ) for k , v in <EOL> self . _generator_kwargs . items ( ) ) <EOL> kwargs [ '<STR_LIT:source>' ] = self . _source . resolve ( context ) <EOL> kwargs . update ( parse_dimensions ( self . _dimensions . resolve ( context ) ) ) <EOL> generator = generator_registry . get ( generator_id , ** kwargs ) <EOL> context [ variable_name ] = ImageCacheFile ( generator ) <EOL> return '<STR_LIT>' <EOL> class ThumbnailImageTagNode ( template . Node ) : <EOL> def __init__ ( self , generator_id , dimensions , source , generator_kwargs , html_attrs ) : <EOL> self . _generator_id = generator_id <EOL> self . _dimensions = dimensions <EOL> self . _source = source <EOL> self . _generator_kwargs = generator_kwargs <EOL> self . _html_attrs = html_attrs <EOL> def render ( self , context ) : <EOL> generator_id = self . _generator_id . resolve ( context ) if self . _generator_id else DEFAULT_THUMBNAIL_GENERATOR <EOL> dimensions = parse_dimensions ( self . _dimensions . resolve ( context ) ) <EOL> kwargs = dict ( ( k , v . resolve ( context ) ) for k , v in <EOL> self . _generator_kwargs . items ( ) ) <EOL> kwargs [ '<STR_LIT:source>' ] = self . _source . resolve ( context ) <EOL> kwargs . update ( dimensions ) <EOL> generator = generator_registry . get ( generator_id , ** kwargs ) <EOL> file = ImageCacheFile ( generator ) <EOL> attrs = dict ( ( k , v . resolve ( context ) ) for k , v in <EOL> self . _html_attrs . items ( ) ) <EOL> if not '<STR_LIT:width>' in attrs and not '<STR_LIT>' in attrs : <EOL> attrs . update ( width = file . width , height = file . height ) <EOL> attrs [ '<STR_LIT:src>' ] = file . url <EOL> attr_str = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % ( escape ( k ) , escape ( v ) ) for k , v in <EOL> attrs . items ( ) ) <EOL> return mark_safe ( '<STR_LIT>' % attr_str ) <EOL> def parse_ik_tag_bits ( parser , bits ) : <EOL> """<STR_LIT>""" <EOL> varname = None <EOL> html_attrs = { } <EOL> tag_name = bits . pop ( <NUM_LIT:0> ) <EOL> if len ( bits ) >= <NUM_LIT:2> and bits [ - <NUM_LIT:2> ] == ASSIGNMENT_DELIMETER : <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> bits = bits [ : - <NUM_LIT:2> ] <EOL> if HTML_ATTRS_DELIMITER in bits : <EOL> if varname : <EOL> raise template . TemplateSyntaxError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( HTML_ATTRS_DELIMITER , tag_name ) ) <EOL> index = bits . index ( HTML_ATTRS_DELIMITER ) <EOL> html_bits = bits [ index + <NUM_LIT:1> : ] <EOL> bits = bits [ : index ] <EOL> if not html_bits : <EOL> raise template . TemplateSyntaxError ( '<STR_LIT>' <EOL> '<STR_LIT>' % HTML_ATTRS_DELIMITER ) <EOL> args , html_attrs = parse_bits ( parser , html_bits , [ ] , '<STR_LIT:args>' , <EOL> '<STR_LIT>' , None , False , tag_name ) <EOL> if len ( args ) : <EOL> raise template . TemplateSyntaxError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( tag_name , <EOL> HTML_ATTRS_DELIMITER ) ) <EOL> return ( tag_name , bits , html_attrs , varname ) <EOL> def generateimage ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> tag_name , bits , html_attrs , varname = parse_ik_tag_bits ( parser , bits ) <EOL> args , kwargs = parse_bits ( parser , bits , [ '<STR_LIT>' ] , '<STR_LIT:args>' , '<STR_LIT>' , <EOL> None , False , tag_name ) <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> raise template . TemplateSyntaxError ( '<STR_LIT>' <EOL> '<STR_LIT>' % tag_name ) <EOL> generator_id = args [ <NUM_LIT:0> ] <EOL> if varname : <EOL> return GenerateImageAssignmentNode ( varname , generator_id , kwargs ) <EOL> else : <EOL> return GenerateImageTagNode ( generator_id , kwargs , html_attrs ) <EOL> def thumbnail ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> tag_name , bits , html_attrs , varname = parse_ik_tag_bits ( parser , bits ) <EOL> args , kwargs = parse_bits ( parser , bits , [ ] , '<STR_LIT:args>' , '<STR_LIT>' , <EOL> None , False , tag_name ) <EOL> if len ( args ) < <NUM_LIT:2> : <EOL> raise template . TemplateSyntaxError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % tag_name ) <EOL> elif len ( args ) > <NUM_LIT:3> : <EOL> raise template . TemplateSyntaxError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % tag_name ) <EOL> dimensions , source = args [ - <NUM_LIT:2> : ] <EOL> generator_id = args [ <NUM_LIT:0> ] if len ( args ) > <NUM_LIT:2> else None <EOL> if varname : <EOL> return ThumbnailAssignmentNode ( varname , generator_id , dimensions , <EOL> source , kwargs ) <EOL> else : <EOL> return ThumbnailImageTagNode ( generator_id , dimensions , source , kwargs , <EOL> html_attrs ) <EOL> generateimage = register . tag ( generateimage ) <EOL> thumbnail = register . tag ( thumbnail ) </s>
