-
Notifications
You must be signed in to change notification settings - Fork 12
/
sort.py
100 lines (86 loc) · 4.07 KB
/
sort.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# coding: utf-8
"""
The approach taken is explained below. I decided to do it simply.
Initially I was considering parsing the data into some sort of
structure and then generating an appropriate README. I am still
considering doing it - but for now this should work. The only issue
I see is that it only sorts the entries at the lowest level, and that
the order of the top-level contents do not match the order of the actual
entries.
This could be extended by having nested blocks, sorting them recursively
and flattening the end structure into a list of lines. Revision 2 maybe ^.^.
"""
def sort_blocks():
# First, we load the current README into memory
with open('README.md', 'r', encoding="latin-1") as read_me_file:
read_me = read_me_file.read()
# Separating the 'table of contents' from the contents (blocks)
table_of_contents = ''.join(read_me.split('---')[0])
blocks = ''.join(read_me.split('---')[1]).split('\n# ')
# print(table_of_contents)
for i in range(len(blocks)):
if i == 0:
blocks[i] = blocks[i] + '\n'
else:
blocks[i] = '# ' + blocks[i] + '\n'
# Sorting the libraries
# inner_blocks = sorted(blocks[0].split('##')) ## this sorts categories as well which we do not want
inner_blocks = blocks[0].split('###')
for i in range(1, len(inner_blocks)):
if inner_blocks[i][0] != '#':
inner_blocks[i] = '###' + inner_blocks[i]
inner_blocks = ''.join(inner_blocks)
# Replacing the non-sorted libraries by the sorted ones and gathering all at the final_README file
blocks[0] = inner_blocks
final_README = table_of_contents + '---\n\n' + ''.join(blocks).strip() + '\n'
with open('README.md', 'w+', encoding="latin-1") as sorted_file:
sorted_file.write(final_README)
def main():
# First, we load the current README into memory as an array of lines
with open('README.md', 'r', encoding="latin-1") as read_me_file:
read_me = read_me_file.readlines()
# Then we cluster the lines together as blocks
# Each block represents a collection of lines that should be sorted
# This was done by assuming only links ([...](...)) are meant to be sorted
# Clustering is done by indentation
blocks = []
last_indent = None
for line in read_me:
s_line = line.lstrip()
indent = len(line) - len(s_line)
if any([s_line.startswith(s) for s in ['* [', '- [']]):
if indent == last_indent:
blocks[-1].append(line)
else:
blocks.append([line])
last_indent = indent
else:
blocks.append([line])
last_indent = None
ignore_sort_list = ["- [Awesome Utrecht University](#awesome-utrecht-university)",
"- [Projects](#projects)",
"- [Research code](#research-code)",
"- [Research software](#research-software)",
"- [Research data](#research-data)",
"- [Research project management](#research-project-management)",
"- [Education and workshops](#education-and-workshops)",
"- [Collaboration groups](#collaboration-groups)",
"- [Add project to this list](#add-project-to-this-list)",
"- [Background](#background)",
"- [What is an Awesome list?](#what-is-an-awesome-list)",
"- [Initial project collection](#initial-project-collection)",
"- [Implementing Awesome lists for your university](#implementing-awesome-lists-for-your-university)",
"- [Contact](#contact)"]
# print(not any(substring in ''.join(blocks[20]) for substring in ignore_sort_list))
with open('README.md', 'w+', encoding="latin-1") as sorted_file:
# Then all of the blocks are sorted individually
blocks = [
''.join(sorted(block, key=str.lower)) if not any(toc_block in ''.join(block) for toc_block in ignore_sort_list) else ''.join(block) for block in blocks # do not sort ToC
]
# And the result is written back to README.md
sorted_file.write(''.join(blocks))
# Then we call the sorting method
sort_blocks()
if __name__ == "__main__":
main()