-
Notifications
You must be signed in to change notification settings - Fork 0
/
pymdc.py
175 lines (160 loc) · 6.06 KB
/
pymdc.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
r"Miłosz Jura, version 1.0"
import sys
import os
import re
from pathlib import Path
class Colors:
# Colors
VIOLET = '\033[95m'
DIMMED= '\033[90m'
BLUE = '\033[94m'
BLUE_LIGHT = '\033[96m'
BLUE_LIGHTER = '\033[96m'
CYAN = '\033[96m'
GREEN = '\033[92m'
GREEN_LIGHT = '\033[93m'
GREEN_LIGHTER = '\033[93m'
YELLOW = '\033[93m'
RED = '\033[91m'
# Classes
FAIL = '\033[91m'
WARNING = '\033[93m'
# Other formatting
BOLD = '\033[1m'
ITALIC = '\033[3m'
UNDERLINE = '\033[4m'
ENDC = '\033[0m'
if __name__ == "__main__":
help_text = """PyMDc, simple tool to display MD files.
pymdc.py {FILE} {ARGUMENTS}
--help - you know 8)
--no-clear - won't clear the console before printing contents
--show-raw - prints raw output before everything else
--no-filename - won't print filename at the beggining
--casual-print - won't block with input, and only display
"""
show_raw = "--show-raw" in sys.argv[1:]
# read first argument and treat it as file_name
try:
file_name = Path(sys.argv[1]).name
except IndexError:
print(help_text)
exit()
# print help if help was called
if "--help" in sys.argv:
print(help_text)
exit()
# clear the console or not, depending on settings
if not "--no-clear" in sys.argv:
os.system("clear")
# open file
file_name = f"{sys.argv[1]}"
try:
with open(file_name, "r") as f:
file_lines = f.readlines()
except FileNotFoundError:
raise FileNotFoundError(Colors.BOLD + Colors.FAIL + f"File '{file_name}' not found!\nPlease ensure file name or path is correct" + Colors.ENDC)
# clear empty lines from file end
while repr(file_lines[-1]) == "'\\n'":
file_lines.pop()
file = ""
# code_started will be used to the text formatted using ```
code_started = False
alert_started = False
comment_started = False
index = -1
# iterate through read file
for l in file_lines:
index += 1
# handle ``` code blocks
if not code_started and l.startswith("```"):
line = Colors.YELLOW+ l
code_started = True
file += line
continue
if code_started:
line = l
if l.startswith("```"):
line = l + Colors.ENDC
code_started = False
if not alert_started and l.startswith("> ["):
if "[!WARNING]" in l:
line = Colors.WARNING + l
elif "[!NOTE]" in l:
line = Colors.BLUE_LIGHT + l
elif "[!TIP]" in l:
line = Colors.GREEN_LIGHT + l
elif "[!IMPORTANT]" in l:
line = Colors.VIOLET + l
elif "[!CAUTION]" in l:
line = Colors.RED + l
else:
line = l
alert_started = True
file += line
continue
if alert_started:
# check if next line is alert as well
if not file_lines[index+1].startswith("> "):
line = l + Colors.ENDC
alert_started = False
file += line
continue
# handle other text
else:
l = re.sub(r"\s((?<!`)``(?!`))", Colors.DIMMED + " ``", l)
l = re.sub(r"((?<=\S)(?<!`)``(?!`))", "``"+ Colors.ENDC, l)
if l.startswith("###"):
line = Colors.CYAN + l + Colors.ENDC
elif l.startswith("##"):
line = Colors.GREEN+ l + Colors.ENDC
elif l.startswith("#"):
line = Colors.BLUE + l + Colors.ENDC
elif l.startswith("- ["):
line = l.replace("[ ]", Colors.BLUE+ "[ ]" + Colors.ENDC).replace("[x]", Colors.GREEN+ "[x]" + Colors.ENDC)
elif l.startswith(" - ["):
line = l.replace("[ ]", Colors.BLUE_LIGHT+ "[ ]" + Colors.ENDC).replace("[x]", Colors.GREEN_LIGHT+ "[x]" + Colors.ENDC)
elif " - [" in l: # TODO do it better, " - [" doesnt work for some reason
line = l.replace("[ ]", Colors.BLUE_LIGHT+ "[ ]" + Colors.ENDC).replace("[x]", Colors.GREEN_LIGHT+ "[x]" + Colors.ENDC)
else:
line = l
# comment
line = re.sub(r"<!--[\s\S]*?-->", "", line)
# bold
"""
# FIXME start add regex
if line.startswith("__") or line.startswith("**"):
line = line.replace("**", " " + Colors.BOLD)
line = line.replace("__", Colors.ENDC + " ")
if line.endswith("__") or line.endswith("**"):
line = line.replace("__", Colors.ENDC + " ")
line = line.replace("**", Colors.ENDC + " ")
if line.startswith("_ _") or line.startswith("* *"):
line = line.replace("* *", " " + Colors.ITALIC)
line = line.replace("_ _", " " + Colors.ITALIC)
if line.endswith("_ _") or line.endswith("* *"):
line = line.replace("_ _", Colors.ENDC + " ")
line = line.replace("* *", Colors.ENDC + " ")
# FIXME end
"""
line = line.replace(" __", " " + Colors.BOLD)
line = line.replace("__ ", Colors.ENDC + " ")
line = line.replace(" **", " " + Colors.BOLD)
line = line.replace("** ", Colors.ENDC + " ")
line = line.replace(" _ _", " " + Colors.ITALIC)
line = line.replace("_ _ ", Colors.ENDC + " ")
line = line.replace(" * *", " " + Colors.ITALIC)
line = line.replace("* * ", Colors.ENDC + " ")
file += line
# print raw line if needed
if show_raw:
print(repr(line))
# display file name
if not "--no-filename" in sys.argv:
print(f"{Colors.BOLD}=== File: {file_name}{Colors.ENDC}")
# display file contents in one ways
if "--casual-print" in sys.argv:
print(file)
else:
input(file)
"""