|
9 | 9 | import re |
10 | 10 |
|
11 | 11 | find_link = re.compile('\[(\w+)\]\(([\w\\\/\_\.\:]+)\)') |
| 12 | +find_nums = re.compile('([0-9]+)\.([0-9]*).*') |
12 | 13 |
|
13 | 14 | readme = [] |
14 | 15 | tree = [] |
15 | 16 |
|
16 | | -def get_file_list(folder): |
17 | | - file_list = [] |
18 | | - filenames = os.listdir(folder) |
19 | | - for filename in filenames: |
20 | | - file_list.append(filename) |
21 | | - return file_list |
22 | | - |
23 | | -# 각 쳅터 및 파트를 숫자로 저장하지 않는 이유는 float 형태로 저장하면 3.20 => 3.2로 인식하기 때문 |
24 | | -def get_chapter_list(): |
25 | | - folder = './Google Python Style Guide' |
26 | | - file_list = get_file_list(folder) |
27 | | - chapter_list = {} |
28 | | - for filename in file_list: |
29 | | - number = filename.split('. ')[0] |
30 | | - chapter_list.update({number:os.path.join(folder, filename)}) |
31 | | - return chapter_list |
32 | | - |
33 | | -def get_part_list(chapter_path): |
34 | | - file_list = get_file_list(chapter_path) |
35 | | - part_list = {} |
36 | | - for filename in file_list: |
37 | | - if filename.lower() == "readme.md": |
38 | | - continue |
39 | | - number = filename.split(" ")[0] |
40 | | - part_list.update({number: os.path.join(chapter_path, filename)}) |
41 | | - return part_list |
42 | | - |
43 | | -def chapter_foreach(chapter_list, f=print): |
44 | | - start = 1 |
45 | | - end = start + len(chapter_list) |
46 | | - for i in range(start, end): |
47 | | - idx = str(i) |
48 | | - f(chapter_list[idx], idx, chapter_list) |
49 | | - |
50 | | -def part_foreach(part_list, number, f=print): |
51 | | - start = 1 |
52 | | - end = start + len(part_list) |
53 | | - for i in range(start, end): |
54 | | - idx = str(number) + "." + str(i) |
55 | | - f(part_list[idx], str(i), part_list) |
| 17 | +def get_files(folder): |
| 18 | + file_list = [] |
| 19 | + filenames = os.listdir(folder) |
| 20 | + for filename in filenames: |
| 21 | + file_list.append(filename) |
| 22 | + return file_list |
| 23 | + |
| 24 | +def get_numbers(files): |
| 25 | + numbers = list(files.keys()) |
| 26 | + numbers.sort() |
| 27 | + return numbers |
| 28 | + |
| 29 | +def get_file_list(path): |
| 30 | + files = get_files(path) |
| 31 | + file_list = {} |
| 32 | + for filename in files: |
| 33 | + nums = find_nums.search(filename) |
| 34 | + if not nums: |
| 35 | + continue |
| 36 | + num = int(nums.group(2) or nums.group(1)) |
| 37 | + file_list.update({num: os.path.join(path, filename)}) |
| 38 | + return file_list |
| 39 | + |
| 40 | +def file_list_foreach(files, f=print): |
| 41 | + for number in get_numbers(files): |
| 42 | + f(files[number], number, files) |
56 | 43 |
|
57 | 44 | def write_readme(readme_path, data, override=False): |
58 | | - write_type = 'w' if override is True else 'a' |
59 | | - f = open(readme_path, write_type) |
60 | | - f.write("".join(data)) |
61 | | - f.close() |
| 45 | + write_type = 'w' if override is True else 'a' |
| 46 | + f = open(readme_path, write_type) |
| 47 | + f.write("".join(data)) |
| 48 | + f.close() |
62 | 49 |
|
63 | 50 | def insert_part_in_readme(part_path, number, all): |
64 | | - part_tree = [] |
65 | | - target_readme = part_path.split(part_path.split('/')[-1])[0] + "ReadMe.md" |
66 | | - f = open(part_path, 'r') |
67 | | - lines = f.readlines() |
68 | | - f.close() |
69 | | - for i in range(0, len(lines)): |
70 | | - syntax = lines[i].split(" ")[0] |
71 | | - if syntax in ["##", "###", "####"]: |
72 | | - link = lines[i-2].replace('<a id="', "").replace('"></a>', "").replace("\n", "") |
73 | | - title = lines[i].replace("# ", "").replace("#", "").replace("\n", "") |
74 | | - link_in_title = find_link.search(title) |
75 | | - if link_in_title: |
76 | | - title = title.replace(link_in_title.group(), find_link.findall(title)[0][0]) |
77 | | - part_tree.append({"link":link, "title":title}) |
78 | | - if part_tree: |
79 | | - tree.append(part_tree) |
80 | | - # 파트가 시작하는 부분에 라인 추가 (d5e507c / #12) |
81 | | - lines.append("\n---\n") |
82 | | - write_readme(target_readme, lines, True if number == "1" else False) |
| 51 | + part_tree = [] |
| 52 | + target_readme = part_path.split(part_path.split('/')[-1])[0] + "ReadMe.md" |
| 53 | + f = open(part_path, 'r') |
| 54 | + lines = f.readlines() |
| 55 | + f.close() |
| 56 | + for i in range(0, len(lines)): |
| 57 | + syntax = lines[i].split(" ")[0] |
| 58 | + if syntax not in ["##", "###", "####"]: |
| 59 | + continue |
| 60 | + link = lines[i-2].replace('<a id="', "").replace('"></a>', "").replace("\n", "") |
| 61 | + title = lines[i].replace("# ", "").replace("#", "").replace("\n", "") |
| 62 | + link_in_title = find_link.search(title) |
| 63 | + if link_in_title: |
| 64 | + title = title.replace(link_in_title.group(), find_link.findall(title)[0][0]) |
| 65 | + part_tree.append({"link":link, "title":title}) |
| 66 | + if part_tree: |
| 67 | + tree.append(part_tree) |
| 68 | + # 파트가 시작하는 부분에 라인 추가 (d5e507c / #12) |
| 69 | + lines.append("\n---\n") |
| 70 | + write_readme(target_readme, lines, True if number == 1 else False) |
83 | 71 |
|
84 | 72 | def tree_to_contents(): |
85 | | - contents = [] |
86 | | - for item in tree: |
87 | | - if type(item) == dict: |
88 | | - contents.append("- ["+item["title"]+"](#"+item["link"]+")") |
89 | | - continue |
90 | | - contents.append(" * ["+item[0]["title"]+"](#"+item[0]["link"]+")") |
91 | | - for part in item[1:]: |
92 | | - contents.append(" + ["+part["title"]+"](#"+part["link"]+")") |
93 | | - |
94 | | - return "\n".join(["\n<details>", " <summary>Table of Contents</summary>\n\n"]) + "\n".join(contents)+ "\n\n</details>\n" |
| 73 | + contents = [] |
| 74 | + for item in tree: |
| 75 | + if type(item) == dict: |
| 76 | + contents.append("- ["+item["title"]+"](#"+item["link"]+")") |
| 77 | + continue |
| 78 | + contents.append(" * ["+item[0]["title"]+"](#"+item[0]["link"]+")") |
| 79 | + for part in item[1:]: |
| 80 | + contents.append(" + ["+part["title"]+"](#"+part["link"]+")") |
| 81 | + |
| 82 | + return "\n".join(["\n<details>", " <summary>Table of Contents</summary>\n\n"]) + "\n".join(contents)+ "\n\n</details>\n" |
95 | 83 |
|
96 | 84 | def chapter_build(chapter_path, number, all): |
97 | | - chapter_title = chapter_path.split("/")[-1] |
98 | | - chapter_link = "s"+chapter_path.split("/")[-1].split(". ")[0] |
99 | | - tree.append({"title":chapter_title, "link": chapter_link}) |
100 | | - chapter_header = [ |
101 | | - '\n<a id="'+chapter_link+'"></a>\n', |
102 | | - "\n", |
103 | | - "## "+chapter_title +"\n\n", |
104 | | - ] |
105 | | - part_foreach(get_part_list(chapter_path), number, insert_part_in_readme) |
106 | | - from_readme = chapter_path + "/ReadMe.md" |
107 | | - f = open(from_readme, 'r') |
108 | | - data = f.readlines() |
109 | | - f.close() |
110 | | - # 챕터가 시작하는 부분에 <br> 추가 (d5e507c / #12) |
111 | | - readme.append("\n<br>\n" + "".join(chapter_header) + "".join(data)) |
| 85 | + chapter_title = chapter_path.split("/")[-1] |
| 86 | + chapter_link = "s"+chapter_path.split("/")[-1].split(". ")[0] |
| 87 | + tree.append({"title":chapter_title, "link": chapter_link}) |
| 88 | + chapter_header = [ |
| 89 | + '\n<a id="'+chapter_link+'"></a>\n', |
| 90 | + "\n", |
| 91 | + "## "+chapter_title +"\n\n", |
| 92 | + ] |
| 93 | + file_list_foreach(get_file_list(chapter_path), insert_part_in_readme) |
| 94 | + from_readme = chapter_path + "/ReadMe.md" |
| 95 | + f = open(from_readme, 'r') |
| 96 | + data = f.readlines() |
| 97 | + f.close() |
| 98 | + # 챕터가 시작하는 부분에 <br> 추가 (d5e507c / #12) |
| 99 | + readme.append("\n<br>\n" + "".join(chapter_header) + "".join(data)) |
112 | 100 |
|
113 | 101 | def build(): |
114 | | - to_readme = "./Google Python Style Guide kor.md" |
| 102 | + to_readme = "./Google Python Style Guide kor.md" |
115 | 103 |
|
116 | | - chapter_list = get_chapter_list() |
117 | | - chapter_foreach(chapter_list, chapter_build) |
| 104 | + chapter_list = get_file_list('./Google Python Style Guide') |
| 105 | + file_list_foreach(chapter_list, chapter_build) |
118 | 106 |
|
119 | | - title = "# Google Python Style Guide\n" |
120 | | - Contents = tree_to_contents() |
| 107 | + title = "# Google Python Style Guide\n" |
| 108 | + Contents = tree_to_contents() |
121 | 109 |
|
122 | | - write_readme(to_readme, title + Contents + "".join(readme), True) |
| 110 | + write_readme(to_readme, title + Contents + "".join(readme), True) |
123 | 111 |
|
124 | 112 | build() |
0 commit comments