2020-08-02 02:51:49 -04:00
|
|
|
#!/usr/bin/env python
|
2014-07-03 02:30:24 -04:00
|
|
|
# coding: utf-8
|
|
|
|
|
2014-07-02 18:35:39 -04:00
|
|
|
"""
|
|
|
|
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 ^.^.
|
|
|
|
"""
|
2016-11-12 09:26:46 -05:00
|
|
|
|
2016-11-11 09:57:50 -05:00
|
|
|
def sort_blocks():
|
2016-11-11 08:45:39 -05:00
|
|
|
# First, we load the current README into memory
|
2014-07-02 18:35:39 -04:00
|
|
|
with open('README.md', 'r') as read_me_file:
|
2016-11-11 08:45:39 -05:00
|
|
|
read_me = read_me_file.read()
|
2016-11-12 09:26:46 -05:00
|
|
|
|
2016-11-11 08:45:39 -05:00
|
|
|
# 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:
|
2016-11-12 09:26:46 -05:00
|
|
|
blocks[i] = blocks[i] + '\n'
|
2014-07-02 18:35:39 -04:00
|
|
|
else:
|
2016-11-12 09:26:46 -05:00
|
|
|
blocks[i] = '# ' + blocks[i] + '\n'
|
|
|
|
|
|
|
|
# Sorting the libraries
|
2016-11-11 08:45:39 -05:00
|
|
|
inner_blocks = sorted(blocks[0].split('##'))
|
2019-08-08 01:30:38 -04:00
|
|
|
for i in range(1, len(inner_blocks)):
|
2016-11-11 08:45:39 -05:00
|
|
|
if inner_blocks[i][0] != '#':
|
2016-11-12 09:26:46 -05:00
|
|
|
inner_blocks[i] = '##' + inner_blocks[i]
|
2019-08-08 01:30:38 -04:00
|
|
|
inner_blocks = ''.join(inner_blocks)
|
2016-11-11 08:45:39 -05:00
|
|
|
|
|
|
|
# Replacing the non-sorted libraries by the sorted ones and gathering all at the final_README file
|
|
|
|
blocks[0] = inner_blocks
|
2016-11-12 09:26:46 -05:00
|
|
|
final_README = table_of_contents + '- - -' + ''.join(blocks)
|
|
|
|
|
2014-07-02 18:35:39 -04:00
|
|
|
with open('README.md', 'w+') as sorted_file:
|
2016-11-11 08:45:39 -05:00
|
|
|
sorted_file.write(final_README)
|
2014-07-03 02:30:24 -04:00
|
|
|
|
2016-11-11 09:57:50 -05:00
|
|
|
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
|
2019-08-08 01:30:38 -04:00
|
|
|
blocks = [
|
|
|
|
''.join(sorted(block, key=str.lower)) for block in blocks
|
|
|
|
]
|
2016-11-11 09:57:50 -05:00
|
|
|
# And the result is written back to README.md
|
|
|
|
sorted_file.write(''.join(blocks))
|
2016-11-12 09:26:46 -05:00
|
|
|
|
2016-11-11 09:57:50 -05:00
|
|
|
# Then we call the sorting method
|
|
|
|
sort_blocks()
|
2016-11-12 09:26:46 -05:00
|
|
|
|
2014-07-02 18:35:39 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|