-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsnipt.py
executable file
·60 lines (49 loc) · 2.45 KB
/
snipt.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
import sublime, sublime_plugin, urllib2, json, os, re
class SyncSniptCommand(sublime_plugin.TextCommand):
def get_username(self):
# snipt plugin settings
self.settings = sublime.load_settings("Snipt.sublime-settings")
self.username = self.settings.get('snipt_username')
def run(self, edit):
# check for username config
self.get_username()
username = self.username
if (not self.username):
sublime.error_message('No snipt.net username. You must first set you username in: Sublime Text2 ~> Preferences ~> Package Settings ~> Snipt Tools ~> Settings')
else:
# grab the user data
try:
response = urllib2.urlopen('http://snipt.net/api/users/%s.json' % username)
except urllib2.URLError, (err):
sublime.error_message("Connection refused. Try again later. Snipt step: 1")
return
# grab all user snipt #'s
parse = json.load(response)
parse_me = parse['snipts']
# run the loop
for item in parse_me:
# grab the user url and parse the snipts
try:
response = urllib2.urlopen('http://beta.snipt.net/api/public/snipt/%s/?format=json' % item)
except urllib2.URLError, (err):
sublime.error_message("Connection refused. Try again later. Snipt step: 2")
return
# parse the response for code snipt
parse = json.load(response)
code = parse['code']
clean = 'CDATA'
# cleaning CDATA
if not clean in code:
# cleaning the title for filename
title = parse['title']
rx = re.compile('\W+')
cleantitle = rx.sub(' ', title).strip()
# lets turn wine (snipts) into water (sublime snippets)
buildfile = 'repo/%s.sublime-snippet' % cleantitle[0:20]
newfile = open(buildfile,'w+')
newfile.write('<snippet><content><![CDATA[%s]]></content><tabTrigger>snipt</tabTrigger></snippet>' % code)
newfile.close()
# This is coming soon. Ability to send text to snipt.net and create a sublime snippet.
# class CreateSniptCommand(sublime_plugin.TextCommand):
# def run(self, edit):
# print 1