|
12 | 12 | This could be extended by having nested blocks, sorting them recursively |
13 | 13 | and flattening the end structure into a list of lines. Revision 2 maybe ^.^. |
14 | 14 | """ |
| 15 | + |
15 | 16 | def sort_blocks(): |
16 | 17 | # First, we load the current README into memory |
17 | 18 | with open('README.md', 'r') as read_me_file: |
18 | 19 | read_me = read_me_file.read() |
19 | | - |
| 20 | + |
20 | 21 | # Separating the 'table of contents' from the contents (blocks) |
21 | 22 | table_of_contents = ''.join(read_me.split('- - -')[0]) |
22 | 23 | blocks = ''.join(read_me.split('- - -')[1]).split('\n# ') |
23 | 24 | for i in range(len(blocks)): |
24 | 25 | if i == 0: |
25 | | - blocks[i] = blocks[i]+'\n' |
| 26 | + blocks[i] = blocks[i] + '\n' |
26 | 27 | else: |
27 | | - blocks[i] = '#' + blocks[i]+'\n' |
28 | | - |
29 | | - # Sorting the libraries |
| 28 | + blocks[i] = '# ' + blocks[i] + '\n' |
| 29 | + |
| 30 | + # Sorting the libraries |
30 | 31 | inner_blocks = sorted(blocks[0].split('##')) |
31 | 32 | for i in range(1 , len(inner_blocks)): |
32 | 33 | if inner_blocks[i][0] != '#': |
33 | | - inner_blocks[i]='##'+inner_blocks[i] |
| 34 | + inner_blocks[i] = '##' + inner_blocks[i] |
34 | 35 | inner_blocks=''.join(inner_blocks) |
35 | 36 |
|
36 | 37 | # Replacing the non-sorted libraries by the sorted ones and gathering all at the final_README file |
37 | 38 | blocks[0] = inner_blocks |
38 | | - final_README = table_of_contents + '- - -'+ ''.join(blocks) |
39 | | - |
| 39 | + final_README = table_of_contents + '- - -' + ''.join(blocks) |
| 40 | + |
40 | 41 | with open('README.md', 'w+') as sorted_file: |
41 | 42 | sorted_file.write(final_README) |
42 | 43 |
|
@@ -70,10 +71,10 @@ def main(): |
70 | 71 | blocks = [''.join(sorted(block, key=lambda s: s.lower())) for block in blocks] |
71 | 72 | # And the result is written back to README.md |
72 | 73 | sorted_file.write(''.join(blocks)) |
73 | | - |
| 74 | + |
74 | 75 | # Then we call the sorting method |
75 | 76 | sort_blocks() |
76 | | - |
| 77 | + |
77 | 78 |
|
78 | 79 | if __name__ == "__main__": |
79 | 80 | main() |
0 commit comments