From 98952dcd400fa4caf80856c8aeafb8f4448c6571 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 6 Jun 2012 23:17:09 +0300 Subject: [PATCH 1/4] Add command to run Prefixr --- Default.sublime-commands | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Default.sublime-commands b/Default.sublime-commands index ce205f5..5f896c0 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -46,5 +46,9 @@ "file": "${packages}/User/Default (Linux).sublime-keymap", "platform": "Linux" } + }, + { + "caption": "Prefixr", + "command": "prefixr" } ] \ No newline at end of file From 449b602b87180210d65b1122dc6001dc4e5f6097 Mon Sep 17 00:00:00 2001 From: Andrew Schmadel Date: Wed, 27 Mar 2013 14:43:23 -0400 Subject: [PATCH 2/4] Add brackets to work around prefixr bug Adds brackets around all styles before sending them to the Prefixr api. Prefixr has a bug that occasionally clobbers any styles preceeding the ones being prefixed. Until that gets fixed, this patch should allow the plugin to work correctly... --- Prefixr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Prefixr.py b/Prefixr.py index 3fccaa8..d73a81f 100644 --- a/Prefixr.py +++ b/Prefixr.py @@ -152,11 +152,11 @@ def __init__(self, sel, string, timeout): def run(self): try: - data = urllib.urlencode({'css': self.original}) + data = urllib.urlencode({'css': '{%s}' % (self.original)}) request = urllib2.Request('http://prefixr.com/api/index.php', data, headers={"User-Agent": "Sublime Prefixr"}) http_file = urllib2.urlopen(request, timeout=self.timeout) - self.result = http_file.read() + self.result = http_file.read().replace('{', '').replace('}', '') return except (urllib2.HTTPError) as (e): From 620362b2bd363d1beadf144d4653938d979fa74f Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 11 Jul 2013 12:09:34 -0400 Subject: [PATCH 3/4] Add support for ST3 Adds support for Sublime Text 3 and Python3. --- Prefixr.py | 56 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/Prefixr.py b/Prefixr.py index d73a81f..e13bb86 100644 --- a/Prefixr.py +++ b/Prefixr.py @@ -1,10 +1,14 @@ import sublime import sublime_plugin -import urllib -import urllib2 import threading import re - +try: + from urllib.request import Request, urlopen + from urllib.parse import urlencode + from urllib.error import HTTPError, URLError +except ImportError: + from urllib import urlencode + from urllib2 import Request, HTTPError, URLError, urlopen class PrefixrCommand(sublime_plugin.TextCommand): def run(self, edit): @@ -38,21 +42,18 @@ def run(self, edit): # We clear all selection because we are going to manually set them self.view.sel().clear() - - # This creates an edit group so we can undo all changes in one go - edit = self.view.begin_edit('prefixr') - self.handle_threads(edit, threads, braces) def handle_threads(self, edit, threads, braces, offset=0, i=0, dir=1): next_threads = [] + finished_threads = [] for thread in threads: if thread.is_alive(): next_threads.append(thread) continue if thread.result == False: continue - offset = self.replace(edit, thread, braces, offset) + finished_threads.append(thread) threads = next_threads if len(threads): @@ -71,17 +72,31 @@ def handle_threads(self, edit, threads, braces, offset=0, i=0, dir=1): braces, offset, i, dir), 100) return - self.view.end_edit(edit) - + replacements = [] + for thread in finished_threads: + replacements.append({"begin": thread.sel.begin(), + "end":thread.sel.end(), + "original": thread.original, + "result": thread.result.decode('utf8')}) + args = {"braces": braces, "replacements": replacements} + self.view.run_command("prefixrreplace",args) self.view.erase_status('prefixr') selections = len(self.view.sel()) sublime.status_message('Prefixr successfully run on %s selection%s' % (selections, '' if selections == 1 else 's')) - def replace(self, edit, thread, braces, offset): - sel = thread.sel - original = thread.original - result = thread.result +class PrefixrreplaceCommand(sublime_plugin.TextCommand): + def run(self, edit, **args): + offset = 0 + braces = args['braces'] + for replacement in args['replacements']: + offset = self.replace(edit, replacement, braces, offset) + return + + def replace(self, edit, replacement, braces, offset): + sel = sublime.Region(replacement['begin'],replacement['end']) + original = replacement['original'] + result = replacement['result'] # Here we adjust each selection for any text we have already inserted if offset: @@ -152,16 +167,17 @@ def __init__(self, sel, string, timeout): def run(self): try: - data = urllib.urlencode({'css': '{%s}' % (self.original)}) - request = urllib2.Request('http://prefixr.com/api/index.php', data, + data = urlencode({'css': self.original}) + data = data.encode('utf8') + request = Request('http://prefixr.com/api/index.php', data, headers={"User-Agent": "Sublime Prefixr"}) - http_file = urllib2.urlopen(request, timeout=self.timeout) - self.result = http_file.read().replace('{', '').replace('}', '') + http_file = urlopen(request, timeout=self.timeout) + self.result = http_file.read() return - except (urllib2.HTTPError) as (e): + except (HTTPError) as e: err = '%s: HTTP error %s contacting API' % (__name__, str(e.code)) - except (urllib2.URLError) as (e): + except (URLError) as e: err = '%s: URL error %s contacting API' % (__name__, str(e.reason)) sublime.error_message(err) From 62e67a00354329f1bff6391e4a3ca533c5976c45 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 8 Aug 2013 10:48:24 -0400 Subject: [PATCH 4/4] Fix bracket-adding code Fixes made in 449b602b87180210d65b1122dc6001dc4e5f6097 were accidentally undone in 620362b2bd363d1beadf144d4653938d979fa74f . --- Prefixr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Prefixr.py b/Prefixr.py index e13bb86..6d3a2b3 100644 --- a/Prefixr.py +++ b/Prefixr.py @@ -167,12 +167,12 @@ def __init__(self, sel, string, timeout): def run(self): try: - data = urlencode({'css': self.original}) + data = urlencode({'css': '{%s}' % (self.original)}) data = data.encode('utf8') request = Request('http://prefixr.com/api/index.php', data, headers={"User-Agent": "Sublime Prefixr"}) http_file = urlopen(request, timeout=self.timeout) - self.result = http_file.read() + self.result = http_file.read().replace('{', '').replace('}', '') return except (HTTPError) as e: