-
Notifications
You must be signed in to change notification settings - Fork 15
/
combine_snippets.py
50 lines (38 loc) · 1.19 KB
/
combine_snippets.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
import os
def create_snippet(s) -> str:
return "\n".join([
"```",
s,
"```\n"
])
def create_image_link(file):
return f"![]({os.path.relpath(file)})"
def section_template(section_name, snippets) -> str:
return f"""
## {section_name}
{snippets}
"""
def create_section(d) -> str:
snippets = []
files = os.scandir(d.path)
for file in files:
is_image = file.name.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif'))
if is_image:
snippet = create_image_link(file)
snippets.append(snippet)
else:
with open(file, encoding="utf8") as f:
snippet = create_snippet(f.read())
snippets.append(snippet)
combined_snippets = "\n".join(snippets)
return section_template(d.name, combined_snippets)
def main():
sections = [create_section(d)
for d in os.scandir(".")
if d.name != '.git' and d.is_dir()]
with open("SNIPPETS.md", "w", encoding="utf8") as snippets_file:
snippets_file.write("# All Snippets \n")
for s in sections:
snippets_file.write(s)
if __name__ == '__main__':
main()