forked from leachim6/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_list.py
executable file
·34 lines (24 loc) · 1.12 KB
/
update_list.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
#!/usr/bin/env python3
import os
import posixpath
from urllib.parse import quote
import re
def regexReplace(string, search, replacement):
return re.compile(search).sub(replacement, string)
languageCount = 0
languagesText = ""
# List the available languages
for directory in sorted(os.listdir('.')):
if not (directory == '.' or directory == '..' or directory[0] == '.' or os.path.isfile(directory)):
for filename in sorted(os.listdir(directory), key=lambda s: s.lower()):
if os.path.isfile(os.path.join(directory, filename)):
language = os.path.splitext(filename)[0].replace(
'-', ' ').replace('_', ' ').title()
languagesText += f'* [{language}]({posixpath.join(quote(directory), quote(filename))})\n'
languageCount += 1
result = f"""<!--Languages start-->
## Languages ({languageCount} total)
{languagesText}<!--Languages end-->"""
readmeContents = open('readme.md', 'r', encoding="utf-8").read()
open('readme.md', 'w', encoding="utf-8").write(regexReplace(
readmeContents, r"<!--Languages start-->(.|\n)*<!--Languages end-->", result))