|
15 | 15 |
|
16 | 16 |
|
17 | 17 | 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 |
19 | 19 | 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' |
38 | 28 | 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 | + |
42 | 42 | 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) |
47 | 44 |
|
48 | 45 |
|
49 | 46 | if __name__ == "__main__": |
|
0 commit comments