代码拉取完成,页面将自动刷新
# -*- 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')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。