Sublime Forum

Run selection through web service

#1

I’m trying to build a plugin for the web service Prefixr using cURL if it’s available on the system. I’m capable of getting the selections from all selection regions and concatenate them into one selection, however I’m then unable to concatenate them to a variable like:

cssComm = '"css=' + selectedCSS + "'"

I then have absolutely no idea how to take the output from cURL command and replace the selection with it :frowning:

[code]import sublime, sublime_plugin
import os

class Prefixr(sublime_plugin.TextCommand):
def run(self, edit):
if not self.view.file_name():
return

	selectedCSS = ""
	for region in self.view.sel():
		if not region.empty():
			selectedCSS += self.view.substr(region)

	cssComm = '"css='
	cssComm += selectedCSS
	cssComm += "'"
	print cssComm
	newCSS = self.view.window().run_command('exec', {'cmd': 'curl', '-sSd', cssComm, 'http://prefixr.com/api/index.php']})
	self.view.sel()[0] = newCSS[/code]

Cheers.

0 Likes

#2

[quote=“jbrooksuk”]I’m trying to build a plugin for the web service Prefixr using cURL if it’s available on the system. I’m capable of getting the selections from all selection regions and concatenate them into one selection, however I’m then unable to concatenate them to a variable like:

cssComm = '"css=' + selectedCSS + "'"

I then have absolutely no idea how to take the output from cURL command and replace the selection with it :frowning:[/quote]

I’ve actually been working on a Prefixr plugin over the past couple days and I just pushed it live at wbond.net/sublime_packages/prefixr. Of course, you can grab it through Package Control.

I ended up using urllib2 to do the requests so that the plugin would work on all platforms without any external dependencies. I did the requests in a thread so that if a user has a slow internet connection, the whole ST2 interface does not freeze.

The first step was to detect if the selection contained braces, and if so, I just passed the selected CSS to Prefix and used the view.replace() method to replace the original with the prefixed CSS. For selections that didn’t contain a brace I also did some whitespace gymnastics to get the Prefixr results to fit in place nicely.

You can check out the actual code at github.com/wbond/sublime_prefix … Prefixr.py. Feel free to ask any questions!

0 Likes