-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown2mindnode.py
executable file
·66 lines (59 loc) · 2.2 KB
/
markdown2mindnode.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
#!/usr/bin/env python
import pyperclip
import os
import sys
def markdown_refactor_to_mindnode(file):
refactor_strs = []
with open(file, mode='r', encoding='utf-8') as f:
for line in f :
list_symbol_index = line.find('-')
num_of_space = list_symbol_index * 2
newline = ''.join([' '] * num_of_space) + line.replace('-', ' ').lstrip()
refactor_strs.append(newline)
return refactor_strs
def save_to_clipboard(file):
refactor_strs = markdown_refactor_to_mindnode(file)
str = ''.join(refactor_strs)
pyperclip.copy(str)
print("Successfully saved to clipboard!")
def save_as_refactored_file(file):
index = file.find('.')
name = file[:index]
appidx = os.path.splitext(file)[-1]
newfile = name + '_refactor' + appidx
refactor_strs = markdown_refactor_to_mindnode(file)
with open(newfile, mode='w', encoding='utf-8') as f:
for line in refactor_strs:
f.write(line)
print("Successfully saved to " + newfile + '!')
if __name__ == '__main__':
file = '#'
try:
if len(sys.argv) > 3: # there is Space in filename
if sys.argv[-1] in ('file', 'clipboard'):
file = ' '.join(sys.argv[1:-1])
else:
file = ' '.join(sys.argv[1:])
else:
file = sys.argv[1]
except Exception:
print("You must provide a .md file!")
finally:
if file != '#':
try:
mode = sys.argv[-1]
if mode.find('.md') != -1: # actually don't provide the second param
raise Exception
except Exception:
mode = 'clipboard'
print("By default, the transformed content is copied to clipboard.")
finally:
if mode == 'clipboard':
save_to_clipboard(file)
elif mode == 'file':
save_as_refactored_file(file)
else:
print("The second parameter option is [clipboard | file], copy to clipboard by default.")
save_to_clipboard(file)
# file = '/Users/doublez/Downloads/README.md'
# markdown_refactor_to_mindnode(file)