forked from cgutierrez/JsMinifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8bbfe4d
Showing
7 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
{ | ||
"keys": ["ctrl+alt+m"], "command": "minify" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
{ | ||
"keys": ["ctrl+alt+m"], "command": "minify" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
{ | ||
"keys": ["ctrl+alt+m"], "command": "minify" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ | ||
"caption": "Minify Javascript", | ||
"command": "minify" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import sublime, sublime_plugin | ||
from GoogleClosureCall import GoogleClosureCall | ||
|
||
class Minify(sublime_plugin.TextCommand): | ||
def run(self, edit): | ||
print edit | ||
selections = self.view.sel() | ||
|
||
# check if the user has any actual selections | ||
has_selections = False | ||
for sel in selections: | ||
if sel.empty() == False: | ||
has_selections = True | ||
|
||
# if not, add the entire file as a selection | ||
if not has_selections: | ||
full_region = sublime.Region(0, self.view.size()) | ||
selections.add(full_region) | ||
|
||
|
||
threads = [] | ||
for sel in selections: | ||
selbody = self.view.substr(sel) | ||
thread = GoogleClosureCall(sel, selbody, 5) | ||
threads.append(thread) | ||
thread.start() | ||
|
||
selections.clear() | ||
editgroup = self.view.begin_edit('minify') | ||
self.handle_threads(edit, threads, selections, offset = 0, i = 0, dir = 1) | ||
|
||
def handle_threads(self, edit, threads, selections, offset = 0, i = 0, dir = 1): | ||
print "handling threads" | ||
next_threads = [] | ||
for thread in threads: | ||
if thread.is_alive(): | ||
next_threads.append(thread) | ||
continue | ||
if thread.result == False: | ||
continue | ||
offset = self.replace(edit, thread, selections, offset) | ||
threads = next_threads | ||
|
||
if len(threads): | ||
before = i % 8 | ||
after = (7) - before | ||
|
||
if not after: | ||
dir = -1 | ||
if not before: | ||
dir = 1 | ||
|
||
i += dir | ||
|
||
self.view.set_status('minify', '[%s=%s]' % (' ' * before, ' ' * after)) | ||
|
||
sublime.set_timeout(lambda: self.handle_threads(edit, threads, selections, offset, i, dir), 100) | ||
return | ||
|
||
self.view.end_edit(edit) | ||
self.view.erase_status('minify') | ||
sublime.status_message('Successfully minified') | ||
|
||
def replace(self, edit, thread, selections, offset): | ||
sel = thread.sel | ||
original = thread.original | ||
result = thread.result | ||
|
||
if offset: | ||
sel = sublime.Region(sel.begin() + offset, sel.end() + offset) | ||
|
||
self.view.replace(edit, sel, result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import sublime, sublime_plugin | ||
import urllib | ||
import urllib2 | ||
import threading | ||
|
||
class GoogleClosureCall(threading.Thread): | ||
def __init__(self, sel, string, timeout): | ||
self.sel = sel | ||
self.original = string | ||
self.timeout = timeout | ||
self.result = None | ||
threading.Thread.__init__(self) | ||
|
||
def run(self): | ||
try: | ||
data = urllib.urlencode({ | ||
'js_code': self.original, | ||
'compilation_level': "WHITESPACE_ONLY", | ||
'output_info': "compiled_code" }) | ||
|
||
ua = 'Sublime Text - Google Closure' | ||
req = urllib2.Request("http://closure-compiler.appspot.com/compile", data, headers = { 'User-Agent': ua }) | ||
file = urllib2.urlopen(req, timeout = self.timeout) | ||
self.result = file.read() | ||
return | ||
except (urllib2.HTTPError) as (e): | ||
err = '%s: HTTP error %s contacting API' % (__name__, str(e.code)) | ||
except (urllib2.URLError) as (e): | ||
err = '%s: URL error %s contacting API' % (__name__, str(e.code)) | ||
|
||
sublime.error_message(err) | ||
self.result = False |