2016-08-25 07:12:55 -04:00
|
|
|
#!/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.
|
|
|
|
|
2017-08-21 05:53:37 -04:00
|
|
|
from __future__ import print_function
|
2016-08-25 07:12:55 -04:00
|
|
|
import os
|
|
|
|
import shutil
|
2018-04-19 11:51:04 -04:00
|
|
|
import re
|
2016-08-25 07:12:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
README_FILE = '../README_zh-CN.md'
|
|
|
|
TEMP_FILE = 'temp_zh.md'
|
|
|
|
|
|
|
|
# only works for those items between BEGIN and END.
|
|
|
|
BEGIN = '## 应用'
|
|
|
|
END = '## 设置'
|
|
|
|
|
2018-04-19 11:51:04 -04:00
|
|
|
regex = re.compile(r"[^[]*\[([^]]*)\]")
|
2016-08-25 07:12:55 -04:00
|
|
|
|
|
|
|
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):
|
2023-09-06 13:12:38 -04:00
|
|
|
print(f'Error: no such file or directory: {README_FILE}')
|
2016-08-25 07:12:55 -04:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
sort_enable = False
|
2023-09-06 13:12:38 -04:00
|
|
|
items = []
|
2016-08-25 07:12:55 -04:00
|
|
|
|
2023-09-06 13:12:38 -04:00
|
|
|
print(f'Loading file: {README_FILE}')
|
2016-08-25 07:12:55 -04:00
|
|
|
|
|
|
|
# read file: README.md
|
2023-09-06 13:12:38 -04:00
|
|
|
with (open(README_FILE) as infile, open(TEMP_FILE, 'w') as outfile):
|
2016-08-25 07:12:55 -04:00
|
|
|
# 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
|
|
|
|
|
2023-09-06 13:12:38 -04:00
|
|
|
if sort_enable and line.startswith(('-')):
|
|
|
|
line = line.strip()
|
|
|
|
items.append(line)
|
|
|
|
elif sort_enable and not line.startswith(('-')) and line == '\n':
|
|
|
|
# when we meet the next header, we should stop adding new item to the list.
|
|
|
|
for item in sorted(items, key=lambda x: regex.findall(x.upper())[len(regex.findall(x.upper()))-1]):
|
|
|
|
# write the ordered list to the temporary file.
|
|
|
|
print(item, file=outfile)
|
|
|
|
items.clear()
|
|
|
|
|
|
|
|
# remember to put the next header in the temporary file.
|
|
|
|
print(line, end='', file=outfile)
|
|
|
|
elif (
|
|
|
|
sort_enable
|
|
|
|
and not line.startswith(('-'))
|
|
|
|
and line != '\n'
|
|
|
|
and line.startswith('#')
|
|
|
|
):
|
|
|
|
sort_enable = END not in line
|
|
|
|
print(line, end='', file=outfile)
|
2016-08-25 07:12:55 -04:00
|
|
|
else:
|
|
|
|
print(line, end='', file=outfile)
|
|
|
|
print('Replace the original file: README_zh-CN.md')
|
|
|
|
shutil.move(TEMP_FILE, README_FILE)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
|