Skip to content

Commit ebab050

Browse files
author
Ahmed Abdelaal
authored
Update sort.py
1 parent 60734b1 commit ebab050

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

sort.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,32 @@
1515

1616

1717
def main():
18-
# First, we load the current README into memory as an array of lines
18+
# First, we load the current README into memory
1919
with open('README.md', 'r') as read_me_file:
20-
read_me = read_me_file.readlines()
21-
22-
# Then we cluster the lines together as blocks
23-
# Each block represents a collection of lines that should be sorted
24-
# This was done by assuming only links ([...](...)) are meant to be sorted
25-
# Clustering is done by indentation
26-
blocks = []
27-
last_indent = None
28-
for line in read_me:
29-
s_line = line.lstrip()
30-
indent = len(line) - len(s_line)
31-
32-
if any([s_line.startswith(s) for s in ['* [', '- [']]):
33-
if indent == last_indent:
34-
blocks[-1].append(line)
35-
else:
36-
blocks.append([line])
37-
last_indent = indent
20+
read_me = read_me_file.read()
21+
22+
# Separating the 'table of contents' from the contents (blocks)
23+
table_of_contents = ''.join(read_me.split('- - -')[0])
24+
blocks = ''.join(read_me.split('- - -')[1]).split('\n# ')
25+
for i in range(len(blocks)):
26+
if i == 0:
27+
blocks[i] = blocks[i]+'\n'
3828
else:
39-
blocks.append([line])
40-
last_indent = None
41-
29+
blocks[i] = '#' + blocks[i]+'\n'
30+
31+
# Sorting the libraries
32+
inner_blocks = sorted(blocks[0].split('##'))
33+
for i in range(1 , len(inner_blocks)):
34+
if inner_blocks[i][0] != '#':
35+
inner_blocks[i]='##'+inner_blocks[i]
36+
inner_blocks=''.join(inner_blocks)
37+
38+
# Replacing the non-sorted libraries by the sorted ones and gathering all at the final_README file
39+
blocks[0] = inner_blocks
40+
final_README = table_of_contents + '- - -'+ ''.join(blocks)
41+
4242
with open('README.md', 'w+') as sorted_file:
43-
# Then all of the blocks are sorted individually
44-
blocks = [''.join(sorted(block, key=lambda s: s.lower())) for block in blocks]
45-
# And the result is written back to README.md
46-
sorted_file.write(''.join(blocks))
43+
sorted_file.write(final_README)
4744

4845

4946
if __name__ == "__main__":

0 commit comments

Comments
 (0)