-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction-script.py
More file actions
executable file
·88 lines (76 loc) · 2.57 KB
/
action-script.py
File metadata and controls
executable file
·88 lines (76 loc) · 2.57 KB
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
#!/usr/bin/env python3
import os
import fnmatch
from pathlib import Path
# prefix components:
space = ' '
branch = '│ '
# pointers:
tee = '├── '
last = '└── '
def tree(dir_path: Path, prefix: str=''):
"""A recursive generator, given a directory Path object
will yield a visual tree structure line by line
with each line prefixed by the same characters
"""
contents = list(filter(lambda path: not "commitments" in str(path) and not ".git" in str(path) and not "README" in str(path) and not ".py" in str(path), dir_path.iterdir()))
# contents each get pointers that are ├── with a final └── :
pointers = [tee] * (len(contents) - 1) + [last]
for pointer, path in zip(pointers, contents):
yield prefix + pointer + path.name.rstrip(".pdf")
if path.is_dir(): # extend the prefix and recurse:
extension = branch if pointer == tee else space
# i.e. space because last, └── , above so no more |
yield from tree(path, prefix=prefix+extension)
def drawDirectoryStructure():
s = ""
for line in tree(Path(".")):
s += line + '\n'
return s
def frontmatter():
matches = []
for root,dirnames,filenames in os.walk('.'):
for filename in fnmatch.filter(filenames,'*.md'):
if filename == 'README.md': continue
matches.append([os.path.join(root,filename),filename])
for filedir,filename in matches:
with open(filedir,'r') as f:
cont = f.read()
if '---\n' not in cont:
with open(filedir,'w') as fp:
fp.write(f"""
---
title: {filename[:-3]}
---""" + cont)
def index():
dirs = []
for root,dirnames,filenames in os.walk('.'):
if '.git' in root or './commitments' in root or root == '.':
continue;
dirs.append((root,[f for f in filenames if f.split('.')[-1] == 'md']))
for folder,filenames in dirs:
if os.path.exists(os.path.join(folder,'_index.md')):
continue;
with open(os.path.join(folder,'_index.md'),'w') as fp:
fp.write(f"""
---
title: {os.path.basename(folder)}
---
""")
return
def main():
frontmatter()
index()
struct = drawDirectoryStructure()
print(struct)
with open('README.md','w') as fp:
fp.write(f"""# EL Notes
If you want, please compile all statistics and data you find in this repository. For EL4131. Additional articles are also appreciated.
## Directory Structure
```
{struct}
```
""")
with open('tree.txt','w') as fp:
fp.write(struct)
main()