-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSendToPasteBin.py
161 lines (137 loc) · 6.02 KB
/
SendToPasteBin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import re
import sublime
import sublime_plugin
import sys
import threading
# Ensure it works for python 2 and 3
try:
from urllib.parse import urlencode
from urllib.request import urlopen
except ImportError:
from urllib import urlencode, urlopen
ROOT_URL = "https://pastebin.com"
API_URL = ROOT_URL + "/api/api_post.php"
class SendToPasteBinPromptCommand(sublime_plugin.WindowCommand):
"""Show a window to allow the user to setup the Paste options"""
def run(self):
self.window.show_input_panel(
"Paste Name:", "", self.on_done, None, None)
def on_done(self, paste_name):
if self.window.active_view():
self.window.active_view().run_command(
"send_to_paste_bin", {"paste_name": paste_name})
class SendToPasteBinCommand(sublime_plugin.TextCommand):
def run(self, view, paste_name=None):
self.settings = sublime.load_settings(
"SendToPasteBin.sublime-settings")
# These are all the syntaxes supported by PasteBin currently (https://pastebin.com/api)
syntaxes = {
'ActionScript.sublime-syntax': 'actionscript',
'AppleScript.sublime-syntax': 'applescript',
'ASP.sublime-syntax': 'asp',
'Bibtex.sublime-syntax': 'bibtex',
'C.sublime-syntax': 'c',
'C#.sublime-syntax': 'csharp',
'C++.sublime-syntax': 'cpp',
'Clojure.sublime-syntax': 'clojure',
'CoffeeScript.sublime-syntax': 'coffeescript',
'CSS.sublime-syntax': 'css',
'D.sublime-syntax': 'd',
'Diff.sublime-syntax': 'diff',
'DOT.sublime-syntax': 'dot',
'Erlang.sublime-syntax': 'erlang',
'Go.sublime-syntax': 'go',
'Groovy.sublime-syntax': 'groovy',
'Haskell.sublime-syntax': 'haskell',
'HTML.sublime-syntax': 'html5',
'Java.sublime-syntax': 'java',
'JavaScript.sublime-syntax': 'javascript',
'JSON.sublime-syntax': 'javascript',
'JSON Generic Array Elements.sublime-syntax': 'javascript',
'LaTeX.sublime-syntax': 'latex',
'LaTeX Beamer.sublime-syntax': 'latex',
'LaTeX Memoir.sublime-syntax': 'latex',
'Lisp.sublime-syntax': 'lisp',
'Literate Haskell.sublime-syntax': 'haskell',
'Lua.sublime-syntax': 'lua',
'Makefile.sublime-syntax': 'make',
'Matlab.sublime-syntax': 'matlab',
'Objective-C.sublime-syntax': 'objc',
'Objective-C++.sublime-syntax': 'objc',
'OCaml.sublime-syntax': 'ocaml',
'OCamllex.sublime-syntax': 'ocaml',
'OCamlyacc.sublime-syntax': 'ocaml',
'Perl.sublime-syntax': 'perl',
'PHP.sublime-syntax': 'php',
'Plain text.sublime-syntax': 'text',
'Python.sublime-syntax': 'python',
'R.sublime-syntax': 'rsplus',
'R Console.sublime-syntax': 'rsplus',
'Regular Expressions (Python).sublime-syntax': 'python',
'Ruby.sublime-syntax': 'ruby',
'Ruby Haml.sublime-syntax': 'ruby',
'Ruby on Rails.sublime-syntax': 'rails',
'Scala.sublime-syntax': 'scala',
'SCSS.sublime-syntax': 'css',
'Shell-Unix-Generic.sublime-syntax': 'bash',
'SQL.sublime-syntax': 'sql',
'SQL (Rails).sublime-syntax': 'sql',
'Tcl.sublime-syntax': 'tcl',
'TeX.sublime-syntax': 'latex',
'TeX Math.sublime-syntax': 'latex',
'Textile.sublime-syntax': 'latex',
'XML.sublime-syntax': 'xml',
'YAML.sublime-syntax': 'yaml'
}
# Use the filename as a default paste name
if paste_name is None:
paste_name = self.view.file_name()
# Check if file exists on disk
if paste_name is not None:
# Only use the basename (we don't care about the path)
paste_name = os.path.basename(paste_name)
else:
paste_name = "Untitled"
# Manage the user selected text
for region in self.view.sel():
syntax = syntaxes.get(self.view.settings().get(
'syntax').split('/')[-1], 'text')
text = self.view.substr(region)
if not text:
sublime.status_message(
'Error sending to PasteBin: Nothing selected')
else:
args = {
'api_dev_key': self.settings.get("api_dev_key"),
'api_user_key': self.settings.get("api_user_key"),
'api_paste_expire_date': self.settings.get("paste_expiration"),
'api_paste_private': self.settings.get("paste_privacy"),
'api_paste_code': text,
'api_option': 'paste',
'api_paste_format': syntax,
'api_paste_name': paste_name
}
# Use a background thread to avoid freezing the main thread
thread = PasteBinApiCall(args)
thread.start()
class PasteBinApiCall(threading.Thread):
"""Manages the call to PasteBin's API.
Used to be able to do the call in another thread."""
def __init__(self, call_args):
self.call_args = call_args
threading.Thread.__init__(self)
self.settings = sublime.load_settings(
"SendToPasteBin.sublime-settings")
def run(self):
sublime.status_message('Sending to PasteBin ...')
response = urlopen(
url=API_URL,
data=urlencode(self.call_args).encode('utf8')
).read().decode('utf8')
url_regex = '(.*//pastebin\.com/)(.*$)'
paste_key = re.match(url_regex, response, flags=re.IGNORECASE).group(2)
if self.settings.get("paste_url_type") == "raw":
response = ROOT_URL + "/raw/" + paste_key
sublime.set_clipboard(response)
sublime.status_message('PasteBin URL copied to clipboard: ' + response)