Skip to content

Commit

Permalink
fix sort.py
Browse files Browse the repository at this point in the history
  • Loading branch information
vinta committed Nov 12, 2016
1 parent 6041c0b commit 3a3a011
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,32 @@
This could be extended by having nested blocks, sorting them recursively
and flattening the end structure into a list of lines. Revision 2 maybe ^.^.
"""

def sort_blocks():
# First, we load the current README into memory
with open('README.md', 'r') as read_me_file:
read_me = read_me_file.read()

# Separating the 'table of contents' from the contents (blocks)
table_of_contents = ''.join(read_me.split('- - -')[0])
blocks = ''.join(read_me.split('- - -')[1]).split('\n# ')
for i in range(len(blocks)):
if i == 0:
blocks[i] = blocks[i]+'\n'
blocks[i] = blocks[i] + '\n'
else:
blocks[i] = '#' + blocks[i]+'\n'
# Sorting the libraries
blocks[i] = '# ' + blocks[i] + '\n'

# Sorting the libraries
inner_blocks = sorted(blocks[0].split('##'))
for i in range(1 , len(inner_blocks)):
if inner_blocks[i][0] != '#':
inner_blocks[i]='##'+inner_blocks[i]
inner_blocks[i] = '##' + inner_blocks[i]
inner_blocks=''.join(inner_blocks)

# Replacing the non-sorted libraries by the sorted ones and gathering all at the final_README file
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:
sorted_file.write(final_README)

Expand Down Expand Up @@ -70,10 +71,10 @@ def main():
blocks = [''.join(sorted(block, key=lambda s: s.lower())) for block in blocks]
# And the result is written back to README.md
sorted_file.write(''.join(blocks))

# Then we call the sorting method
sort_blocks()


if __name__ == "__main__":
main()

0 comments on commit 3a3a011

Please sign in to comment.