-
Notifications
You must be signed in to change notification settings - Fork 3
/
minihtml_preview.py
77 lines (63 loc) · 2.54 KB
/
minihtml_preview.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
import sublime
import sublime_plugin
import webbrowser
template = """\
<body id="minihtml-preview">
<style>
</style>
<h1>minihtml Example</h1>
<p>Enter your own minihtml content on the left to see a preview of it on the
right.</p>
<p><a href="https://www.sublimetext.com/docs/3/minihtml.html">minihtml reference</a>
</p>
</body>
"""
class MinihtmlPreviewCommand(sublime_plugin.ApplicationCommand):
def run(self):
sublime.run_command('new_window')
new_window = sublime.active_window()
new_window.run_command('set_layout', {
'cols': [0.0, 0.5, 1.0],
'rows': [0.0, 1.0],
'cells': [[0, 0, 1, 1], [1, 0, 2, 1]]
})
new_window.focus_group(0)
edit_view = new_window.new_file()
edit_view_settings = edit_view.settings()
edit_view_settings.set('minihtml_preview_edit_view', True)
edit_view.set_scratch(True)
edit_view.set_syntax_file('Packages/HTML/HTML.sublime-syntax')
# Unfortunately Sublime indents on 'insert'
edit_view.settings().set('auto_indent', False)
edit_view.run_command('insert', {'characters': template})
edit_view.settings().set('auto_indent', True)
new_window.focus_group(1)
output_view = new_window.new_file()
output_view.set_scratch(True)
edit_view_settings.set('minihtml_preview_output_view_id',
output_view.id())
new_window.focus_group(0)
def _on_navigate(href):
print('Navigate to: %s' % (href,))
webbrowser.open(href)
class MinihtmlPreviewListener(sublime_plugin.ViewEventListener):
def __init__(self, view):
super(MinihtmlPreviewListener, self).__init__(view)
self.phantom_sets = {}
@classmethod
def is_applicable(cls, settings):
return settings.get('minihtml_preview_edit_view', False)
def on_modified_async(self):
output_id = self.view.settings().get('minihtml_preview_output_view_id')
output_view = next(view for view in self.view.window().views()
if view.id() == output_id)
content = self.view.substr(sublime.Region(0, self.view.size()))
buffer_id = output_view.buffer_id()
if buffer_id in self.phantom_sets:
ps = self.phantom_sets[buffer_id]
else:
ps = sublime.PhantomSet(output_view, 'minihtml_preview_phantom')
self.phantom_sets[buffer_id] = ps
p = sublime.Phantom(sublime.Region(0), content,
sublime.LAYOUT_BLOCK, _on_navigate)
ps.update([p])