forked from condemil/gist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
119 lines (87 loc) · 2.94 KB
/
helpers.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
# -*- coding: utf-8 -*-
import os
import sys
import re
PY3 = sys.version >= '3'
#ST3 = sys.version >= '3'
#ST3 = int(sublime.version()) >= 3000
if PY3:
from .settings import *
else:
from settings import *
def gistify_view(view, gist, gist_filename):
statusline_string = "Gist: " + gist_title(gist)[0]
if not view.file_name():
view.set_name(gist_filename)
elif os.path.basename(view.file_name()) != gist_filename:
statusline_string = "%s (%s)" % (statusline_string, gist_filename)
view.settings().set('gist_html_url', gist["html_url"])
view.settings().set('gist_description', gist['description'])
view.settings().set('gist_url', gist["url"])
view.settings().set('gist_filename', gist_filename)
view.set_status("Gist", statusline_string)
def ungistify_view(view):
view.settings().erase('gist_html_url')
view.settings().erase('gist_description')
view.settings().erase('gist_url')
view.settings().erase('gist_filename')
view.erase_status("Gist")
def gist_title(gist):
description = gist.get('description')
if description and settings.get('prefer_filename') is False:
title = description
else:
title = list(gist['files'].keys())[0]
if settings.get('show_authors'):
return [title, gist.get('user').get('login')]
else:
return [title]
def gists_filter(all_gists):
prefix = settings.get('gist_prefix')
if prefix:
prefix_len = len(prefix)
if settings.get('gist_tag'):
tag_prog = re.compile('(^|\s)#' + re.escape(settings.get('gist_tag')) + '($|\s)')
else:
tag_prog = False
gists = []
gists_names = []
for gist in all_gists:
name = gist_title(gist)
if not gist['files']:
continue
if prefix:
if name[0][0:prefix_len] == prefix:
name[0] = name[0][prefix_len:] # remove prefix from name
else:
continue
if tag_prog:
match = re.search(tag_prog, name[0])
if match:
name[0] = name[0][0:match.start()] + name[0][match.end():]
else:
continue
gists.append(gist)
gists_names.append(name)
return [gists, gists_names]
def set_syntax(view, file_data):
if not "language" in file_data:
return
language = file_data['language']
if language is None:
return
if language == 'C':
new_syntax = os.path.join('C++', "{0}.tmLanguage".format(language))
else:
new_syntax = os.path.join(language, "{0}.tmLanguage".format(language))
if PY3:
new_syntax_path = os.path.join('Packages', new_syntax)
if os.name == 'nt':
new_syntax_path = new_syntax_path.replace('\\', '/')
else:
new_syntax_path = os.path.join(sublime.packages_path(), new_syntax)
try:
#print(new_syntax_path)
view.set_syntax_file(new_syntax_path)
except:
pass