forked from Bhupesh-V/30-seconds-of-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_readme.py
executable file
·183 lines (147 loc) · 7.28 KB
/
update_readme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
''' Simple script to auto-generate the README.md file for a 30 Seconds of C++ project.
Apply as a git hook by running the following command in linux:
cd .git/hooks/ && ln -s ../../createtil.py pre-commit && cd -
'''
from __future__ import print_function
import os
EMOJIS = {
"algorithm": ':sparkles:',
"stack": ':books:',
"queue": ':large_blue_circle:',
"map": ':world_map: ',
"string": ':red_circle:',
"list": ':page_with_curl:',
"vector": ':arrow_upper_right:',
"set": ":zap:",
"unordered_map": ':world_map:',
"implemented":':heavy_check_mark:',
"not_implemented":':x:'
}
HEADER = '''# 30 Seconds of C++
>### 30 Seconds Of Standard Template Library in C++
<p align="center">
<a href="https://github.com/Bhupesh-V/30-Seconds-of-cpp">
<img src="https://github.com/Bhupesh-V/30-Seconds-of-cpp/blob/master/logo/new_logo_2.jpg" height=auto weight=100%>
</a>
<br>
<hr>
<strong>A collection of C++ STL features (functions/libraries) which can be learned in 30 seconds or less</strong>
</p>
<hr>
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/72e93df05bce4d7598f222676bfb511c)](https://app.codacy.com/app/Bhupesh-V/30-Seconds-Of-STL?utm_source=github.com&utm_medium=referral&utm_content=Bhupesh-V/30-Seconds-Of-STL&utm_campaign=Badge_Grade_Dashboard)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
[![LICENSE](https://img.shields.io/github/license/Bhupesh-V/30-seconds-of-cpp?color=blue)](https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/LICENSE)
[![Build Status](https://travis-ci.org/Bhupesh-V/30-seconds-of-cpp.svg?branch=master)](https://travis-ci.org/Bhupesh-V/30-seconds-of-cpp)
[![CodeFactor](https://www.codefactor.io/repository/github/bhupesh-v/30-seconds-of-cpp/badge)](https://www.codefactor.io/repository/github/bhupesh-v/30-seconds-of-cpp)
[![Open Source Helpers](https://www.codetriage.com/bhupesh-v/30-seconds-of-cpp/badges/users.svg)](https://www.codetriage.com/bhupesh-v/30-seconds-of-cpp)
[![GitHub contributors](https://img.shields.io/github/contributors/Bhupesh-V/30-seconds-of-cpp)](https://github.com/Bhupesh-V/30-seconds-of-cpp/graphs/contributors)
<hr>
'''
FOOTER = '''## :speaking_head: Community
Join other C++ enthusiasts on
- [Slack](https://join.slack.com/t/30-seconds-of-cpp/shared_invite/enQtNTU0MTYxMDk0MzQxLTlhY2Q1MWZmOTM0ODRmOWMwYTllYzVjMzM2ZDk5NjY3MDU2NDljNDlmMDQ5MTMyOGQyOTVlNzEwOTc0MmYwOWY)
- [Discord](https://discord.gg/KAD3JZq)
## :globe_with_meridians: 30 Seconds of Knowledge
> Experience *30 Seconds of C++* as a [Web Extension](https://chrome.google.com/webstore/detail/30-seconds-of-knowledge/mmgplondnjekobonklacmemikcnhklla?hl=en) built by [Stefan Petrovic](https://github.com/petrovicstefanrs)
- A big thanks to [Shriam Kaushik](https://github.com/shriamkaushik) for the lovely new logo :heart:
## Author
:bust_in_silhouette: **Bhupesh Varshney**
- Twitter: [@bhupeshimself](https://twitter.com/bhupeshimself)
- DEV: [bhupesh](https://dev.to/bhupesh)
## :memo: License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## :wave: Contributing
Please read the *updated* [CONTRIBUTING](CONTRIBUTING.md) for the process for submitting pull requests to us.
'''
def get_list_of_categories():
''' Walk the current directory and get a list of all subdirectories at that
level. These are the "header directories" in which there are functions.'''
dirs = [x for x in os.listdir('.') if os.path.isdir(x) and
'.git' not in x and x != 'snippets' and x != 'logo' ]
return dirs
def get_title(function_file):
''' Read the file until we hit the first line that starts with a #
indicating a title in markdown. We'll use that as the title for this
entry. '''
with open(function_file) as _file:
for line in _file:
line = line.strip()
if line.startswith('#'):
return line[1:].lstrip() # text after # and whitespace
def get_functions(category):
''' For a given category, get the list of function() titles. '''
files = [x for x in os.listdir(category)]
titles = []
for filename in files:
fullname = os.path.join(category, filename)
if (os.path.isfile(fullname)) and fullname.endswith('.md') and fullname != category + '/README.md':
title = get_title(fullname)
titles.append((title, fullname))
return titles
def get_category_dict(category_names):
categories = {}
count = 0
for category in category_names:
titles = get_functions(category)
categories[category] = titles
count += len(titles)
return count, categories
def print_file(category_names, count, categories):
''' Now we have all the information, print it out in markdown format. '''
with open('README.md', 'w') as file_:
file_.write(HEADER)
file_.write('\n')
file_.write('''
### Header Files
''')
# print the list of categories with links
for category in sorted(category_names):
file_.write('* [{0}](#{1}) '.format(category,
category))
file_.write(EMOJIS[category] + '\n')
# print the section for each category
file_.write('''
---
''')
for category in sorted(category_names):
file_.write('### {0} '.format(category.capitalize()))
file_.write('\n')
file_.write('<details><summary>View contents</summary>\n<ol>\n')
tils = categories[category]
for (title, filename) in sorted(tils):
file_.write('<li><a href="{0}"><code>{1}</code></a></li>\n'.format(filename, title))
file_.write('\n</ol>\n</details>\n\n')
file_.write(FOOTER)
def print_subfile(category):
''' Print out the correct information to the README inside of a category'''
not_implemented_functions = []
implemented_functions = map(lambda entry: entry[0], get_functions(category))
with open(category + '/todo.txt', 'r') as file_:
lines = file_.readlines()
with open(category + '/todo.txt', 'w') as file_:
for function in lines:
if function.strip('\n') not in implemented_functions:
not_implemented_functions.append(function.strip('\n'))
file_.write(function)
with open(category + '/README.md', 'w') as file_:
file_.write('# `<{0}>`\n'.format(category))
for function in sorted(implemented_functions):
file_.write('{0} [{1}]({2}) \n'.format(EMOJIS["implemented"],function, function+'.md'))
for function in sorted(not_implemented_functions):
file_.write('{0} {1} \n'.format(EMOJIS["not_implemented"],function))
def create_readme():
''' Create a 30C++ README.md file with a nice index for using it directly
from github. '''
category_names = get_list_of_categories()
count, categories = get_category_dict(category_names)
print_file(category_names, count, categories)
def create_subreadmes():
''' Create all 30C++ README.md files for all subcategories '''
category_names = get_list_of_categories()
for category in category_names:
print_subfile(category)
if __name__ == '__main__':
create_readme()
create_subreadmes()
os.system('git add README.md')