-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtree2md.py
82 lines (62 loc) · 2.04 KB
/
rtree2md.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
import sublime, sublime_plugin
import re
class Rtree2mdCommand(sublime_plugin.TextCommand):
def run(self, edit):
bufferText = self.view.substr(sublime.Region(0, self.view.size()))
mdText = ''
lines = bufferText.splitlines()
reH1 = re.compile(r"^(\d+)\s")
reHm = re.compile(r"^((\t+)((\d+\.)+\d))\s")
for line in lines:
matchH1 = re.search(reH1,line)
if (matchH1):
if (mdText != ''):
mdText += "\n"
mdText += re.sub(r"^\d+\s",'#',line)
mdText += "\n"
continue
matchHm = re.search(reHm,line)
if (matchHm):
mdText += "\n"
mdText += line.replace(matchHm.group(1),'#' + ('#' * len(matchHm.group(2))))
mdText += "\n"
continue
mdText += re.sub(r"\t+","",line)
mdText += "\n"
self.view.replace(edit, sublime.Region(0, self.view.size()), mdText)
class Rtreeheader2listCommand(sublime_plugin.TextCommand):
def run(self, edit):
textSelection = [a for a in self.view.sel()][0]
resultText = ''
startFlag = 0
baseHeaderLevel = 0
lines = self.view.substr(textSelection).splitlines()
baseHeadCheckRe = re.compile(r"^(\#+)\s")
listLabelArr = ['-','*']
for line in lines:
if (line == '' and startFlag == 0):
resultText += "\n"
continue
if (line == ''):
continue
matchHead = re.search(baseHeadCheckRe,line)
if (matchHead):
if (startFlag == 0):
baseHeaderLevel = len(matchHead.group(1))
startFlag = 1
listLevel = len(matchHead.group(1)) - baseHeaderLevel
if (listLevel < 0):
resultText += "\n"
resultText += line
resultText += "\n"
continue
resultText += "\t" * listLevel
resultText += listLabelArr[(listLevel + 1) % 2]
resultText += " "
resultText += re.sub(r"\#+","",line)
resultText += "\n"
continue
resultText += line
resultText += "\n"
print(resultText)
self.view.replace(edit, textSelection, resultText)