fix sort.py

This commit is contained in:
Vinta 2016-11-12 22:26:46 +08:00
parent 6041c0b211
commit 3a3a01150d

View File

@ -12,6 +12,7 @@
This could be extended by having nested blocks, sorting them recursively This could be extended by having nested blocks, sorting them recursively
and flattening the end structure into a list of lines. Revision 2 maybe ^.^. and flattening the end structure into a list of lines. Revision 2 maybe ^.^.
""" """
def sort_blocks(): def sort_blocks():
# First, we load the current README into memory # First, we load the current README into memory
with open('README.md', 'r') as read_me_file: with open('README.md', 'r') as read_me_file:
@ -22,20 +23,20 @@ def sort_blocks():
blocks = ''.join(read_me.split('- - -')[1]).split('\n# ') blocks = ''.join(read_me.split('- - -')[1]).split('\n# ')
for i in range(len(blocks)): for i in range(len(blocks)):
if i == 0: if i == 0:
blocks[i] = blocks[i]+'\n' blocks[i] = blocks[i] + '\n'
else: else:
blocks[i] = '#' + blocks[i]+'\n' blocks[i] = '# ' + blocks[i] + '\n'
# Sorting the libraries # Sorting the libraries
inner_blocks = sorted(blocks[0].split('##')) inner_blocks = sorted(blocks[0].split('##'))
for i in range(1 , len(inner_blocks)): for i in range(1 , len(inner_blocks)):
if inner_blocks[i][0] != '#': if inner_blocks[i][0] != '#':
inner_blocks[i]='##'+inner_blocks[i] inner_blocks[i] = '##' + inner_blocks[i]
inner_blocks=''.join(inner_blocks) inner_blocks=''.join(inner_blocks)
# Replacing the non-sorted libraries by the sorted ones and gathering all at the final_README file # Replacing the non-sorted libraries by the sorted ones and gathering all at the final_README file
blocks[0] = inner_blocks blocks[0] = inner_blocks
final_README = table_of_contents + '- - -'+ ''.join(blocks) final_README = table_of_contents + '- - -' + ''.join(blocks)
with open('README.md', 'w+') as sorted_file: with open('README.md', 'w+') as sorted_file:
sorted_file.write(final_README) sorted_file.write(final_README)