Home Download Buy Blog Forum Support

Run selection through web service

Run selection through web service

Postby jbrooksuk on Mon Sep 05, 2011 10:23 am

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:
Code: Select all
cssComm = '"css=' + selectedCSS + "'"

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

Code: Select all
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

Cheers.
jbrooksuk
 
Posts: 781
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Run selection through web service

Postby wbond on Tue Sep 06, 2011 4:24 am

jbrooksuk wrote: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:
Code: Select all
cssComm = '"css=' + selectedCSS + "'"

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


I've actually been working on a Prefixr plugin over the past couple days and I just pushed it live at http://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 https://github.com/wbond/sublime_prefix ... Prefixr.py. Feel free to ask any questions!
wbond
 
Posts: 528
Joined: Mon Feb 28, 2011 5:33 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests