-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-doc.py
executable file
·49 lines (39 loc) · 1.86 KB
/
update-doc.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
#!/usr/bin/env python3
import re
import subprocess
def run_command(cmd):
return subprocess.run(['bash', '-c', cmd], capture_output=True, check=True).stdout.decode("utf-8").strip("\n")
def get_file_contents(filename):
with open(filename, 'r', encoding='utf-8') as file_handler:
return file_handler.read()
output = ''
with open('README.md', 'r', encoding='utf-8') as file_handler:
is_between_command_placeholders = False
is_between_include_placeholders = False
for line in file_handler:
matches = re.match(r'\[\/\/\]\: \<\> \(command-placeholder-start "(.*)"\)', line)
if not is_between_command_placeholders and matches is not None:
is_between_command_placeholders = True
output += line
output += "```\n%s\n```\n" % (run_command(matches.group(1)))
continue
matches = re.match(r'\[\/\/\]\: \<\> \(command-placeholder-end\)', line)
if is_between_command_placeholders and matches is not None:
is_between_command_placeholders = False
output += line
continue
matches = re.match(r'\[\/\/\]\: \<\> \(include-placeholder-start "(.*)"\)', line)
if not is_between_include_placeholders and matches is not None:
is_between_include_placeholders = True
output += line
output += "```\n%s\n```\n" % (get_file_contents(matches.group(1)))
continue
matches = re.match(r'\[\/\/\]\: \<\> \(include-placeholder-end\)', line)
if is_between_include_placeholders and matches is not None:
is_between_include_placeholders = False
output += line
continue
if not is_between_command_placeholders and not is_between_include_placeholders:
output += line
with open('README.md', 'w', encoding='utf-8') as file_handler:
file_handler.write(output)