2 Star 4 Fork 2

ColinUED/Standard

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
spec-gen-4github.py 2.76 KB
一键复制 编辑 原始数据 按行查看 历史
allenxieyusheng 提交于 2016-12-07 22:05 . baidu
# -*- coding: utf-8 -*-
import os
import re
import urllib
def genSpec(type):
mdContent = []
catalogFile = open(type + '/README.md', 'r')
rootChapter = {
'title': 'root',
'children': []
}
readState = [ 0, 0, 0, 0 ]
# read title
titleFile = open(type + '/title.md', 'r');
title = titleFile.read();
titleFile.close()
for line in catalogFile.readlines():
match = re.search('\[[^\]]+\]\(([^\)]+)\)', line)
if match is not None:
chapter = type + '/' + match.group(1)
if os.path.exists(chapter):
genChapter(chapter, mdContent, readState, rootChapter)
catalogFile.close()
output = open(type + '-github.md', 'w')
output.write(title + genCatalog(rootChapter) + ''.join(mdContent))
output.close()
def genCatalog(root):
catalogContent = []
catalogContent.append('\n\n')
for node in root['children']:
genCatalogNode(node, catalogContent, 0)
catalogContent.append('\n\n')
return '\n\n'.join(catalogContent)
def genCatalogNode(node, catalogContent, level):
catalogContent.append(level * '  ' + '[{}](#user-content-{})'.format(node['title'], titleHash(node['title'])))
for child in node['children']:
genCatalogNode(child, catalogContent, level + 1)
def genChapter(chapter, contentBucket, readState, rootChapter):
chapterFile = open(chapter, 'r')
for line in chapterFile:
match = re.search('^(#(#+)\s+)[^\[]', line)
if match is not None:
level = len(match.group(2))
title = line[len(match.group(1)):]
titleIndex = []
parentNode = rootChapter
for i in range(4):
if i == level - 1:
readState[i] += 1
titleIndex.append(str(readState[i]))
title = '.'.join(titleIndex) + ' ' + title.strip()
parentNode['children'].append({
'title': title,
'children': []
})
elif i >= level:
readState[i] = 0
else:
parentNode = parentNode['children'][readState[i] - 1]
titleIndex.append(str(readState[i]))
contentBucket.append((level + 1) * '#' + ' ' + title + '\n\n')
else:
contentBucket.append(line)
contentBucket.append('\n\n\n')
chapterFile.close()
def titleHash(title):
return urllib.quote(re.sub(
'\s+',
' ',
title.strip().lower().replace('.', '').replace('(', '').replace(')', '').replace('!', '')
).replace(' ', '-'))
genSpec('javascript')
genSpec('css')
genSpec('html')
genSpec('es-next')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/SOHONET/Standard.git
[email protected]:SOHONET/Standard.git
SOHONET
Standard
Standard
master

搜索帮助