<s> from functools import partial <EOL> from django import template <EOL> from django . template . base import TagHelperNode , token_kwargs <EOL> from django . template . defaultfilters import safe <EOL> register = template . Library ( ) <EOL> class EmailContentTransform ( TagHelperNode ) : <EOL> def __init__ ( self , tag_name , parser , token ) : <EOL> self . tag_name = tag_name <EOL> bits = token . split_contents ( ) [ <NUM_LIT:1> : ] <EOL> self . kwargs = { k : v . var for k , v in token_kwargs ( bits , parser ) . items ( ) } <EOL> nodelist = parser . parse ( ( '<STR_LIT>' . format ( tag_name ) , ) ) <EOL> parser . delete_first_token ( ) <EOL> self . nodelist = nodelist <EOL> def render ( self , context ) : <EOL> email_template_namespace = context . get ( "<STR_LIT>" , "<STR_LIT:default>" ) <EOL> shard_ext = context . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> template_file = "<STR_LIT>" . format ( <EOL> email_template_namespace , <EOL> self . tag_name , <EOL> shard_ext <EOL> ) <EOL> t = context . template . engine . get_template ( template_file ) <EOL> content = self . nodelist . render ( context ) <EOL> with context . push ( content = content , ** self . kwargs ) : <EOL> rendered = t . render ( context ) <EOL> if shard_ext == "<STR_LIT:html>" : <EOL> rendered = safe ( rendered . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) ) <EOL> return rendered <EOL> email_tags = [ <EOL> "<STR_LIT:a>" , "<STR_LIT:p>" , "<STR_LIT>" , "<STR_LIT>" <EOL> ] <EOL> for tag_name in email_tags : <EOL> fn = partial ( EmailContentTransform , tag_name ) <EOL> register . tag ( name = tag_name ) ( fn ) <EOL> class EmailShardTypeNode ( template . Node ) : <EOL> def __init__ ( self , shard_type , nodelist ) : <EOL> self . shard_type = shard_type <EOL> self . nodelist = nodelist <EOL> def render ( self , context ) : <EOL> with context . push ( alert_shardtype = self . shard_type ) : <EOL> return self . nodelist . render ( context ) <EOL> @ register . tag <EOL> def shardtype ( parser , token ) : <EOL> try : <EOL> tag_name , shard_type = token . split_contents ( ) <EOL> except ValueError : <EOL> raise template . TemplateSyntaxError ( <EOL> "<STR_LIT>" % token . contents . split ( ) [ <NUM_LIT:0> ] <EOL> ) <EOL> if not ( shard_type [ <NUM_LIT:0> ] == shard_type [ - <NUM_LIT:1> ] and shard_type [ <NUM_LIT:0> ] in ( '<STR_LIT:">' , "<STR_LIT:'>" ) ) : <EOL> raise template . TemplateSyntaxError ( <EOL> "<STR_LIT>" % tag_name <EOL> ) <EOL> shard_type = shard_type [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> nodelist = parser . parse ( ( '<STR_LIT>' . format ( tag_name ) , ) ) <EOL> parser . delete_first_token ( ) <EOL> return EmailShardTypeNode ( shard_type , nodelist ) </s>
