From ebab050c3bda18f9293d41a1fa6e46633fe4a7d9 Mon Sep 17 00:00:00 2001 From: Ahmed Abdelaal Date: Fri, 11 Nov 2016 16:45:39 +0300 Subject: [PATCH 1/2] Update sort.py --- sort.py | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/sort.py b/sort.py index d809100..0c341a7 100644 --- a/sort.py +++ b/sort.py @@ -15,35 +15,32 @@ def main(): - # First, we load the current README into memory as an array of lines + # First, we load the current README into memory 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 + 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.append([line]) - last_indent = None + 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: - # Then all of the blocks are sorted individually - 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)) + sorted_file.write(final_README) if __name__ == "__main__": From 53cc1fafd01bc47ae76b1e42e5b8c81237f4f5a0 Mon Sep 17 00:00:00 2001 From: Ahmed Abdelaal Date: Fri, 11 Nov 2016 17:57:50 +0300 Subject: [PATCH 2/2] Update sort.py --- sort.py | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/sort.py b/sort.py index 0c341a7..26198d6 100644 --- a/sort.py +++ b/sort.py @@ -12,9 +12,7 @@ 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 main(): +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() @@ -42,6 +40,40 @@ def main(): 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=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()