-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
207 lines (177 loc) · 7.09 KB
/
main.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
import sublime
import sublime_plugin
import re
import os
from html import escape
class MarkdownCodeExporter(sublime_plugin.ViewEventListener):
@classmethod
def is_applicable(cls, settings):
return settings.get('syntax', '').lower().find('markdown') != -1
def __init__(self, view):
super().__init__(view)
self.phantom_set = sublime.PhantomSet(view)
self.timeout_scheduled = False
self.needs_update = False
self.update_phantoms()
def on_modified(self):
# Don't operate on 1MB or larger files.
if self.view.size() > 2**20:
return
# Call update_phantoms(), but not any more than 10 times a second
if self.timeout_scheduled:
self.needs_update = True
else:
self.timeout_scheduled = True
sublime.set_timeout(lambda: self.handle_timeout(), 100)
self.update_phantoms()
def on_load(self):
self.update_phantoms()
def handle_timeout(self):
self.timeout_scheduled = False
if self.needs_update:
self.needs_update = False
self.update_phantoms()
def update_phantoms(self):
# Find all fenced code blocks
code_blocks = self.find_code_blocks()
phantoms = []
for region in code_blocks:
# Create phantom content
content = '''
<body id="markdown-code-exporter">
<style>
div {
padding: 0.2rem 0;
margin-bottom: 3px;
}
a {
padding: 0.4rem;
border-radius: 5px;
color: #000;
background-color: #CCCCCC;
text-decoration: none;
}
</style>
<div>
<a href="copy">copy</a>
<a href="new_tab">open in tab</a>
</div>
</body>
'''
# Create and add phantom
phantom = sublime.Phantom(
sublime.Region(region.begin() - 1, region.begin() - 1), # -1 puts the phantom to the previous line.
content,
sublime.LAYOUT_BLOCK,
on_navigate=lambda href, region=region: self.handle_phantom_click(href, region)
)
phantoms.append(phantom)
self.phantom_set.update(phantoms)
def find_code_blocks(self):
code_blocks = []
view = self.view
# Find all fenced code blocks (```)
content = view.substr(sublime.Region(0, view.size()))
pattern = r'^( *```+)[\w\-]*\n[\s\S]*?\n\1$'
matches = re.finditer(pattern, content, re.MULTILINE)
for match in matches:
start = match.start()
end = match.end()
code_blocks.append(sublime.Region(start, end))
return code_blocks
def handle_phantom_click(self, href, region):
# Get the code content (excluding the first and last lines with ```)
lines = self.view.substr(region).split('\n')
code = '\n'.join(lines[1:-1]) + "\n"
if href == "copy":
# Copy to clipboard
sublime.set_clipboard(code)
sublime.status_message("Code copied to clipboard")
elif href == "new_tab":
# Open in new tab
new_view = self.view.window().new_file()
new_view.run_command('append', {'characters': code})
# Try to detect and set syntax
identifier = re.sub(r"^ *`+", "", lines[0]).strip().lower() # Remove ``` and get language identifier
# We want to prevent mismatches. For example, "sql" might match with
# "Packages/Rails/SQL (Rails).sublime-syntax" before it matches with
# "Packages/SQL/SQL.sublime-syntax".
#
# There may be syntaxes here that Sublime Text doesn't have. Those will
# be skipped and the first existing syntax will be used.
id_syntax_map = [
{
"identifier": ["md", "markdown", "mdown"],
"syntaxes": [
"Packages/MarkdownEditing/Markdown.sublime-syntax",
"Packages/MarkdownEditing/MultiMarkdown.tmLanguage",
"Packages/MarkdownEditing/Markdown (Standard).tmLanguage",
"Packages/Markdown/Markdown.sublime-syntax",
"Packages/Markdown/MultiMarkdown.sublime-syntax",
],
},
{
"identifier": ["js", "javascript"],
"syntaxes": [
"Packages/JavaScript/JavaScript.sublime-syntax",
],
},
{
"identifier": ["html"],
"syntaxes": [
"Packages/HTML/HTML.sublime-syntax",
],
},
{
"identifier": ["java"],
"syntaxes": [
"Packages/Java/Java.sublime-syntax",
],
},
{
"identifier": ["php"],
"syntaxes": [
"Packages/PHP/PHP.sublime-syntax",
],
},
{
"identifier": ["py", "python"],
"syntaxes": [
"Packages/Python/Python.sublime-syntax",
],
},
{
"identifier": ["rb", "ruby"],
"syntaxes": [
"Packages/Ruby/Ruby.sublime-syntax",
],
},
{
"identifier": ["sh", "shell"],
"syntaxes": [
"Packages/ShellScript/Shell-Unix-Generic.sublime-syntax",
],
},
{
"identifier": ["sql"],
"syntaxes": [
"Packages/SQL/SQL.sublime-syntax",
],
},
]
if not identifier:
return
syntax_files = sublime.find_resources('*.tmLanguage') + sublime.find_resources('*.sublime-syntax')
for mapping in id_syntax_map:
if not identifier in mapping["identifier"]:
continue
# Now find the first syntax that Sublime Text supports.
for syntax in mapping["syntaxes"]:
if syntax in syntax_files:
new_view.assign_syntax(syntax)
return
# There were no match with "id_syntax_map". Now do a plain search.
for syntax_file in syntax_files:
if identifier in os.path.basename(syntax_file.lower()):
new_view.assign_syntax(syntax_file)
return