Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sublime, sublime_plugin
- import re , sys
- # importante nome da Class tem de ser igual "command": "css_tominifier",
- class CssTominifierCommand ( sublime_plugin.TextCommand ) :
- # activar ou desactivar botao caso seja um dos files que pretendo
- def is_enabled(self):
- return run()
- def run ( self , edit ) :
- selections = self.view.sel()
- file_types_to_minify = ['js', 'css']
- not_minify = ['min']
- file_name_parts = self.view.file_name().split('.')
- current_file_and_path = self.view.file_name()
- SplitWhat = '\\' if sys.platform == 'win32' else '/'
- parts_of_file_and_path = self.view.file_name().split( SplitWhat )
- current_file = parts_of_file_and_path[-1]
- current_file_name = current_file.split('.')[0]
- current_file_size = self.view.size()
- current_file_ext = file_name_parts[-1]
- current_path = current_file_and_path.replace ( current_file , "" )
- full_file = current_path + current_file_name + '.min.' + current_file_ext
- # Files to exame css and js only
- if current_file_ext in file_types_to_minify :
- with open( current_file_and_path ) as file_in:
- lines = []
- for line in file_in:
- line = re.sub ( r'\s*\+\s*' , '+' , line )
- line = re.sub ( r'\s*>\s*' , '>' , line )
- line = re.sub ( r'\s*{\s*' , '{' , line )
- line = re.sub ( r'\s*}\s*' , '}' , line )
- line = re.sub ( r'\s*\(\s*' , '(' , line )
- line = re.sub ( r'\s*\)\s*' , ')' , line )
- line = re.sub ( r'\s*,\s*' , ',' , line )
- line = re.sub ( r'\s*!\s*' , '!' , line )
- line = re.sub ( r'\s*:\s*' , ':' , line )
- line = re.sub ( r'\s*;\s*' , ';' , line )
- line = re.sub ( r'\s*\*\s*' , '*' , line )
- line = re.sub ( r'\t|\n|\r|\s\s+|\/(.+)\/' , '' , line )
- lines.append(line)
- # agora vou meter tudo numa so linha
- clean = ''.join ( l for l in lines )
- # Criar novo ficheiro min
- if sublime.version() < '3':
- with open( full_file , 'w+' ) as min_file:
- min_file.write( clean )
- else:
- with open(full_file, 'wb+' ) as min_file:
- min_file.write ( bytes ( clean , 'utf-8' ) )
- # abrir o novo ficheiro numa nova tab
- self.view.window().open_file( full_file )
Add Comment
Please, Sign In to add comment