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