├── Default.sublime-keymap ├── README.md └── pastebin.py /Default.sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { "keys": ["ctrl+alt+c"], "command": "pastebin" } 3 | ] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Sublime Pastebin Plugin 2 | ====================== 3 | 4 | This is a simple plugin for [Sublime Text](http://www.sublimetext.com) to allow for easily pasting code to a pastebin 5 | 6 | It takes the current selection in a file, uploads it to (by default) [upl.io](https://upl.io), and returns the URL to the clipboard. 7 | 8 | Support 9 | ------- 10 | Sublime Pastebin Plugin supports both [Sublime Text 2](http://www.sublimetext.com/2) and [Sublime Text 3](http://www.sublimetext.com/3) 11 | 12 | Installation 13 | ------------ 14 | 15 | Clone this repo into your Sublime Text Packages directory 16 | (replace 2 with 3 if you use sublime text 3) 17 | ###Linux 18 | cd ~/.config/sublime-text-2/Packages/ 19 | git clone git://github.com/Paaskehare/pastebin-sublime-plugin.git 20 | 21 | ###Mac 22 | cd ~/"Library/Application Support/Sublime Text 2/Packages/" 23 | git clone git://github.com/Paaskehare/pastebin-sublime-plugin.git 24 | 25 | Usage 26 | ----- 27 | 28 | Select some text (or don't) and hit `ctrl + alt + c` and afterwards `ctrl + v` - and you're good to go! 29 | 30 | *Good luck!* 31 | -------------------------------------------------------------------------------- /pastebin.py: -------------------------------------------------------------------------------- 1 | import sublime, sublime_plugin 2 | 3 | import string, random, os.path 4 | 5 | # Python 2 6 | try: 7 | import urllib2 8 | 9 | # Python 3 10 | except ImportError: 11 | import urllib.request as urllib2 12 | 13 | HOSTNAME = 'http://http.upl.io' 14 | POST_FILE_FIELD = 'file' 15 | 16 | class FilePart: 17 | 18 | CONTENT_TYPE = 'application/text-plain;charset=utf-8;' 19 | CONTENT_TRANSFER_ENCODING = 'utf-8' 20 | 21 | def __init__(self, name, filename, body, boundary): 22 | 23 | self.name = name 24 | self.filename = filename 25 | self.body = body 26 | self.boundary = boundary 27 | 28 | self.headers = { 29 | 'Content-Type': self.CONTENT_TYPE, 30 | 'Content-Disposition': 'form-data; name="{0}"; filename="{1}"'.format( 31 | self.name, self.filename 32 | ), 33 | 'Content-Transfer-Encoding': self.CONTENT_TRANSFER_ENCODING, 34 | } 35 | 36 | def get(self): 37 | lines = [] 38 | lines.append('--' + self.boundary) 39 | 40 | for key, val in self.headers.items(): 41 | lines.append('{0}: {1}'.format(key, val)) 42 | 43 | lines.append('') 44 | lines.append(self.body) 45 | lines.append('--{0}--'.format(self.boundary)) 46 | lines.append('') 47 | 48 | return lines 49 | 50 | class FileForm: 51 | 52 | NEWLINE = '\r\n' 53 | 54 | # Generate a random boundary 55 | def _gen_boundary(self): 56 | chars = string.ascii_lowercase + string.digits 57 | return ''.join(random.choice(chars) for x in range(40)) 58 | 59 | def __init__(self): 60 | self.boundary = self._gen_boundary() 61 | self._file = None 62 | 63 | def file(self, filename, content): 64 | self._file = FilePart(POST_FILE_FIELD, filename, content, self.boundary) 65 | 66 | def get(self): 67 | # file returns an array 68 | content = self._file.get() 69 | 70 | content_type = 'multipart/form-data; boundary=' + self.boundary 71 | 72 | return content_type, self.NEWLINE.join(content).encode(FilePart.CONTENT_TRANSFER_ENCODING) 73 | 74 | class PastebinCommand(sublime_plugin.TextCommand): 75 | 76 | def _get_file_name(self): 77 | name = "untitled" 78 | try: 79 | name = os.path.split(self.view.file_name())[-1] 80 | except AttributeError: 81 | pass 82 | except TypeError: 83 | pass 84 | return name 85 | 86 | def run(self, edit): 87 | 88 | # init an empty unicode string 89 | content = u'' 90 | 91 | # loop over the selections in the view: 92 | for region in self.view.sel(): 93 | 94 | if not region.empty(): 95 | # be sure to insert a newline if we have multiple selections 96 | if content: 97 | content += FileForm.NEWLINE 98 | content += self.view.substr(region) 99 | 100 | # if we havent gotten data from selected text, 101 | # we assume the entire file should be pasted: 102 | if not content: 103 | content += self.view.substr(sublime.Region(0, self.view.size())) 104 | 105 | filename = self._get_file_name() 106 | 107 | form = FileForm() 108 | 109 | # insert the "fake" file 110 | form.file(filename = filename, content = content) 111 | 112 | content_type, body = form.get() 113 | 114 | request = urllib2.Request(url=HOSTNAME, headers={'Content-Type': content_type}, data=body) 115 | reply = urllib2.urlopen(request).read().decode(FilePart.CONTENT_TRANSFER_ENCODING) 116 | 117 | sublime.set_clipboard(reply) 118 | sublime.status_message("Paste: " + reply) 119 | --------------------------------------------------------------------------------