-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmakedown.py
executable file
·252 lines (197 loc) · 6.3 KB
/
makedown.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
#!/usr/bin/env python
import time
import sys
import re
import os
version = "0.0.8"
alias_to_interpreter = {
"": "bash",
"bash": "bash",
"zsh": "zsh",
"sh": "sh",
"shell": "sh",
"js": "node",
"javascript": "node",
"py": "python",
"python": "python",
"rb": "ruby",
"ruby": "ruby",
"pl": "perl",
"perl": "perl",
"php": "php",
"lua": "lua",
"tcl": "tclsh",
}
DEBUG = os.environ.get("MAKEDOWN_DEBUG") == "TRUE"
NO_COLOR = os.environ.get("MAKEDOWN_NO_COLOR") == "TRUE"
NO_WALK = os.environ.get("MAKEDOWN_NO_WALK") == "TRUE"
def red(text):
if NO_COLOR:
return text
return f"\033[91m{text}\033[0m"
def green(text):
if NO_COLOR:
return text
return f"\033[92m{text}\033[0m"
def blue(text):
if NO_COLOR:
return text
return f"\033[94m{text}\033[0m"
def yellow(text):
if NO_COLOR:
return text
return f"\033[93m{text}\033[0m"
def find_md_files():
current_dir = os.getcwd()
while True:
files = os.listdir(current_dir)
files.sort()
found = False
for file in files:
if file.lower().endswith(".md"):
yield os.path.abspath(os.path.join(current_dir, file))
found = True
if found and NO_WALK:
break
parent_dir = os.path.dirname(current_dir)
if parent_dir == current_dir:
break
current_dir = parent_dir
class Command:
def __init__(
self, file, level, name, dependencies, description, source, line_number
):
self.file = file
self.level = level
self.name = name
self.dependencies = dependencies
self.description = description
self.source = source
self.line_number = line_number
def parse_md_file(file):
commands = []
with open(file, "r") as f:
lines = [line.rstrip() for line in f.read().splitlines()]
for line_number, line in enumerate(lines):
match = re.match(r"^(#+)\s\[([^\]]+)\]\(([^\)]*)\)\s*(.*)$", line)
if not match:
continue
source = re.split(
r"^#", "\n".join(lines[line_number:]), flags=re.MULTILINE
)[1].strip()
commands.append(
Command(
file=file,
level=len(match.group(1)),
name=match.group(2).strip(),
dependencies=re.split(r"\s+", match.group(3).strip()),
description=match.group(4).strip(),
source=source,
line_number=line_number + 1,
)
)
return commands
def print_help():
max_length = 0
for file in find_md_files():
for command in parse_md_file(file):
if command.name[0] == "-":
continue
max_length = max(max_length, len(command.name))
print("More info at https://github.com/tzador/makedown")
print()
for file in find_md_files():
commands = parse_md_file(file)
if len(commands) == 0:
continue
print(blue(file))
print()
for command in commands:
if command.name[0] == "-":
continue
print(
"$",
green("makedown " + command.name)
+ (
" "
+ " " * (max_length - len(command.name))
+ " # "
+ command.description
if command.description
else ""
),
)
print()
def print_command_help(command_name):
for file in find_md_files():
commands = parse_md_file(file)
for command in commands:
if command.name != command_name:
continue
print(green("@ " + file))
print()
print(command.source)
print()
return
print(red(f"Unknown command '{command_name}'"), file=sys.stderr)
def execute_dependencies(command, executed_commands=None):
if executed_commands is None:
executed_commands = set()
for dep in command.dependencies:
if dep and dep not in executed_commands:
for file in find_md_files():
for cmd in parse_md_file(file):
if cmd.name == dep:
execute_dependencies(cmd, executed_commands)
execute_command(cmd)
executed_commands.add(dep)
break
else:
continue
break
else:
print(
red("Dependency " + dep +
" not found for command " + command.name),
file=sys.stderr,
)
def execute_command(command):
execute_dependencies(command)
for section in command.source.split("```")[1::2]:
parts = section.split("\n", 1)
alias = parts[0].strip()
script = parts[1].strip() + "\n"
interpreter = alias_to_interpreter.get(alias)
if not interpreter and not script.startswith("#!"):
print(red(f"Unknown interpreter for '{alias}'"), file=sys.stderr)
return
executable = command.file + "." + str(time.time())
with open(executable, "w") as f:
if not script.startswith("#!"):
f.write("#!/usr/bin/env " + interpreter + "\n")
f.write(script)
os.chmod(executable, 0o755)
try:
os.system(executable + " " + " ".join(sys.argv[1:]))
finally:
os.remove(executable)
pass
def main():
if len(sys.argv) == 1 or sys.argv[1] == "--help":
print_help()
return
if len(sys.argv) >= 2 and sys.argv[1] == "--version":
print("makedown-" + version)
return
if len(sys.argv) == 3 and sys.argv[2] == "--help":
print_command_help(sys.argv[1])
return
command_name = sys.argv[1]
for file in find_md_files():
for command in parse_md_file(file):
if command.name == command_name:
execute_command(command)
return
print(red(f"Unknown command '{command_name}'"), file=sys.stderr)
if __name__ == "__main__":
main()