-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsync.py
More file actions
executable file
·34 lines (30 loc) · 856 Bytes
/
sync.py
File metadata and controls
executable file
·34 lines (30 loc) · 856 Bytes
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
#!/usr/bin/python3
import pathlib
import re
SOURCE_PATTERN = re.compile(
r'('
r'\[source\]\(([^)]+)\)\n' # source link
r'('
r'```(\w*)\n' # First line of source
r'([^`]*)' # Contents
r'\n```'
r'|[^`]'
r'))', re.DOTALL)
fn = 'README.md'
s = open(fn).read()
for whole_match, filepath, source_block, source_type, contents in SOURCE_PATTERN.findall(s):
if not pathlib.Path(filepath).exists():
continue
if len(source_block) == 1:
trail = source_block
else:
trail = ''
contents = open(filepath).read().rstrip()
if '<launch>' in contents:
source_type = 'xml'
else:
source_type = 'python'
new_match = f'[source]({filepath})\n```{source_type}\n{contents}\n```{trail}'
s = s.replace(whole_match, new_match)
with open(fn, 'w') as f:
f.write(s)