-
Notifications
You must be signed in to change notification settings - Fork 52
/
uni.py
executable file
·68 lines (59 loc) · 1.87 KB
/
uni.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
#!/usr/bin/python3
import re
import sys
def get_first_anchor(path):
f = open(path)
while(1):
l = f.readline()
if not l:
break
elif re.match("#+ ", l):
f.close()
return '#' + (l[l.find(' ')+1:-1]).lower().replace(' ', '-')
f.close()
return '#'
def uni_file(path, n=0):
f = open(path)
content = f.readlines()
new_content = []
for i in content:
if (n != 0) and re.match("#+ ", i):
new_content.append(n*'#'+i)
elif re.match(" \* \[.+\]\(.+\.md\)", i):
ref_path = re.search("\(.+\.md\)", i).group()
ref_path = ref_path[1:-1]
new_content += ['\n']
new_content += uni_file(ref_path, n+1)
elif re.search("\]\(.+\.md\)", i):
ref = re.search("\]\(.+\.md\)", i)
start = ref.start()
end = ref.end()
ref_path = ref.group()
ref_path = ref_path[2:-1]
if path.rfind('/') != -1:
ref_path = path[:path.rfind('/')+1] + ref_path
new_line = i[:start+2] + get_first_anchor(ref_path) + i[end-1:]
new_content.append(new_line)
elif re.search("\(\.\./LICENSE\)", i):
ref = re.search("\(\.\./LICENSE\)", i)
start = ref.start()
end = ref.end()
new_line = i[:start+1] + \
"https://creativecommons.org/licenses/by/4.0/legalcode" + \
i[end-1:]
new_content.append(new_line)
else:
new_content.append(i)
f.close()
return new_content
def main():
l0_file = [i for i in sys.argv if i.find('/') == -1]
new_content = []
for i in l0_file:
new_content += uni_file(i)
new_content += ['\n']
f = open("uni.md", "w")
f.writelines(new_content)
f.close()
if __name__ == '__main__':
main()