mirror of
https://github.com/vinta/awesome-python.git
synced 2025-01-30 21:23:42 +00:00
Add script to sort links in README.md by
indentation and content
This commit is contained in:
parent
b5bd4d0ad0
commit
f8fac37c35
153
sort.py
153
sort.py
|
@ -2,82 +2,111 @@
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
|
||||||
"""
|
"""
|
||||||
The approach taken is explained below. I decided to do it simply.
|
This script sorts the entries in the README.md file. It clusters the lines
|
||||||
Initially I was considering parsing the data into some sort of
|
into blocks based on indentation and sorts each block individually.
|
||||||
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():
|
def sort_readme():
|
||||||
# First, we load the current README into memory
|
# 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.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:
|
with open('README.md', 'r') as read_me_file:
|
||||||
read_me = read_me_file.readlines()
|
read_me = read_me_file.readlines()
|
||||||
|
|
||||||
# Then we cluster the lines together as blocks
|
# Cluster lines into blocks based on indentation
|
||||||
# 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 = []
|
blocks = []
|
||||||
|
current_block = []
|
||||||
last_indent = None
|
last_indent = None
|
||||||
|
|
||||||
for line in read_me:
|
for line in read_me:
|
||||||
s_line = line.lstrip()
|
stripped_line = line.lstrip()
|
||||||
indent = len(line) - len(s_line)
|
indent = len(line) - len(stripped_line)
|
||||||
|
|
||||||
if any([s_line.startswith(s) for s in ['* [', '- [']]):
|
# Detecting list items by starting character
|
||||||
|
if stripped_line.startswith(('* [', '- [')):
|
||||||
if indent == last_indent:
|
if indent == last_indent:
|
||||||
blocks[-1].append(line)
|
current_block.append(line)
|
||||||
else:
|
else:
|
||||||
blocks.append([line])
|
if current_block:
|
||||||
|
blocks.append(current_block)
|
||||||
|
current_block = [line]
|
||||||
last_indent = indent
|
last_indent = indent
|
||||||
else:
|
else:
|
||||||
|
if current_block:
|
||||||
|
blocks.append(current_block)
|
||||||
|
current_block = []
|
||||||
blocks.append([line])
|
blocks.append([line])
|
||||||
last_indent = None
|
last_indent = None
|
||||||
|
|
||||||
with open('README.md', 'w+') as sorted_file:
|
if current_block:
|
||||||
# Then all of the blocks are sorted individually
|
blocks.append(current_block)
|
||||||
blocks = [
|
|
||||||
''.join(sorted(block, key=str.lower)) for block in blocks
|
# Sort each block individually
|
||||||
]
|
sorted_blocks = []
|
||||||
# And the result is written back to README.md
|
for block in blocks:
|
||||||
sorted_file.write(''.join(blocks))
|
if block[0].lstrip().startswith(('* [', '- [')):
|
||||||
|
sorted_blocks.append(sorted(block, key=lambda s: s.lower()))
|
||||||
# Then we call the sorting method
|
else:
|
||||||
sort_blocks()
|
sorted_blocks.append(block)
|
||||||
|
|
||||||
|
# Write the sorted blocks back to the README.md file
|
||||||
|
with open('README.md', 'w') as sorted_file:
|
||||||
|
for block in sorted_blocks:
|
||||||
|
sorted_file.write(''.join(block))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
sort_readme()
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
"""
|
||||||
|
This script sorts the entries in the README.md file. It clusters the lines
|
||||||
|
into blocks based on indentation and sorts each block individually.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def sort_readme():
|
||||||
|
# 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()
|
||||||
|
|
||||||
|
# Cluster lines into blocks based on indentation
|
||||||
|
blocks = []
|
||||||
|
current_block = []
|
||||||
|
last_indent = None
|
||||||
|
|
||||||
|
for line in read_me:
|
||||||
|
stripped_line = line.lstrip()
|
||||||
|
indent = len(line) - len(stripped_line)
|
||||||
|
|
||||||
|
# Detecting list items by starting character
|
||||||
|
if stripped_line.startswith(('* [', '- [')):
|
||||||
|
if indent == last_indent:
|
||||||
|
current_block.append(line)
|
||||||
|
else:
|
||||||
|
if current_block:
|
||||||
|
blocks.append(current_block)
|
||||||
|
current_block = [line]
|
||||||
|
last_indent = indent
|
||||||
|
else:
|
||||||
|
if current_block:
|
||||||
|
blocks.append(current_block)
|
||||||
|
current_block = []
|
||||||
|
blocks.append([line])
|
||||||
|
last_indent = None
|
||||||
|
|
||||||
|
if current_block:
|
||||||
|
blocks.append(current_block)
|
||||||
|
|
||||||
|
# Sort each block individually
|
||||||
|
sorted_blocks = []
|
||||||
|
for block in blocks:
|
||||||
|
if block[0].lstrip().startswith(('* [', '- [')):
|
||||||
|
sorted_blocks.append(sorted(block, key=lambda s: s.lower()))
|
||||||
|
else:
|
||||||
|
sorted_blocks.append(block)
|
||||||
|
|
||||||
|
# Write the sorted blocks back to the README.md file
|
||||||
|
with open('README.md', 'w') as sorted_file:
|
||||||
|
for block in sorted_blocks:
|
||||||
|
sorted_file.write(''.join(block))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sort_readme()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user