11import os
22import re
3+ import git
4+ import linecache
35from glob import glob
46
57pwd = os .getcwd ()
6- all_chapters_path = glob (pwd + "/source/c0*" ) + glob (pwd + "/source/c1*" )
7-
8+ source_dir = os .path .join (pwd , "source" )
89
910def get_chapter_name (file ):
10- with open (file ) as f :
11- f .readline ()
12- chapter_name = f .readline ().strip ()
13-
14- return chapter_name
11+ return linecache .getline (file , 2 ).strip ()
1512
1613def get_title (file ):
17- with open (file ) as f :
18- first_line = f .readline ()
14+ first_line = linecache .getline (file , 1 )
1915
2016 if first_line .startswith ("#" ):
21- return first_line [1 :].strip ()
17+ return first_line .replace ("# " , "" ).strip ()
18+
19+ def get_current_brance ():
20+ repo = git .Repo (pwd )
21+ all_branchs = repo .git .branch ()
22+
23+ current_branch = ""
24+ for branch in all_branchs .split ('\n ' ):
25+ if "*" in branch :
26+ current_branch = branch .replace ("* " , "" )
27+ if current_branch == "master" :
28+ current_branch = "latest"
29+ break
30+
31+ return current_branch
32+
33+ def get_all_chapter ():
34+ all_chapters_path = []
35+ os .chdir (source_dir )
36+
37+ for dir_name in glob ("c*" ):
38+ if dir_name == "chapters" or dir_name == "conf.py" :
39+ continue
40+ all_chapters_path .append (os .path .join (dir_name ))
41+ return all_chapters_path
2242
23- def generate_mapping ():
43+ def generate_mapping (all_chapters_path ):
2444 mapping = dict .fromkeys ([os .path .basename (chapter_path ) for chapter_path in all_chapters_path ])
2545 for key in mapping .keys ():
2646 chapter_file = os .path .join (pwd , "source" , "chapters" , key .replace ("c" , "p" ) + ".rst" )
2747 mapping [key ] = get_chapter_name (chapter_file )
2848
2949 return mapping
3050
31- def get_toc_info ():
51+ def get_toc_info (all_chapters_path , current_branch ):
3252 toc = {}
33- for dir_path in all_chapters_path :
34- dir_name = os .path .basename (dir_path )
3553
54+ for dir_name in all_chapters_path :
3655 chapter_toc = {}
37- files = glob ( dir_path + "/*.md" )
56+ os . chdir ( os . path . join ( source_dir , dir_name ) )
3857
39- for file in files :
40- file_name = os .path .basename (file )
58+ for file_name in sorted (glob (dir_name + "*.md" )):
4159 section = int (re .findall (r"c\d{2}_(\d{2}).md" , file_name )[0 ])
4260
43- #md_path = os.path.join("./source/", dir_name, file_name)
44- md_path = os .path .join ("http://python.iswbm.com/en/latest/" , dir_name , file_name .replace ("md" , "html" ))
45- title = get_title (file )
61+ md_path = os .path .join ("http://pycharm.iswbm.com/zh_CN/" , current_branch , dir_name , file_name .replace ("md" , "html" ))
62+ title = get_title (file_name )
4663 if not title :
4764 continue
4865
@@ -63,8 +80,10 @@ def print_md_toc(toc_info, mapping):
6380 print (" " , f"* [{ post [1 ][0 ]} ]({ post [1 ][1 ]} )" )
6481
6582def main ():
66- mapping = generate_mapping ()
67- toc_info = get_toc_info ()
83+ all_chapter = get_all_chapter ()
84+ mapping = generate_mapping (all_chapter )
85+ current_branch = get_current_brance ()
86+ toc_info = get_toc_info (all_chapter , current_branch )
6887 print_md_toc (toc_info , mapping )
6988
7089if __name__ == '__main__' :
0 commit comments