<s> from django import template <EOL> from django . contrib . sites . models import Site <EOL> from django . conf import settings <EOL> from . . utils import string_to_dict , roundrobin <EOL> register = template . Library ( ) <EOL> @ register . tag <EOL> def opengraph_meta ( parser , token ) : <EOL> try : <EOL> tag_name , properties = token . split_contents ( ) <EOL> except ValueError : <EOL> raise template . TemplateSyntaxError ( <EOL> "<STR_LIT>" % token . contents . split ( ) [ <NUM_LIT:0> ] <EOL> ) <EOL> properties = string_to_dict ( properties [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> return OpenGraphNode ( properties ) <EOL> class OpenGraphNode ( template . Node ) : <EOL> def __init__ ( self , properties ) : <EOL> self . properties = properties <EOL> def render ( self , context ) : <EOL> site_domain = getattr ( settings , "<STR_LIT>" , None ) or Site . objects . get_current ( ) . domain <EOL> list_keys = [ ] <EOL> result_list = [ ] <EOL> for key , value in self . properties . items ( ) : <EOL> if type ( value ) == list : <EOL> list_keys . append ( [ ( key , item ) for item in value ] ) <EOL> else : <EOL> result_list . append ( self . render_tag ( key , value , context ) ) <EOL> if len ( list_keys ) > <NUM_LIT:0> : <EOL> for item in roundrobin ( * list_keys ) : <EOL> result_list . append ( self . render_tag ( item [ <NUM_LIT:0> ] , item [ <NUM_LIT:1> ] , context ) ) <EOL> return u'<STR_LIT:\n>' . join ( result_list ) <EOL> def render_tag ( self , key , value , context ) : <EOL> try : <EOL> value = template . Variable ( value ) . resolve ( context ) <EOL> except template . base . VariableDoesNotExist : <EOL> pass <EOL> og_layout = u'<STR_LIT>' <EOL> value = value . replace ( '<STR_LIT:">' , '<STR_LIT:U+0020>' ) <EOL> key = key . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> if key in [ u'<STR_LIT:url>' , u'<STR_LIT:image>' , u'<STR_LIT>' , u'<STR_LIT>' ] and value and value [ <NUM_LIT:0> ] == u'<STR_LIT:/>' : <EOL> value = u'<STR_LIT>' . format ( site_domain , value ) <EOL> og_formatted = og_layout . format ( key , value ) <EOL> return og_formatted </s>
