forked from aziz/PlainTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlainTasks.py
567 lines (508 loc) · 27.8 KB
/
PlainTasks.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re
import sublime
import sublime_plugin
import webbrowser
from datetime import datetime
if int(sublime.version()) < 3000:
import locale
class PlainTasksBase(sublime_plugin.TextCommand):
def run(self, edit):
self.taskpaper_compatible = self.view.settings().get('taskpaper_compatible')
if self.taskpaper_compatible:
self.open_tasks_bullet = self.done_tasks_bullet = self.canc_tasks_bullet = '-'
self.before_date_space = ''
else:
self.open_tasks_bullet = self.view.settings().get('open_tasks_bullet')
self.done_tasks_bullet = self.view.settings().get('done_tasks_bullet')
self.canc_tasks_bullet = self.view.settings().get('cancelled_tasks_bullet')
self.before_date_space = ' '
translate_tabs_to_spaces = self.view.settings().get('translate_tabs_to_spaces')
self.before_tasks_bullet_spaces = ' ' * self.view.settings().get('before_tasks_bullet_margin') if translate_tabs_to_spaces else '\t'
self.tasks_bullet_space = self.view.settings().get('tasks_bullet_space', ' ' if translate_tabs_to_spaces else '\t')
self.date_format = self.view.settings().get('date_format')
if self.view.settings().get('done_tag') or self.taskpaper_compatible:
self.done_tag = "@done"
self.canc_tag = "@cancelled"
else:
self.done_tag = ""
self.canc_tag = ""
if int(sublime.version()) < 3000:
self.sys_enc = locale.getpreferredencoding()
self.project_postfix = self.view.settings().get('project_tag')
self.archive_name = self.view.settings().get('archive_name')
self.runCommand(edit)
class PlainTasksNewCommand(PlainTasksBase):
def runCommand(self, edit):
selections = list(self.view.sel()) # for ST3 support
selections.reverse() # because with multiple selections regions would be messed up after first iteration
for region in selections:
line = self.view.line(region)
line_contents = self.view.substr(line).rstrip()
not_empty_line = re.match('^(\s*)(\S.+)$', self.view.substr(line))
empty_line = re.match('^(\s+)$', self.view.substr(line))
current_scope = self.view.scope_name(line.a)
if 'item' in current_scope:
grps = not_empty_line.groups()
line_contents = self.view.substr(line) + '\n' + grps[0] + self.open_tasks_bullet + self.tasks_bullet_space
elif 'header' in current_scope and not self.view.settings().get('header_to_task'):
grps = not_empty_line.groups()
line_contents = self.view.substr(line) + '\n' + grps[0] + self.before_tasks_bullet_spaces + self.open_tasks_bullet + self.tasks_bullet_space
elif 'separator' in current_scope:
grps = not_empty_line.groups()
line_contents = self.view.substr(line) + '\n' + grps[0] + self.before_tasks_bullet_spaces + self.open_tasks_bullet + self.tasks_bullet_space
elif not ('header' and 'separator') in current_scope or self.view.settings().get('header_to_task'):
if not_empty_line:
grps = not_empty_line.groups()
line_contents = (grps[0] if len(grps[0]) > 0 else self.before_tasks_bullet_spaces) + self.open_tasks_bullet + self.tasks_bullet_space + grps[1]
elif empty_line: # only whitespaces
grps = empty_line.groups()
line_contents = grps[0] + self.open_tasks_bullet + self.tasks_bullet_space
else: # completely empty, no whitespaces
line_contents = self.before_tasks_bullet_spaces + self.open_tasks_bullet + self.tasks_bullet_space
else:
print('oops, need to improve PlainTasksNewCommand')
self.view.replace(edit, line, line_contents)
# convert each selection to single cursor, ready to type
new_selections = []
for sel in list(self.view.sel()):
if not sel.empty():
new_selections.append(sublime.Region(sel.b, sel.b))
else:
new_selections.append(sel)
self.view.sel().clear()
for sel in new_selections:
self.view.sel().add(sel)
class PlainTasksCompleteCommand(PlainTasksBase):
def runCommand(self, edit):
original = [r for r in self.view.sel()]
try:
done_line_end = ' %s%s%s' % (self.done_tag, self.before_date_space, datetime.now().strftime(self.date_format).decode(self.sys_enc))
except:
done_line_end = ' %s%s%s' % (self.done_tag, self.before_date_space, datetime.now().strftime(self.date_format))
offset = len(done_line_end)
rom = r'^(\s*)(\[\s\]|.)(\s*.*)$'
rdm = r'^(\s*)(\[x\]|.)(\s*[^\b]*?\s*)(?=\s@done|@project|\s\(|$).*$'
rcm = r'^(\s*)(\[\-\]|.)(\s*[^\b]*?\s*)(?=\s@cancelled|@project|\s\(|$).*$'
started = r'^\s*[^\b]*?\s*@started(\([\d\w,\.:\-\/ @]*\)).*$'
for region in self.view.sel():
line = self.view.line(region)
line_contents = self.view.substr(line).rstrip()
open_matches = re.match(rom, line_contents, re.U)
done_matches = re.match(rdm, line_contents, re.U)
canc_matches = re.match(rcm, line_contents, re.U)
started_matches = re.match(started, line_contents, re.U)
current_scope = self.view.scope_name(line.a)
if 'pending' in current_scope:
grps = open_matches.groups()
eol = self.view.insert(edit, line.end(), done_line_end)
replacement = u'%s%s%s' % (grps[0], self.done_tasks_bullet, grps[2].rstrip())
self.view.replace(edit, line, replacement)
if started_matches:
eol -= len(grps[1]) - len(self.done_tasks_bullet)
self.calc_end_start_time(self, edit, line, started_matches.group(1), done_line_end, eol)
elif 'header' in current_scope:
eol = self.view.insert(edit, line.end(), done_line_end)
if started_matches:
self.calc_end_start_time(self, edit, line, started_matches.group(1), done_line_end, eol)
indent = re.match('^(\s*)\S', line_contents, re.U)
self.view.insert(edit, line.begin() + len(indent.group(1)), '%s ' % self.done_tasks_bullet)
elif 'completed' in current_scope:
grps = done_matches.groups()
replacement = u'%s%s%s' % (grps[0], self.open_tasks_bullet, grps[2].rstrip())
self.view.replace(edit, line, replacement)
offset = -offset
elif 'cancelled' in current_scope:
grps = canc_matches.groups()
self.view.insert(edit, line.end(), done_line_end)
replacement = u'%s%s%s' % (grps[0], self.done_tasks_bullet, grps[2].rstrip())
self.view.replace(edit, line, replacement)
offset = -offset
self.view.sel().clear()
for ind, pt in enumerate(original):
ofs = ind * offset
new_pt = sublime.Region(pt.a + ofs, pt.b + ofs)
self.view.sel().add(new_pt)
@staticmethod
def calc_end_start_time(self, edit, line, started_matches, done_line_end, eol, tag='lasted'):
start = datetime.strptime(started_matches, self.date_format)
end = datetime.strptime(done_line_end.replace('@done', '').replace('@cancelled', '').strip(), self.date_format)
self.view.insert(edit, line.end() + eol, ' @%s(%s)' % (tag, str(end - start)))
class PlainTasksCancelCommand(PlainTasksBase):
def runCommand(self, edit):
original = [r for r in self.view.sel()]
try:
canc_line_end = ' %s%s%s' % (self.canc_tag,self.before_date_space, datetime.now().strftime(self.date_format).decode(self.sys_enc))
except:
canc_line_end = ' %s%s%s' % (self.canc_tag,self.before_date_space, datetime.now().strftime(self.date_format))
offset = len(canc_line_end)
rom = r'^(\s*)(\[\s\]|.)(\s*.*)$'
rdm = r'^(\s*)(\[x\]|.)(\s*[^\b]*?\s*)(?=\s@done|@project|\s\(|$).*$'
rcm = r'^(\s*)(\[\-\]|.)(\s*[^\b]*?\s*)(?=\s@cancelled|@project|\s\(|$).*$'
started = '^\s*[^\b]*?\s*@started(\([\d\w,\.:\-\/ @]*\)).*$'
for region in self.view.sel():
line = self.view.line(region)
line_contents = self.view.substr(line).rstrip()
open_matches = re.match(rom, line_contents, re.U)
done_matches = re.match(rdm, line_contents, re.U)
canc_matches = re.match(rcm, line_contents, re.U)
started_matches = re.match(started, line_contents, re.U)
current_scope = self.view.scope_name(line.a)
if 'pending' in current_scope:
grps = open_matches.groups()
eol = self.view.insert(edit, line.end(), canc_line_end)
replacement = u'%s%s%s' % (grps[0], self.canc_tasks_bullet, grps[2].rstrip())
self.view.replace(edit, line, replacement)
if started_matches:
eol -= len(grps[1]) - len(self.canc_tasks_bullet)
PlainTasksCompleteCommand.calc_end_start_time(self, edit, line, started_matches.group(1), canc_line_end, eol, tag='wasted')
elif 'header' in current_scope:
eol = self.view.insert(edit, line.end(), canc_line_end)
if started_matches:
PlainTasksCompleteCommand.calc_end_start_time(self, edit, line, started_matches.group(1), canc_line_end, eol, tag='wasted')
indent = re.match('^(\s*)\S', line_contents, re.U)
self.view.insert(edit, line.begin() + len(indent.group(1)), '%s ' % self.canc_tasks_bullet)
elif 'completed' in current_scope:
sublime.status_message('You cannot cancel what have been done, can you?')
# grps = done_matches.groups()
# replacement = u'%s%s%s' % (grps[0], self.canc_tasks_bullet, grps[2].rstrip())
# self.view.replace(edit, line, replacement)
# offset = -offset
elif 'cancelled' in current_scope:
grps = canc_matches.groups()
replacement = u'%s%s%s' % (grps[0], self.open_tasks_bullet, grps[2].rstrip())
self.view.replace(edit, line, replacement)
offset = -offset
self.view.sel().clear()
for ind, pt in enumerate(original):
ofs = ind * offset
new_pt = sublime.Region(pt.a + ofs, pt.b + ofs)
self.view.sel().add(new_pt)
class PlainTasksArchiveCommand(PlainTasksBase):
def runCommand(self, edit):
rds = 'meta.item.todo.completed'
rcs = 'meta.item.todo.cancelled'
# finding archive section
archive_pos = self.view.find(self.archive_name, 0, sublime.LITERAL)
done_tasks = [i for i in self.view.find_by_selector(rds) if i.a < (archive_pos.a if archive_pos and archive_pos.a > 0 else self.view.size())]
for i in done_tasks:
self.get_task_note(i, done_tasks)
canc_tasks = [i for i in self.view.find_by_selector(rcs) if i.a < (archive_pos.a if archive_pos and archive_pos.a > 0 else self.view.size())]
for i in canc_tasks:
self.get_task_note(i, canc_tasks)
all_tasks = done_tasks + canc_tasks
all_tasks.sort()
if all_tasks:
if archive_pos and archive_pos.a > 0:
line = self.view.full_line(archive_pos).end()
else:
create_archive = u'\n\n___________________\n' + self.archive_name + '\n'
self.view.insert(edit, self.view.size(), create_archive)
line = self.view.size()
projects = self.view.find_all('^\s*(\w+.+:\s*(\@[^\s]+(\(.*?\))?\s*)*$\n?)|^\s*---.{3,5}---+$', 0)
# adding tasks to archive section
for task in all_tasks:
match_task = re.match('^\s*(\[[x-]\]|.)(\s+.*$)', self.view.substr(task), re.U)
current_scope = self.view.scope_name(task.a)
if rds in current_scope or rcs in current_scope:
pr = self.get_task_project(task, projects)
if self.project_postfix:
eol = (self.before_tasks_bullet_spaces + self.view.substr(task).lstrip() +
(' @project(' if pr else '') + pr + (')' if pr else '') +
'\n')
else:
eol = (self.before_tasks_bullet_spaces +
match_task.group(1) + # bullet
(self.tasks_bullet_space if pr else '') + pr + (':' if pr else '') +
match_task.group(2) + # very task
'\n')
else:
eol = self.before_tasks_bullet_spaces * 2 + self.view.substr(task).lstrip() + '\n'
line += self.view.insert(edit, line, eol)
# remove moved tasks (starting from the last one otherwise it screw up regions after the first delete)
for task in reversed(all_tasks):
self.view.erase(edit, self.view.full_line(task))
self.view.run_command('plain_tasks_sort_by_date')
def get_task_project(self, task, projects):
index = -1
for ind, pr in enumerate(projects):
if task < pr:
if ind > 0:
index = ind-1
break
#if there is no projects for task - return empty string
if index == -1:
return ''
prog = re.compile('^\n*(\s*)(.+):(?=\s|$)\s*(\@[^\s]+(\(.*?\))?\s*)*')
hierarhProject = ''
if index >= 0:
depth = re.match(r"\s*", self.view.substr(self.view.line(task))).group()
while index >= 0:
strProject = self.view.substr(projects[index])
if prog.match(strProject):
spaces = prog.match(strProject).group(1)
if len(spaces) < len(depth):
hierarhProject = prog.match(strProject).group(2) + ((" / " + hierarhProject) if hierarhProject else '')
depth = spaces
if len(depth) == 0:
break
else:
sep = re.compile('(^\s*)---.{3,5}---+$')
spaces = sep.match(strProject).group(1)
if len(spaces) < len(depth):
depth = spaces
if len(depth) == 0:
break
index -= 1
if not hierarhProject:
return ''
else:
return hierarhProject
def get_task_note(self, task, tasks):
note_line = task.end() + 1
while self.view.scope_name(note_line) == 'text.todo notes.todo ':
note = self.view.line(note_line)
if note not in tasks:
tasks.append(note)
note_line = self.view.line(note_line).end() + 1
class PlainTasksNewTaskDocCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.new_file()
view.set_syntax_file('Packages/PlainTasks/PlainTasks.tmLanguage')
class PlainTasksOpenUrlCommand(sublime_plugin.TextCommand):
#It is horrible regex but it works perfectly
URL_REGEX = r"""(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))
+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))"""
def run(self, edit):
s = self.view.sel()[0]
# expand selection to possible URL
start = s.a
end = s.b
# expand selection to nearest stopSymbols
view_size = self.view.size()
stopSymbols = ['\t', ' ', '\"', '\'', '>', '<', ',']
# move the selection back to the start of the url
while (start > 0
and not self.view.substr(start - 1) in stopSymbols
and self.view.classify(start) & sublime.CLASS_LINE_START == 0):
start -= 1
# move end of selection forward to the end of the url
while (end < view_size
and not self.view.substr(end) in stopSymbols
and self.view.classify(end) & sublime.CLASS_LINE_END == 0):
end += 1
# grab the URL
url = self.view.substr(sublime.Region(start, end))
# optional select URL
self.view.sel().add(sublime.Region(start, end))
exp = re.search(self.URL_REGEX, url, re.X)
if exp and exp.group(0):
strUrl = exp.group(0)
if strUrl.find("://") == -1:
strUrl = "http://" + strUrl
webbrowser.open_new_tab(strUrl)
else:
sublime.status_message("Looks like there is nothing to open")
class PlainTasksOpenLinkCommand(sublime_plugin.TextCommand):
LINK_PATTERN = re.compile(r'\.[\\/](?P<fn>[^\\/:*?"<>|]+)+[\\/]?(>(?P<sym>\w+))?(\:(?P<line>\d+))?(\:(?P<col>\d+))?(\"(?P<text>[^\n]*)\")?', re.I| re.U)
def _format_res(self, res):
return [res[0], "line: %d column: %d" % (int(res[1]), int(res[2]))]
def _on_panel_selection(self, selection):
if selection >= 0:
res = self._current_res[selection]
win = sublime.active_window()
self.opened_file = win.open_file('%s:%s:%s' % res, sublime.ENCODED_POSITION)
def show_panel_or_open(self, fn, sym, line, col, text):
win = sublime.active_window()
self._current_res = list()
if sym:
for name, _, pos in win.lookup_symbol_in_index(sym):
if name.endswith(fn):
line, col = pos
self._current_res.append((name, line, col))
else:
fn = fn.replace('/', os.sep)
all_folders = win.folders() + [os.path.dirname(v.file_name()) for v in win.views() if v.file_name()]
for folder in set(all_folders):
for root, dirnames, filenames in os.walk(folder):
filenames = [os.path.join(root, f) for f in filenames]
for name in filenames:
if name.lower().endswith(fn.lower()):
self._current_res.append((name, line if line else 0, col if col else 0))
self._current_res = list(set(self._current_res))
if len(self._current_res) == 1:
self._on_panel_selection(0)
else:
entries = [self._format_res(res) for res in self._current_res]
win.show_quick_panel(entries, self._on_panel_selection)
def run(self, edit):
point = self.view.sel()[0].begin()
line = self.view.substr(self.view.line(point))
match = self.LINK_PATTERN.search(line)
if match:
fn, sym, line, col, text = match.group('fn', 'sym', 'line', 'col', 'text')
self.show_panel_or_open(fn, sym, line, col, text)
if text:
sublime.set_timeout(lambda: self.find_text(self.opened_file, text, line), 300)
def find_text(self, view, text, line):
result = view.find(text, view.sel()[0].a if line else 0, sublime.LITERAL)
view.sel().clear()
view.sel().add(result.a)
view.set_viewport_position(view.text_to_layout(view.size()), False)
view.show_at_center(result)
class PlainTasksSortByDate(PlainTasksBase):
def runCommand(self, edit):
archive_pos = self.view.find(self.archive_name, 0, sublime.LITERAL)
if archive_pos:
have_date = '(^\s*[^\n]*?\s\@(?:done|cancelled)\s*(\([\d\w,\.:\-\/ ]*\))[^\n]*$)'
tasks_prefixed_date = []
tasks = self.view.find_all(have_date, archive_pos.b-1, "\\2\\1", tasks_prefixed_date)
notes = []
for ind, task in enumerate(tasks):
note_line = task.end() + 1
while self.view.scope_name(note_line) == 'text.todo notes.todo ':
note = self.view.line(note_line)
notes.append(note)
tasks_prefixed_date[ind] += '\n' + self.view.substr(note)
note_line = note.end() + 1
to_remove = tasks+notes
to_remove.sort()
for i in reversed(to_remove):
self.view.erase(edit, self.view.full_line(i))
tasks_prefixed_date.sort(reverse=self.view.settings().get('new_on_top'))
eol = archive_pos.end()
for a in tasks_prefixed_date:
eol += self.view.insert(edit, eol, '\n' + re.sub('^\([\d\w,\.:\-\/ ]*\)([^\b]*$)', '\\1', a))
else:
sublime.status_message("Nothing to sort")
class PlainTaskInsertDate(PlainTasksBase):
def runCommand(self, edit):
for s in reversed(list(self.view.sel())):
self.view.insert(edit, s.b, datetime.now().strftime(self.date_format))
class PlainTasksConvertToHtml(PlainTasksBase):
def is_enabled(self):
return self.view.score_selector(0, "text.todo") > 0
def runCommand(self, edit):
all_lines_regions = self.view.split_by_newlines(sublime.Region(0, self.view.size()))
html_doc = []
patterns = {'HEADER' : 'text.todo keyword.control.header.todo ',
'EMPTY' : 'text.todo ',
'NOTE' : 'text.todo notes.todo ',
'OPEN' : 'text.todo meta.item.todo.pending ',
'DONE' : 'text.todo meta.item.todo.completed ',
'CANCELLED' : 'text.todo meta.item.todo.cancelled ',
'SEPARATOR' : 'text.todo meta.punctuation.separator.todo ',
'ARCHIVE' : 'text.todo meta.punctuation.archive.todo '
}
for r in all_lines_regions:
i = self.view.scope_name(r.a)
if patterns['HEADER'] in i:
ht = '<span class="header">%s</span>' % self.view.substr(r)
elif i == patterns['EMPTY']:
# these are empty lines
ht = '<span class="empty-line">%s</span>' % self.view.substr(r)
elif patterns['NOTE'] in i:
scopes = self.extracting_scopes(self, r, i)
note = '<span class="note">'
for s in scopes:
sn = self.view.scope_name(s.a)
if 'italic' in sn:
note += '<i>%s</i>' % self.view.substr(s).replace('_', '').replace('*', '')
elif 'bold' in sn:
note += '<b>%s</b>' % self.view.substr(s).replace('_', '').replace('*', '')
else:
note += self.view.substr(s)
ht = note + '</span>'
elif patterns['OPEN'] in i:
scopes = self.extracting_scopes(self, r)
indent = self.view.substr(sublime.Region(r.a, scopes[0].a)) if r.a != scopes[0].a else ''
pending = '<span class="open">%s' % indent
for s in scopes:
sn = self.view.scope_name(s.a)
if 'bullet' in sn:
pending += '<span class="bullet-pending">%s</span>' % self.view.substr(s)
elif 'meta.tag' in sn:
pending += '<span class="tag">%s</span>' % self.view.substr(s)
elif 'tag.todo.today' in sn:
pending += '<span class="tag-today">%s</span>' % self.view.substr(s)
else:
pending += self.view.substr(s)
ht = pending + '</span>'
elif patterns['DONE'] in i:
scopes = self.extracting_scopes(self, r)
indent = self.view.substr(sublime.Region(r.a, scopes[0].a)) if r.a != scopes[0].a else ''
done = '<span class="done">%s' % indent
for s in scopes:
sn = self.view.scope_name(s.a)
if 'bullet' in sn:
done += '<span class="bullet-done">%s</span>' % self.view.substr(s)
elif 'tag.todo.completed' in sn:
done += '<span class="tag-done">%s</span>' % self.view.substr(s)
else:
done += self.view.substr(s)
ht = done + '</span>'
elif patterns['CANCELLED'] in i:
scopes = self.extracting_scopes(self, r)
indent = self.view.substr(sublime.Region(r.a, scopes[0].a)) if r.a != scopes[0].a else ''
cancelled = '<span class="cancelled">%s' % indent
for s in scopes:
sn = self.view.scope_name(s.a)
if 'bullet' in sn:
cancelled += '<span class="bullet-cancelled">%s</span>' % self.view.substr(s)
elif 'tag.todo.cancelled' in sn:
cancelled += '<span class="tag-cancelled">%s</span>' % self.view.substr(s)
else:
cancelled += self.view.substr(s)
ht = cancelled + '</span>'
elif patterns['SEPARATOR'] in i:
ht = sep = '<span class="sep">%s</span>' % self.view.substr(r)
elif patterns['ARCHIVE'] in i:
ht = sep_archive = '<span class="sep-archive">%s</span>' % self.view.substr(r)
else:
sublime.error_message('Hey! you are not supposed to see this message.\n'
'Please, report an issue in PlainTasks repository on GitHub.')
html_doc.append(ht)
# create file
import tempfile, io
tmp_html = tempfile.NamedTemporaryFile(delete=False, suffix='.html')
with io.open('%s/PlainTasks/templates/template.html' % sublime.packages_path(), 'r', encoding='utf8') as template:
title = os.path.basename(self.view.file_name()) if self.view.file_name() else 'Export'
for line in template:
line = line.replace('$title', title).replace('$content', '\n'.join(html_doc))
tmp_html.write(line.encode('utf-8'))
tmp_html.close()
webbrowser.open_new_tab("file://%s" % tmp_html.name)
def extracting_scopes(self, edit, region, scope_name=''):
'''extract scope for each char in line wo dups, ineffective but it works?'''
scopes = []
for p in range(region.b-region.a):
sr = self.view.extract_scope(region.a + p)
# fix multi-line notes, because variable region is always a single line
if 'note' in scope_name and sr.a < region.a or sr.b - 1 > region.b:
if scopes and p == scopes[~0].b: # *text* inbetween *markups*
sr = sublime.Region(p, region.b + 1)
else: # multi-line
sr = sublime.Region(region.a, region.b + 1)
# main block, add unique entity to the list
if sr != region and sr.b - 1 <= region.b and sr not in scopes:
scopes.append(sr)
# fix intersecting regions, e.g. markup in notes
if scopes and sr.a < scopes[~0].b and p - 1 == scopes[~0].b:
scopes.append(sublime.Region(scopes[~0].b, sr.b))
if len(scopes) > 1:
# fix bullet
if scopes[0].intersects(scopes[1]):
scopes[0] = sublime.Region(scopes[0].a, scopes[1].a)
# fix text after tag(s)
if scopes[~0].b <= region.b or scopes[~0].a < region.a:
scopes.append(sublime.Region(scopes[~0].b, region.b))
for i, s in enumerate(scopes[:0:~0]):
# fix overall intersections
if s.intersects(scopes[~(i + 1)]):
if scopes[~(i + 1)].b < s.b:
scopes[~i] = sublime.Region(scopes[~(i + 1)].b, s.b)
else:
scopes[~(i + 1)] = sublime.Region(scopes[~(i + 1)].a, s.a)
return scopes