1
|
|
|
import os |
2
|
|
|
import glob |
3
|
|
|
|
4
|
|
|
def delete_old_rst(directory): |
5
|
|
|
for fp in glob.glob(f'{directory}/*.rst'): |
6
|
|
|
os.remove(fp) |
7
|
|
|
|
8
|
|
|
def create_toc_tree(directory, files): |
9
|
|
|
toc_tree_text = ['.. toctree::\n', ' :maxdepth: 3\n', ' :hidden:\n','\n'] |
10
|
|
|
for file_name in files: |
11
|
|
|
if file_name == 'index': |
12
|
|
|
continue |
13
|
|
|
toc_tree_text.append(f' {directory}/{file_name}\n') |
14
|
|
|
|
15
|
|
|
return toc_tree_text |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def write_new_rst(directory, readme_path): |
19
|
|
|
readme = [] |
20
|
|
|
files = ['index'] |
21
|
|
|
sections = [0] |
22
|
|
|
# READ THE README |
23
|
|
|
with open(readme_path) as fp: |
24
|
|
|
readme = fp.readlines() |
25
|
|
|
|
26
|
|
|
# CAlCULATE THE SECTIONS |
27
|
|
|
count = 0 |
28
|
|
|
tags = 0 |
29
|
|
|
for line in readme: |
30
|
|
|
if '****\n' in line: |
31
|
|
|
sections.append(count-2) |
32
|
|
|
file_name = readme[count-1].strip() |
33
|
|
|
file_name = file_name.lower().replace(' ', '_') |
34
|
|
|
files.append(file_name) |
35
|
|
|
count += 1 |
36
|
|
|
if '.. TAGs\n' in line: |
37
|
|
|
tags = count |
38
|
|
|
break |
39
|
|
|
|
40
|
|
|
toc_tree_text = create_toc_tree(directory, files) |
41
|
|
|
|
42
|
|
|
# FOR EACH SECTION CREATE A NEW FILE INTO THE INDEX FOLDER |
43
|
|
|
for index, section in enumerate(sections): |
44
|
|
|
path = f'{directory}/{files[index]}.rst' |
45
|
|
|
with open(path,'w') as fp: |
46
|
|
|
if index == len(sections)-1: |
47
|
|
|
start = section |
48
|
|
|
end = tags - 1 |
49
|
|
|
else: |
50
|
|
|
start = section |
51
|
|
|
end = sections[index+1] |
52
|
|
|
|
53
|
|
|
if "license" in files[index]: |
54
|
|
|
replace_file(fp, "../LICENSE", files[index]) |
55
|
|
|
continue |
56
|
|
|
if "authors" in files[index]: |
57
|
|
|
replace_file(fp, "../AUTHORS.rst") |
58
|
|
|
continue |
59
|
|
|
fp.write(''.join(readme[start:end])) |
60
|
|
|
|
61
|
|
|
if index == 0: |
62
|
|
|
fp.write('\n') |
63
|
|
|
fp.write(''.join(readme[tags-1:])) |
64
|
|
|
|
65
|
|
|
with open('index.rst', 'w') as fp: |
66
|
|
|
fp.write(f'.. include:: {directory}/index.rst\n\n') |
67
|
|
|
fp.write(''.join(toc_tree_text)) |
68
|
|
|
|
69
|
|
|
def replace_file(file_pointer, destination, title=None): |
70
|
|
|
with open(destination,'r') as fp: |
71
|
|
|
text = fp.readlines() |
72
|
|
|
|
73
|
|
|
if title: |
74
|
|
|
file_pointer.write(f"{title.title()}\n{'*'*len(title)}\n\n") |
75
|
|
|
file_pointer.write("".join(text)) |
76
|
|
|
|
77
|
|
|
if __name__ == '__main__': |
78
|
|
|
delete_old_rst('./home') |
79
|
|
|
write_new_rst('./home', '../README.rst') |
80
|
|
|
|