<s> from siteglass . mergemin import MergeMinBuilder <EOL> from cssmin import cssmin <EOL> import re <EOL> import os . path <EOL> import base64 <EOL> import urllib <EOL> class CSSBuilder ( MergeMinBuilder ) : <EOL> name = '<STR_LIT>' <EOL> import_pattern = re . compile ( '<STR_LIT>' ) <EOL> asset_types = [ <EOL> { <EOL> '<STR_LIT>' : re . compile ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : re . compile ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT>' : re . compile ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT>' : re . compile ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ] <EOL> def process_content ( self , path , content ) : <EOL> if self . options . get ( '<STR_LIT>' , True ) : <EOL> content = self . resolve_imports ( path , content ) <EOL> if self . options . get ( '<STR_LIT>' , True ) : <EOL> content = self . inline_assets ( path , content ) <EOL> return content <EOL> def resolve_imports ( self , path , content ) : <EOL> paths = { } <EOL> for statement , import_path in self . get_matches ( self . import_pattern , path , content ) : <EOL> if paths . get ( import_path ) : <EOL> continue <EOL> paths [ import_path ] = <NUM_LIT:1> <EOL> import_contents = self . get_text_file_contents ( import_path ) <EOL> import_contents = self . adjust_asset_paths ( import_path , path , import_contents ) <EOL> import_contents = self . resolve_imports ( import_path , import_contents ) <EOL> content = content . replace ( statement , import_contents ) <EOL> return content <EOL> def adjust_asset_paths ( self , old_path , new_path , content ) : <EOL> old_base_path = os . path . dirname ( old_path ) <EOL> new_base_path = os . path . dirname ( new_path ) <EOL> if old_base_path == new_base_path : <EOL> return content <EOL> for type in self . asset_types : <EOL> for statement , path in self . get_matches ( type [ '<STR_LIT>' ] , old_path , content ) : <EOL> asset_path = os . path . abspath ( path ) <EOL> content = content . replace ( <EOL> os . path . relpath ( asset_path , old_base_path ) , <EOL> os . path . relpath ( asset_path , new_base_path ) <EOL> ) <EOL> return content <EOL> def inline_assets ( self , base_path , content ) : <EOL> for type in self . asset_types : <EOL> for statement , path in self . get_matches ( type [ '<STR_LIT>' ] , base_path , content ) : <EOL> asset_content = self . get_binary_file_contents ( path ) <EOL> encoded_content = urllib . quote ( base64 . encodestring ( asset_content ) ) <EOL> new_statement = u'<STR_LIT>' % ( type [ '<STR_LIT>' ] , encoded_content ) <EOL> content = content . replace ( statement , new_statement ) <EOL> return content <EOL> def get_matches ( self , pattern , path , content ) : <EOL> if self . options . get ( '<STR_LIT>' ) == '<STR_LIT:target>' : <EOL> base_path = self . options . get ( '<STR_LIT:target>' , os . path . dirname ( path ) ) <EOL> else : <EOL> base_path = os . path . dirname ( os . path . abspath ( path ) ) <EOL> for match in pattern . finditer ( content ) : <EOL> yield match . group ( <NUM_LIT:0> ) , os . path . join ( base_path , match . group ( <NUM_LIT:1> ) ) <EOL> def minify ( self , contents ) : <EOL> return cssmin ( '<STR_LIT>' . join ( contents ) ) </s>
