-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown-editor.py
More file actions
146 lines (118 loc) · 3.56 KB
/
Copy pathmarkdown-editor.py
File metadata and controls
146 lines (118 loc) · 3.56 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
import os
formats = ('plain', 'bold', 'italic', 'header', 'link', 'inline-code',
'ordered-list', 'unordered-list', 'new-line')
commands = ('!help', '!done')
formatted_text = ''''''
def helper():
print('Available formatters: ', ' '.join(formats))
print('Special commands: ', ' '.join(commands))
def header():
while True:
try:
level = int(input('Level: '))
assert 1 <= level <= 6
text = input('Text: ')
except (ValueError, AssertionError):
print('The level should be within the range of 1 to 6')
continue
else:
break
return '#' * level + ' ' + text + new_line()
def plain():
text = input('Text: ')
return text
def link():
while True:
try:
label = input('Label: ')
url = input('URL: ')
except ValueError:
pass
else:
break
return f'[{label}]({url})'
def inline_code():
text = input('Text: ')
return f'`{text}`'
def bold():
text = input('Text: ')
return f'**{text}**'
def italic():
text = input('Text: ')
return f'*{text}*'
def new_line():
return '\n'
def ordered_list():
while True:
try:
rows = int(input('Number of rows: '))
assert rows > 0
collector = []
for num in range(rows):
text = input(f'Row #{num + 1}: ')
collector.append(text)
except(ValueError, AssertionError):
print('The number of rows should be greater than zero')
continue
else:
break
our_list = ""
counter = 1
for item in collector:
our_list += f'{counter}. {str(item) + new_line()}'
counter += 1
return our_list
def unordered_list():
while True:
try:
rows = int(input('Number of rows: '))
assert rows > 0
collector = []
for num in range(rows):
text = input(f'Row #{num + 1}: ')
collector.append(text)
except(ValueError, AssertionError):
print('The number of rows should be greater than zero')
continue
else:
break
our_list = ""
for item in collector:
our_list += f'* {str(item) + new_line()}'
return our_list
def formatter(format_type, text):
text += format_funcs.get(format_type)()
return text
format_funcs = dict(
{
'header': header,
'plain': plain,
'link': link,
'inline-code': inline_code,
'bold': bold,
'italic': italic,
'new-line': new_line,
'ordered-list': ordered_list,
'unordered-list': unordered_list
}
)
while True:
cmd = input('Choose a formatter: ')
if cmd == '!help':
helper()
elif cmd == '!done':
# this assumes both the python file and output.md are in the same folder
# os.path.abspath(__file__) retrieves the absolute path of the current Python script
# os.path.dirname() extracts the directory path from it.
current_dir = os.path.dirname(os.path.abspath(__file__))
filename = 'output.md'
file_path = os.path.join(current_dir, filename)
with open(str(file_path), 'w', encoding='utf-8') as file:
file.write(formatted_text)
file.close()
break
elif cmd in formats:
formatted_text = formatter(cmd, formatted_text)
print(formatted_text)
else:
print('Unknown formatting type or command')