<s> from __future__ import absolute_import <EOL> import io <EOL> import jinja2 <EOL> import jinja2 . ext <EOL> from jinja2 import nodes <EOL> from jinja2 . ext import Extension <EOL> from jinja2 . nodes import CallBlock , Call , ExtensionAttribute <EOL> from compressor . exceptions import TemplateSyntaxError , TemplateDoesNotExist <EOL> def flatten_context ( context ) : <EOL> if hasattr ( context , '<STR_LIT>' ) : <EOL> context_dict = { } <EOL> for d in context . dicts : <EOL> context_dict . update ( d ) <EOL> return context_dict <EOL> return context <EOL> class SpacelessExtension ( Extension ) : <EOL> """<STR_LIT>""" <EOL> tags = set ( [ '<STR_LIT>' ] ) <EOL> def parse ( self , parser ) : <EOL> lineno = next ( parser . stream ) . lineno <EOL> body = parser . parse_statements ( [ '<STR_LIT>' ] , drop_needle = True ) <EOL> return nodes . CallBlock ( self . call_method ( '<STR_LIT>' , [ ] ) , <EOL> [ ] , [ ] , body ) . set_lineno ( lineno ) <EOL> def _spaceless ( self , caller ) : <EOL> from django . utils . html import strip_spaces_between_tags <EOL> return strip_spaces_between_tags ( caller ( ) . strip ( ) ) <EOL> def url_for ( mod , filename ) : <EOL> """<STR_LIT>""" <EOL> from django . contrib . staticfiles . templatetags import staticfiles <EOL> if mod == "<STR_LIT>" : <EOL> return staticfiles . static ( filename ) <EOL> return "<STR_LIT>" <EOL> class Jinja2Parser ( object ) : <EOL> COMPRESSOR_ID = '<STR_LIT>' <EOL> def __init__ ( self , charset , env ) : <EOL> self . charset = charset <EOL> self . env = env <EOL> def parse ( self , template_name ) : <EOL> with io . open ( template_name , mode = '<STR_LIT:rb>' ) as file : <EOL> try : <EOL> template = self . env . parse ( file . read ( ) . decode ( self . charset ) ) <EOL> except jinja2 . TemplateSyntaxError as e : <EOL> raise TemplateSyntaxError ( str ( e ) ) <EOL> except jinja2 . TemplateNotFound as e : <EOL> raise TemplateDoesNotExist ( str ( e ) ) <EOL> return template <EOL> def process_template ( self , template , context ) : <EOL> return True <EOL> def get_init_context ( self , offline_context ) : <EOL> context = self . env . globals . copy ( ) <EOL> context . update ( offline_context ) <EOL> return context <EOL> def process_node ( self , template , context , node ) : <EOL> pass <EOL> def _render_nodes ( self , template , context , nodes ) : <EOL> compiled_node = self . env . compile ( jinja2 . nodes . Template ( nodes ) ) <EOL> template = jinja2 . Template . from_code ( self . env , compiled_node , { } ) <EOL> flat_context = flatten_context ( context ) <EOL> return template . render ( flat_context ) <EOL> def render_nodelist ( self , template , context , node ) : <EOL> return self . _render_nodes ( template , context , node . body ) <EOL> def render_node ( self , template , context , node ) : <EOL> return self . _render_nodes ( template , context , [ node ] ) <EOL> def get_nodelist ( self , node ) : <EOL> body = getattr ( node , "<STR_LIT:body>" , getattr ( node , "<STR_LIT>" , [ ] ) ) <EOL> if isinstance ( node , jinja2 . nodes . If ) : <EOL> return body + node . else_ <EOL> return body <EOL> def walk_nodes ( self , node , block_name = None ) : <EOL> for node in self . get_nodelist ( node ) : <EOL> if ( isinstance ( node , CallBlock ) and <EOL> isinstance ( node . call , Call ) and <EOL> isinstance ( node . call . node , ExtensionAttribute ) and <EOL> node . call . node . identifier == self . COMPRESSOR_ID ) : <EOL> node . call . node . name = '<STR_LIT>' <EOL> yield node <EOL> else : <EOL> for node in self . walk_nodes ( node , block_name = block_name ) : <EOL> yield node </s>
