-
Notifications
You must be signed in to change notification settings - Fork 1
/
support.py
235 lines (194 loc) · 7.3 KB
/
support.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""Support command."""
import sublime
import sublime_plugin
import textwrap
import webbrowser
import re
__version__ = "1.5.1"
__pc_name__ = 'SubNotify'
CSS = '''
div.sub-notify { padding: 10px; margin: 0; }
.sub-notify h1, .sub-notify h2, .sub-notify h3,
.sub-notify h4, .sub-notify h5, .sub-notify h6 {
{{'.string'|css}}
}
.sub-notify blockquote { {{'.comment'|css}} }
.sub-notify a { text-decoration: none; }
'''
frontmatter = {
"markdown_extensions": [
"markdown.extensions.admonition",
"markdown.extensions.attr_list",
"markdown.extensions.def_list",
"markdown.extensions.nl2br",
# Smart quotes always have corner cases that annoy me, so don't bother with them.
{"markdown.extensions.smarty": {"smart_quotes": False}},
"pymdownx.betterem",
{
"pymdownx.magiclink": {
"repo_url_shortener": True,
"repo_url_shorthand": True,
"user": "facelessuser",
"repo": "SubNotify"
}
},
"pymdownx.extrarawhtml",
"pymdownx.keys",
{"pymdownx.escapeall": {"hardbreak": True, "nbsp": True}},
# Sublime doesn't support superscript, so no ordinal numbers
{"pymdownx.smartsymbols": {"ordinal_numbers": False}}
]
}
def list2string(obj):
"""Convert list to string."""
return '.'.join([str(x) for x in obj])
def format_version(module, attr, call=False):
"""Format the version."""
try:
if call:
version = getattr(module, attr)()
else:
version = getattr(module, attr)
except Exception as e:
print(e)
version = 'Version could not be acquired!'
if not isinstance(version, str):
version = list2string(version)
return version
def is_installed_by_package_control():
"""Check if installed by package control."""
settings = sublime.load_settings('Package Control.sublime-settings')
return str(__pc_name__ in set(settings.get('installed_packages', [])))
class SubNotifySupportInfoCommand(sublime_plugin.ApplicationCommand):
"""Support info."""
def run(self):
"""Run command."""
info = {}
info["platform"] = sublime.platform()
info["version"] = sublime.version()
info["arch"] = sublime.arch()
info["plugin_version"] = __version__
info["pc_install"] = is_installed_by_package_control()
try:
import mdpopups
info["mdpopups_version"] = format_version(mdpopups, 'version', call=True)
except Exception:
info["mdpopups_version"] = 'Version could not be acquired!'
try:
import gntp
info["gntp_version"] = format_version(gntp.version, '__version__')
except Exception:
info["gntp_version"] = 'Version could not be acquired!'
try:
import markdown
info["markdown_version"] = format_version(markdown, 'version')
except Exception:
info["markdown_version"] = 'Version could not be acquired!'
try:
import jinja2
info["jinja_version"] = format_version(jinja2, '__version__')
except Exception:
info["jinja_version"] = 'Version could not be acquired!'
try:
import pygments
info["pygments_version"] = format_version(pygments, '__version__')
except Exception:
info["pygments_version"] = 'Version could not be acquired!'
msg = textwrap.dedent(
"""\
- ST ver.: %(version)s
- Platform: %(platform)s
- Arch: %(arch)s
- Plugin ver.: %(plugin_version)s
- Install via PC: %(pc_install)s
- mdpopups ver.: %(mdpopups_version)s
- gntp ver.: %(gntp_version)s
- markdown ver.: %(markdown_version)s
- pygments ver.: %(pygments_version)s
- jinja2 ver.: %(jinja_version)s
""" % info
)
sublime.message_dialog(msg + '\nInfo has been copied to the clipboard.')
sublime.set_clipboard(msg)
class SubNotifyOpenSiteCommand(sublime_plugin.ApplicationCommand):
"""Open site links."""
def run(self, url):
"""Open the URL."""
webbrowser.open_new_tab(url)
class SubNotifyDocCommand(sublime_plugin.WindowCommand):
"""Open doc page."""
re_pkgs = re.compile(r'^Packages')
def on_navigate(self, href):
"""Handle links."""
if href.startswith('sub://Packages'):
sublime.run_command('open_file', {"file": self.re_pkgs.sub('${packages}', href[6:])})
else:
webbrowser.open_new_tab(href)
def run(self, page):
"""Open page."""
try:
import mdpopups
import pymdownx
has_phantom_support = (mdpopups.version() >= (1, 10, 0)) and (int(sublime.version()) >= 3124)
fmatter = mdpopups.format_frontmatter(frontmatter) if pymdownx.version_info[:3] >= (4, 3, 0) else ''
except Exception:
fmatter = ''
has_phantom_support = False
if not has_phantom_support:
sublime.run_command('open_file', {"file": page})
else:
text = sublime.load_resource(page.replace('${packages}', 'Packages'))
view = self.window.new_file()
view.set_name('SubNotify - Quick Start')
view.settings().set('gutter', False)
view.settings().set('word_wrap', False)
if has_phantom_support:
mdpopups.add_phantom(
view,
'quickstart',
sublime.Region(0),
fmatter + text,
sublime.LAYOUT_INLINE,
css=CSS,
wrapper_class="sub-notify",
on_navigate=self.on_navigate
)
else:
view.run_command('insert', {"characters": text})
view.set_read_only(True)
view.set_scratch(True)
class SubNotifyChangesCommand(sublime_plugin.WindowCommand):
"""Changelog command."""
def run(self):
"""Show the changelog in a new view."""
try:
import mdpopups
import pymdownx
has_phantom_support = (mdpopups.version() >= (1, 10, 0)) and (int(sublime.version()) >= 3124)
fmatter = mdpopups.format_frontmatter(frontmatter) if pymdownx.version_info[:3] >= (4, 3, 0) else ''
except Exception:
fmatter = ''
has_phantom_support = False
text = sublime.load_resource('Packages/SubNotify/CHANGES.md')
view = self.window.new_file()
view.set_name('SubNotify - Changelog')
view.settings().set('gutter', False)
view.settings().set('word_wrap', False)
if has_phantom_support:
mdpopups.add_phantom(
view,
'changelog',
sublime.Region(0),
fmatter + text,
sublime.LAYOUT_INLINE,
wrapper_class="sub-notify",
css=CSS,
on_navigate=self.on_navigate
)
else:
view.run_command('insert', {"characters": text})
view.set_read_only(True)
view.set_scratch(True)
def on_navigate(self, href):
"""Open links."""
webbrowser.open_new_tab(href)