-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkdown_helper.py
executable file
·254 lines (232 loc) · 7.91 KB
/
markdown_helper.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Date : 2020-01-17 15:48:59
# @Author : Lewis Tian ([email protected])
# @Link : github.com/taseikyo
# @Version : python3.8
"""
A simple st3 plugin can help write markdown faster (maybe)
Note: Remember to select the entire line and then press the shortcuts
---
shortcuts:
ctrl + 1-3: h1 - h3
ctrl + 4: ul
ctrl + 5: ol
ctrl + 6: code (block codes)
ctrl + 7: latex formula (block)
"""
import sublime
import sublime_plugin
PLUGIN_NAME = "MarkdownHelper"
def load_settings():
return sublime.load_settings(PLUGIN_NAME + ".sublime-settings")
class PluginEventListener(sublime_plugin.EventListener):
def on_query_context(self, view, key, operator, operand, match_all):
if key == "helper_is_enabled":
settings = load_settings()
allows = settings.get("allow_fileformats")
syntax = view.settings().get("syntax").split("/")[-1].split(".")[0]
if syntax.lower() in allows:
return True
return False
class MarkdownHelperCommand(sublime_plugin.TextCommand):
def run(self, edit, md_type):
"""
select function according $md_type
"""
if md_type[0] == "h":
self.helper_hx(edit, int(md_type[1]))
elif md_type == "ul":
self.helper_ul(edit)
elif md_type == "tl":
self.helper_tl(edit)
elif md_type == "ol":
self.helper_ol(edit)
elif md_type == "code":
self.helper_code(edit)
elif md_type == "icode":
self.helper_text(edit, "`")
elif md_type == "lxf":
self.helper_lxf(edit)
elif md_type == "ilxf":
self.helper_text(edit, "$")
elif md_type == "bt":
self.helper_text(edit, "**")
elif md_type == "it":
self.helper_text(edit, "*")
elif md_type == "dt":
self.helper_text(edit, "~~")
elif md_type == "img":
self.helper_text(edit, "![](", ")")
def helper_h1(self, edit):
"""
generate h1 title
---
xxx => # xxx
"""
# region consists many lines
# so we use `self.view.lines` to get all the lines (type: region)
for region in self.view.sel():
if not region.empty():
line = self.view.line(region)
# st3 uses py3.3, we cannot use f-strings ;(
text = "# " + self.view.substr(line).lstrip()
# remove the old text and insert...
self.view.erase(edit, region)
self.view.insert(edit, line.begin(), text)
def helper_hx(self, edit, level):
"""
generate hx title
---
xxx => # xxx
xxx => ## xxx
...
"""
# region consists many lines
# so we use `self.view.lines` to get all the lines (type: region)
for region in self.view.sel():
if not region.empty():
subregions = self.view.lines(region)
new_text = []
for subregion in subregions:
line = self.view.line(subregion)
# st3 uses py3.3, we cannot use f-strings ;(
text = "#" * level + " " + self.view.substr(line).lstrip() + "\n"
new_text.append(text)
# remove the old text and insert...
self.view.erase(edit, region)
self.view.insert(edit, subregions[0].begin(), "".join(new_text))
def helper_ul(self, edit):
"""
generate unordered list
---
aaa
bbb
ccc
=>
- aaa
- bbb
- ccc
"""
for region in self.view.sel():
if not region.empty():
subregions = self.view.lines(region)
new_text = []
for subregion in subregions:
line = self.view.line(subregion)
# st3 uses py3.3, we cannot use f-strings ;(
text = "- " + self.view.substr(line).lstrip() + "\n"
new_text.append(text)
# remove the old text and insert...
self.view.erase(edit, region)
self.view.insert(edit, subregions[0].begin(), "".join(new_text))
def helper_tl(self, edit):
"""
generate task list
---
aaa
bbb
ccc
=>
- [] aaa
- [] bbb
- [] ccc
"""
for region in self.view.sel():
if not region.empty():
subregions = self.view.lines(region)
new_text = []
for subregion in subregions:
line = self.view.line(subregion)
# st3 uses py3.3, we cannot use f-strings ;(
text = "- [ ] " + self.view.substr(line).lstrip() + "\n"
new_text.append(text)
# remove the old text and insert...
self.view.erase(edit, region)
self.view.insert(edit, subregions[0].begin(), "".join(new_text))
def helper_ol(self, edit):
"""
generate ordered list
---
aaa
bbb
ccc
=>
1. aaa
2. bbb
3. ccc
"""
for region in self.view.sel():
if not region.empty():
subregions = self.view.lines(region)
new_text = []
for index, subregion in enumerate(subregions):
line = self.view.line(subregion)
# st3 uses py3.3, we cannot use f-strings ;(
text = (
str(index + 1) + ". " + self.view.substr(line).lstrip() + "\n"
)
new_text.append(text)
# remove the old text and insert...
self.view.erase(edit, region)
self.view.insert(edit, subregions[0].begin(), "".join(new_text))
def helper_code(self, edit):
"""
generate code
---
aaa
bbb
=>
```
aaa
bbb
```
"""
for region in self.view.sel():
if not region.empty():
# Packages/Python/Python.tmLanguage
# stupid method, this will always return `Markdown`
syntax = self.view.settings().get("syntax").split("/")[-1].split(".")[0]
line = self.view.line(region)
# st3 uses py3.3, we cannot use f-strings ;(
text = "```" + syntax + "\n" + self.view.substr(line) + "\n```\n"
# remove the old text and insert...
self.view.erase(edit, region)
self.view.insert(edit, line.begin(), text)
def helper_lxf(self, edit):
"""
generate latex formula
---
aaa
bbb
=>
$$
aaa
bbb
$$
"""
for region in self.view.sel():
if not region.empty():
line = self.view.line(region)
# st3 uses py3.3, we cannot use f-strings ;(
text = "$$\n" + self.view.substr(line) + "\n$$\n"
# remove the old text and insert...
self.view.erase(edit, region)
self.view.insert(edit, line.begin(), text)
def helper_text(self, edit, begin_char, end_char=""):
"""
generate style text
---
bt => bold text: aaa -> **aaa**
it => italic text: aaa -> *aaa*
dt => delete textL aaa -> ~~aaa~~
"""
for region in self.view.sel():
if not region.empty():
code = self.view.substr(sublime.Region(region.a, region.b))
# replace(edit, region, string)
self.view.replace(
edit,
region,
begin_char + code + (end_char if end_char else begin_char),
)