<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> from django import template <EOL> from django . template import Node , Variable <EOL> from django . utils . encoding import smart_unicode <EOL> from django . template . defaulttags import url <EOL> from django . template import VariableDoesNotExist <EOL> from django . utils . translation import ugettext as _ <EOL> register = template . Library ( ) <EOL> def create_crumb ( title , url = None ) : <EOL> """<STR_LIT>""" <EOL> if url : <EOL> crumb = '<STR_LIT>' % ( url , title ) <EOL> else : <EOL> crumb = '<STR_LIT>' % ( title , ) <EOL> return crumb <EOL> def create_crumb_first ( title , url = None ) : <EOL> """<STR_LIT>""" <EOL> if url : <EOL> crumb = '<STR_LIT>' % ( url , title ) <EOL> else : <EOL> crumb = '<STR_LIT>' % ( title , ) <EOL> return crumb <EOL> @ register . tag <EOL> def breadcrumb ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return BreadcrumbNode ( token . split_contents ( ) [ <NUM_LIT:1> : ] ) <EOL> @ register . tag <EOL> def breadcrumb_root ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return BreadcrumbNode ( token . split_contents ( ) [ <NUM_LIT:1> : ] , create_crumb_first ) <EOL> @ register . tag <EOL> def breadcrumb_url ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if len ( bits ) == <NUM_LIT:2> : <EOL> return breadcrumb ( parser , token ) <EOL> title = bits . pop ( <NUM_LIT:1> ) <EOL> token . contents = '<STR_LIT:U+0020>' . join ( bits ) <EOL> url_node = url ( parser , token ) <EOL> return UrlBreadcrumbNode ( title , url_node ) <EOL> @ register . tag <EOL> def breadcrumb_url_root ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if len ( bits ) == <NUM_LIT:2> : <EOL> return breadcrumb ( parser , token ) <EOL> title = bits . pop ( <NUM_LIT:1> ) <EOL> token . contents = '<STR_LIT:U+0020>' . join ( bits ) <EOL> url_node = url ( parser , token ) <EOL> return UrlBreadcrumbNode ( title , url_node , create_crumb_first ) <EOL> class BreadcrumbNode ( Node ) : <EOL> def __init__ ( self , vars , render_func = create_crumb ) : <EOL> """<STR_LIT>""" <EOL> self . vars = map ( Variable , vars ) <EOL> self . render_func = render_func <EOL> def render ( self , context ) : <EOL> title = self . vars [ <NUM_LIT:0> ] . var <EOL> if title . find ( "<STR_LIT:'>" ) == - <NUM_LIT:1> and title . find ( '<STR_LIT:">' ) == - <NUM_LIT:1> : <EOL> try : <EOL> val = self . vars [ <NUM_LIT:0> ] <EOL> title = val . resolve ( context ) <EOL> except : <EOL> title = '<STR_LIT>' <EOL> else : <EOL> title = title . strip ( "<STR_LIT:'>" ) . strip ( '<STR_LIT:">' ) <EOL> title = smart_unicode ( title ) <EOL> url = None <EOL> if len ( self . vars ) > <NUM_LIT:1> : <EOL> val = self . vars [ <NUM_LIT:1> ] <EOL> try : <EOL> url = val . resolve ( context ) <EOL> except VariableDoesNotExist : <EOL> print '<STR_LIT>' , val <EOL> url = None <EOL> title = _ ( title ) <EOL> return self . render_func ( title , url ) <EOL> class UrlBreadcrumbNode ( Node ) : <EOL> def __init__ ( self , title , url_node , render_func = create_crumb ) : <EOL> self . title = Variable ( title ) <EOL> self . url_node = url_node <EOL> self . render_func = render_func <EOL> def render ( self , context ) : <EOL> title = self . title . var <EOL> if title . find ( "<STR_LIT:'>" ) == - <NUM_LIT:1> and title . find ( '<STR_LIT:">' ) == - <NUM_LIT:1> : <EOL> try : <EOL> val = self . title <EOL> title = val . resolve ( context ) <EOL> except : <EOL> title = '<STR_LIT>' <EOL> else : <EOL> title = title . strip ( "<STR_LIT:'>" ) . strip ( '<STR_LIT:">' ) <EOL> title = smart_unicode ( title ) <EOL> url = self . url_node . render ( context ) <EOL> title = _ ( title ) <EOL> return self . render_func ( title , url ) </s>
