代码拉取完成,页面将自动刷新
同步操作将从 iswbm/GolangCodingTime 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import os
import re
from glob import glob
pwd = os.getcwd()
all_chapters_path = glob(pwd + "/source/c0*")
def get_chapter_name(file):
with open(file) as f:
f.readline()
chapter_name = f.readline().strip()
return chapter_name
def get_title(file):
with open(file) as f:
first_line = f.readline()
if first_line.startswith("#"):
return first_line[1:].strip()
def generate_mapping():
mapping = dict.fromkeys([os.path.basename(chapter_path) for chapter_path in all_chapters_path])
for key in mapping.keys():
chapter_file = os.path.join(pwd, "source", "chapters", key.replace("c", "p") + ".rst")
mapping[key] = get_chapter_name(chapter_file)
return mapping
def get_toc_info():
toc = {}
for dir_path in all_chapters_path:
dir_name = os.path.basename(dir_path)
chapter_toc = {}
files = glob(dir_path + "/*.md")
for file in files:
file_name = os.path.basename(file)
section = int(re.findall(r"c\d{2}_(\d{2}).md", file_name)[0])
#md_path = os.path.join("./source/", dir_name, file_name)
md_path = os.path.join("https://golang.iswbm.com/", dir_name, file_name.replace("md", "html"))
title = get_title(file)
if not title:
continue
chapter_toc[section] = (title, md_path)
toc[dir_name] = chapter_toc
return toc
def print_md_toc(toc_info, mapping):
for chapter in sorted(toc_info.items(), key=lambda item: item[0]):
posts = chapter[1]
chapter_name = mapping[chapter[0]]
print(f"- **{chapter_name}**")
for post in sorted(posts.items(), key=lambda item:item[0]):
# print title only
# print(f"{post[1][0]}")
print(" ", f"* [{post[1][0]}]({post[1][1]})")
def main():
mapping = generate_mapping()
toc_info = get_toc_info()
print_md_toc(toc_info, mapping)
if __name__ == '__main__':
main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。