-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
support.py
205 lines (167 loc) · 6.14 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
"""Support command."""
import sublime
import sublime_plugin
import textwrap
import webbrowser
import re
__version__ = "2.18.1"
__pc_name__ = 'ScopeHunter'
CSS = '''
div.scope-hunter { padding: 10px; margin: 0; }
.scope-hunter h1, .scope-hunter h2, .scope-hunter h3,
.scope-hunter h4, .scope-hunter h5, .scope-hunter h6 {
{{'string'|css}}
}
.scope-hunter blockquote { {{'comment'|css}} }
.scope-hunter 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": "HexViewer"
}
},
"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 ScopeHunterSupportInfoCommand(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!'
msg = textwrap.dedent(
"""\
- ST ver.: {version}
- Platform: {platform}
- Arch: {arch}
- Plugin ver.: {plugin_version}
- Install via PC: {pc_install}
- mdpopups ver.: {mdpopups_version}
""".format(**info)
)
sublime.message_dialog(msg + '\nInfo has been copied to the clipboard.')
sublime.set_clipboard(msg)
class ScopeHunterOpenSiteCommand(sublime_plugin.ApplicationCommand):
"""Open site links."""
def run(self, url):
"""Open the url."""
webbrowser.open_new_tab(url)
class ScopeHunterDocCommand(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
has_phantom_support = (mdpopups.version() >= (1, 10, 0)) and (int(sublime.version()) >= 3124)
fmatter = mdpopups.format_frontmatter(frontmatter)
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('ScopeHunter - 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="scope-hunter",
on_navigate=self.on_navigate
)
else:
view.run_command('insert', {"characters": text})
view.set_read_only(True)
view.set_scratch(True)
class ScopeHunterChangesCommand(sublime_plugin.WindowCommand):
"""Changelog command."""
def run(self):
"""Show the changelog in a new view."""
try:
import mdpopups
has_phantom_support = (mdpopups.version() >= (1, 10, 0)) and (int(sublime.version()) >= 3124)
fmatter = mdpopups.format_frontmatter(frontmatter)
except Exception:
fmatter = ''
has_phantom_support = False
text = sublime.load_resource('Packages/ScopeHunter/CHANGES.md')
view = self.window.new_file()
view.set_name('ScopeHunter - 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="scope-hunter",
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)