-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmdtoc.py
More file actions
368 lines (288 loc) · 11.8 KB
/
mdtoc.py
File metadata and controls
368 lines (288 loc) · 11.8 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import html
import re
import sublime
import sublime_plugin
ATX_PATTERN = re.compile(r'^(?:>\s*)?(#{1,6})\s+(.+)$')
# ^ Blockquotes can contain other Markdown elements, including headers
SETEXT_H1_PATTERN = re.compile(r'^=+\s*$')
SETEXT_H2_PATTERN = re.compile(r'^-+\s*$')
class Heading:
def __init__(self, title, level, line_number):
self.title = title
self.level = level
self.line_number = line_number
class FormattedHeading:
def __init__(self, indentation, title, line_number):
self.indentation = indentation
self.title = title
self.line_number = line_number
phantoms = {}
phantom_positions = {}
prev_html = {}
class MdTocToggleCommand(sublime_plugin.TextCommand):
def run(self, _):
view = self.view
if not is_markdown_file(view):
sublime.status_message("This command only works with Markdown files")
return
view_id = view.id()
if view_id in phantoms and len(phantoms[view_id].phantoms) > 0:
hide_phantom(view)
else:
headings = parse_headings(view)
if not headings:
sublime.status_message("No headings found")
return
show_phantom(view, headings)
class MdTocNavigateCommand(sublime_plugin.TextCommand):
def run(self, _, line_number):
view = self.view
point = view.text_point(line_number, 0)
view.sel().clear()
view.sel().add(sublime.Region(point, point))
view.show(point, False)
# 🩼 waiting for layout to be updated
sublime.set_timeout(lambda: view.show_at_center(point), 30)
class MdTocListener(sublime_plugin.EventListener):
def on_load_async(self, view):
settings = sublime.load_settings("mdtoc.sublime-settings")
show_on_open = settings.get("show_on_open")
if not show_on_open:
return
if is_markdown_file(view):
headings = parse_headings(view)
if headings:
show_phantom(view, headings)
def on_modified(self, view):
# TODO: the function is kinda laggy on really large files
settings = sublime.load_settings("mdtoc.sublime-settings")
auto_update = settings.get("auto_update")
if not auto_update:
return
view_id = view.id()
if view_id in phantoms and len(phantoms[view_id].phantoms) > 0:
headings = parse_headings(view)
show_phantom(view, headings, is_reposition_needed=False)
def on_selection_modified(self, view):
settings = sublime.load_settings("mdtoc.sublime-settings")
hide_toc = settings.get("hide_toc")
if hide_toc == "after_cursor_move":
view_id = view.id()
if view_id in phantoms and len(phantoms[view_id].phantoms) > 0:
hide_phantom(view)
def on_close(self, view):
view_id = view.id()
phantoms.pop(view_id, None)
phantom_positions.pop(view_id, None)
prev_html.pop(view_id, None)
def parse_headings(view):
# the function doesn't handle ```code blocks``` correctly - according to the specification, all headers
# inside code blocks should be ignored (but nobody cares, lol)
headings = []
ignored_lines = []
def get_setext_heading(setext_region, level):
row, _ = view.rowcol(setext_region.begin())
if row > 0 and row - 1 not in ignored_lines:
line_region = view.line(view.text_point(row - 1, 0))
ignored_lines.append(row)
# a line can't be both a text and directive - we need to skip it when processing
# the next line (it might be a setext header too)
line_content = view.substr(line_region)
if line_content.strip():
# if not, the next line should be treated as a horizontal rule, not a heading
return Heading(line_content, level, row - 1)
return None
combined_pattern = ATX_PATTERN.pattern + "|" + SETEXT_H1_PATTERN.pattern + "|" + SETEXT_H2_PATTERN.pattern
for region in view.find_all(combined_pattern):
text = view.substr(region)
match = ATX_PATTERN.search(text)
if match:
row, _ = view.rowcol(region.begin())
headings.append(Heading(match.group(2), len(match.group(1)), row))
match = SETEXT_H1_PATTERN.search(text)
if match:
headings.append(get_setext_heading(region, 1))
match = SETEXT_H2_PATTERN.search(text)
if match:
headings.append(get_setext_heading(region, 2))
return list(filter(None, headings))
def is_markdown_file(view):
syntax = view.settings().get('syntax', '')
file_name = view.file_name() or ''
return 'markdown' in syntax.lower() or file_name.endswith(('.md', '.markdown'))
def make_indentation(heading, indent_style, indent_width):
spaces = "\u00A0" * indent_width * max(heading.level - 1, 0)
if indent_style == "spaces":
return spaces
elif indent_style == "bullets":
bullets = ["", "•\u00A0", "◦\u00A0", "▪\u00A0", "♦\u00A0"]
bullet = bullets[heading.level - 1]
return spaces[:-2] + bullet
return " "
def make_numbers_indentation(headings, indent_width):
formatted_headings = []
levels = [-1, 0, 0, 0, 0, 0, 0]
for index, heading in enumerate(headings):
indentation = ""
levels[heading.level] += 1
has_prev = index > 0
if has_prev and heading.level < headings[index - 1].level:
for i in range(heading.level + 1, len(levels)):
levels[i] = 0
for i in range(1, heading.level + 1):
indentation += "{}.".format(levels[i])
indentation = "\u00A0" * indent_width * max(heading.level - 1, 0) + indentation + "\u00A0"
formatted_headings.append(FormattedHeading(
html.escape(indentation),
html.escape(heading.title),
heading.line_number
))
return formatted_headings
def make_tree_indentation(headings, indent_width):
formatted_headings = []
ind = max(indent_width, 0)
active_levels = [False] * 7
for index, heading in enumerate(headings):
indentation = ""
level = heading.level
prev = index - 1
next = index + 1
prev_level = headings[prev].level if index > 0 else 0
next_level = headings[next].level if next < len(headings) else 0
if prev_level < level - 1:
for level in range(prev_level + 1, level):
active_levels[level] = True
for l in range(2, level):
if active_levels[l]:
indentation += "│\u00A0" + "\u00A0" * ind
else:
indentation += "\u00A0\u00A0" + "\u00A0" * ind
if level > 1:
has_siblings = False
for next_index in range(next, len(headings)):
# no fucks were given while making this O(n²)
if headings[next_index].level < level:
break
if headings[next_index].level == level:
has_siblings = True
break
if has_siblings:
indentation += "├" + "─" * ind + "\u00A0"
active_levels[level] = True
else:
indentation += "└" + "─" * ind + "\u00A0"
active_levels[level] = False
if next_level < level:
for i in range(next_level + 1, 7):
active_levels[i] = False
formatted_headings.append(FormattedHeading(
html.escape(indentation),
html.escape(heading.title),
heading.line_number
))
return formatted_headings
def format_headings(headings):
settings = sublime.load_settings("mdtoc.sublime-settings")
indent_width = settings.get("indent_width")
indent_style = settings.get("indent_style")
if indent_style == "tree":
return make_tree_indentation(headings, indent_width)
if indent_style == "numbers":
return make_numbers_indentation(headings, indent_width)
else:
return [
FormattedHeading(
make_indentation(heading, indent_style, indent_width),
html.escape(heading.title),
heading.line_number
)
for heading in headings
]
def generate_toc_html(_, headings):
settings = sublime.load_settings("mdtoc.sublime-settings")
link_color = settings.get("link_color")
html_parts = []
dynamic_style = '''
.toc-link {{
text-decoration: none;
color: {color};
}}'''.format(color='var(--foreground)' if link_color == "text" else 'var(--bluish)')
html_parts.append('''
<style>
.toc-container {{
border: 1px solid color(var(--foreground) alpha(0.5));
border-radius: 0.5em;
padding: 0.75em 0.75em;
margin: 0.75em 1em 0.75em 0;
}}
.toc-title {{
font-weight: bold;
color: var(--foreground);
padding-bottom: 0.5em;
border-bottom: 1px solid color(var(--foreground) alpha(0.5));
margin-bottom: 0.5em;
}}
.toc-indent {{
font-family: monospace;
white-space: pre-wrap;
}}
{dynamic_style}
</style>
'''.format(dynamic_style=dynamic_style))
html_parts.append(
'<div class="toc-container"><div class="toc-title">Table of Contents</div><div class="toc-content">')
formatted_headings = format_headings(headings)
for heading in formatted_headings:
html_parts.append('<span class="toc-indent">{}</span><a class="toc-link" href="{}">{}</a><br/>'.format(
heading.indentation, heading.line_number, heading.title))
html_parts.append('</div></div>')
return ''.join(html_parts)
def calc_phantom_position(view, headings):
settings = sublime.load_settings("mdtoc.sublime-settings")
position = settings.get("toc_position")
if position == "document_end":
return view.size()
elif position == "after_first_heading" and headings:
first_heading = headings[0]
line_region = view.line(view.text_point(first_heading.line_number, 0))
return line_region.end()
elif position == "at_cursor":
return view.sel()[0].begin() if len(view.sel()) > 0 else 0
else:
return 0
def show_phantom(view, headings, is_reposition_needed=True):
view_id = view.id()
if view_id not in phantoms:
phantoms[view_id] = sublime.PhantomSet(view, 'md_toc')
phantom_set = phantoms[view_id]
html_content = generate_toc_html(view, headings)
if not is_reposition_needed and prev_html[view_id] == html_content:
return
prev_html[view_id] = html_content
if is_reposition_needed or view_id not in phantom_positions:
position = calc_phantom_position(view, headings)
phantom_positions[view_id] = position
else:
position = phantom_positions[view_id]
phantom = sublime.Phantom(
sublime.Region(position, position),
html_content,
sublime.LAYOUT_BLOCK,
on_navigate=lambda href: handle_toc_click(view, href)
)
phantom_set.update([phantom])
def hide_phantom(view):
view_id = view.id()
if view_id in phantoms:
phantoms[view_id].update([])
phantom_positions.pop(view_id, None)
def handle_toc_click(view, href):
settings = sublime.load_settings("mdtoc.sublime-settings")
hide_toc = settings.get("hide_toc")
try:
line_number = int(href)
view.run_command('md_toc_navigate', {'line_number': line_number})
if hide_toc != "never":
hide_phantom(view)
except ValueError:
pass # ¯\_(ツ)_/¯