-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
find-unlinked.py
executable file
·73 lines (62 loc) · 2.05 KB
/
find-unlinked.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
#!/usr/bin/env python3
"""
Running this script finds all RFCs that are in the `text` folder but not
referenced by the `README.md` file. These RFCs can be harder to find as
a result. Running it helps one to quickly remedy this situation.
"""
import os
import re
import sys
_link_re = re.compile(r"\[[^\]]*\]\(([^)]*)\)")
def get_first_sentence(lines):
text = " ".join(lines)
try:
index = text.index(".")
except ValueError:
return text.strip()
return text[:index].strip().rstrip(".")
def main():
linked = set()
with open("README.md") as f:
for line in f:
link = _link_re.search(line)
if link is None:
continue
target = link.group(1)
if target.startswith("text/"):
linked.add(target[5:])
exists = {}
for filename in os.listdir("text"):
if filename.endswith(".md"):
in_summary = False
summary = []
with open("text/" + filename) as f:
for line in f:
if line.strip().startswith("#"):
extra = line.strip()[1:].strip()
if extra.lower() == "summary":
in_summary = True
continue
else:
if in_summary:
break
if in_summary:
summary.append(line)
exists[filename] = get_first_sentence(summary)
unlinked = exists.keys() - linked
if unlinked:
print("Files not linked in README:")
for filename in sorted(unlinked):
print(f" * {filename}")
print()
print("Proposed additions to README:")
print()
for filename in sorted(unlinked):
short = filename[:-3]
summary = exists[filename]
print(f"* [{short}](text/{filename}): {summary}")
sys.exit(1)
else:
print("All files added to README")
if __name__ == "__main__":
main()