-
Notifications
You must be signed in to change notification settings - Fork 50
/
support.py
228 lines (188 loc) · 7.06 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
"""Support command."""
import sublime
import sublime_plugin
import textwrap
import webbrowser
import re
__version__ = "2.7.0"
__pc_name__ = 'HexViewer'
CSS = '''
div.hex-viewer { padding: 10px; margin: 0; }
.hex-viewer h1, .hex-viewer h2, .hex-viewer h3,
.hex-viewer h4, .hex-viewer h5, .hex-viewer h6 {
{{'.string'|css}}
}
.hex-viewer blockquote { {{'.comment'|css}} }
.hex-viewer 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.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 HexViewerSupportInfoCommand(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 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
- 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 HexViewerOpenSiteCommand(sublime_plugin.ApplicationCommand):
"""Open site links."""
def run(self, url):
"""Open the url."""
webbrowser.open_new_tab(url)
class HexViewerDocCommand(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('HexViewer - 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="hex-viewer",
on_navigate=self.on_navigate
)
else:
view.run_command('insert', {"characters": text})
view.set_read_only(True)
view.set_scratch(True)
class HexViewerChangesCommand(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/HexViewer/CHANGES.md')
view = self.window.new_file()
view.set_name('HexViewer - 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="hex-viewer",
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)