diff --git a/auto_sort/README.md b/auto_sort/README.md new file mode 100644 index 0000000..f34c846 --- /dev/null +++ b/auto_sort/README.md @@ -0,0 +1,19 @@ +# Introduction +`asort.py` is a very simple python3 script that aims to make sure items in the README file of the repo [Awesome-Linux-Software](https://github.com/VoLuong/Awesome-Linux-Software) are sorted alphabetically. + +This script only sorts items in the following topics for now (items between topic Applications and topic Setup): +- Applications +- Command Line Utilities +- Desktop Environments +- Display Managers +- Window Managers + +# Usage +1. Add items to corresponding topics, don't worry about the order of those items. +1. Make sure you've installed python3 environment on your system. +1. Open a terminal, type `python3 asort.py` to run this script. After that, you'll get a new README file with all the items in the above topics being sorted alphabetically. + +[test screenshot](./test.png) + +# License +MIT License. diff --git a/auto_sort/asort.py b/auto_sort/asort.py new file mode 100644 index 0000000..3add637 --- /dev/null +++ b/auto_sort/asort.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +# -*-coding: utf-8-*- +# Author : Christopher L +# Blog : http://blog.chriscabin.com +# GitHub : https://www.github.com/chrisleegit +# File : asort.py +# Date : 2016/08/22 11:12 +# Version: 0.1 +# Description: A very simple python script that can sort items alphabetically. + +import os +import shutil + + +README_FILE = '../README.md' +TEMP_FILE = 'temp.md' + +# only works for those items between BEGIN and END. +BEGIN = '## Applications' +END = '## Setup' + + +def main(): + global README_FILE + + # make sure the script can find file: README.md + README_FILE = os.path.abspath(README_FILE) + + if not os.path.exists(README_FILE): + print('Error: no such file or directory: {}'.format(README_FILE)) + exit(1) + + sort_enable = False + items = list() + + print('Loading file: {}'.format(README_FILE)) + + # read file: README.md + with open(README_FILE) as infile, open(TEMP_FILE, 'w') as outfile: + # process each line + for line in infile: + if not sort_enable and BEGIN in line: + sort_enable = True + + # if sort_enable and END in line: + # sort_enable = False + + if sort_enable: + line = line.strip() + + # each item starts with a character '-' (maybe '*' and '+') + if line.startswith(('-', '*', '+')): + items.append(line) + elif line.startswith('#'): + sort_enable = False if END in line else True + + # when we meet the next header, we should stop adding new item to the list. + for item in sorted(items, key=lambda x: x.upper()): + # write the ordered list to the temporary file. + print(item, file=outfile) + print('', file=outfile) + items.clear() + + # remember to put the next header in the temporary file. + print(line, file=outfile) + else: + print(line, end='', file=outfile) + else: + print(line, end='', file=outfile) + + print('Replace the original file: README.md') + shutil.move(TEMP_FILE, README_FILE) + + +if __name__ == '__main__': + main() + diff --git a/auto_sort/test.png b/auto_sort/test.png new file mode 100644 index 0000000..0db4a2a Binary files /dev/null and b/auto_sort/test.png differ