Add sort script for the Chinese version doc

This commit is contained in:
ChrisLeeGit 2016-08-25 19:12:55 +08:00
parent a7be216af7
commit a14b7360d5
2 changed files with 109 additions and 1 deletions

View File

@ -1,5 +1,8 @@
- [English](#introduction)
- [中文](#介绍)
# Introduction # 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. `asort.py` is a very simple python3 script that aims to make sure items in the README file of the repo [awesome-linux-software-cn](https://github.com/ChrisLeeGit/awesome-linux-software-cn) are sorted alphabetically.
This script only sorts items in the following topics for now (items between topic Applications and topic Setup): This script only sorts items in the following topics for now (items between topic Applications and topic Setup):
- Applications - Applications
@ -15,5 +18,33 @@ This script only sorts items in the following topics for now (items between topi
![test screenshot](./test.png) ![test screenshot](./test.png)
## Note
`asort_zh.py` works for the file [README_zh-CN.md](https://github.com/VoLuong/Awesome-Linux-Software/blob/master/README_zh-CN.md), the Chinese version of this list.
# License # License
MIT License. MIT License.
---------------------------------------------
# 介绍
`asort.py` 是一个非常简单的 python3 脚本,它负责将 [Linux 优秀软件资源大全](https://github.com/ChrisLeeGit/awesome-linux-software-cn) 仓库的 [README.md](https://github.com/ChrisLeeGit/awesome-linux-software-cn/blob/master/README.md) 文件中的条目按照字母表顺序排列,以方便查阅。
该脚本只会为如下几个主题中的条目进行排序(实际上就是在主题 *应用程序**配置* 之间的所有条目):
- 应用程序
- 命令行实用工具
- 桌面环境
- 显示管理器
- 窗口管理器
# 用法
1. 请首先按照说明将新的条目添加到合适的主题下,不用考虑排序问题;
1. 然后,确保你在系统上安装了 Python3 运行环境;
1. 打开终端,运行 `python3 asort.py` 即可。之后,你将得到一个新生成的 README 文件,并且文件中所有的在上述几个主题中的条目都已经按照字母表的顺序排列好了。
![test 截图](./test.png)
## 注意
新增的 `asort_zh.py` 专门用于中文版 [README_zh-CN.md](https://github.com/VoLuong/Awesome-Linux-Software/blob/master/README_zh-CN.md) 条目排序。用法与上面类似。
# 许可
MIT 许可协议。

77
auto_sort/asort_zh.py Normal file
View File

@ -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_zh-CN.md'
TEMP_FILE = 'temp_zh.md'
# only works for those items between BEGIN and END.
BEGIN = '## 应用'
END = '## 设置'
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_zh-CN.md')
shutil.move(TEMP_FILE, README_FILE)
if __name__ == '__main__':
main()