-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_toc.py
42 lines (35 loc) · 1.42 KB
/
generate_toc.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
#!/usr/bin/env python3
"""
Generate table-of-content (toc) for each of the markdown notes in a directory
This requires Python 3.6+
npm with markdown-toc
$ npm install -g markdown-toc
"""
import os
import subprocess
import copy
if __name__ == "__main__":
topdir = '.'
# The extension to search for
exten = '.md'
lines = "# Table of content\n<!-- toc -->\n<!-- tocstop -->\n\n"
toc_start = "<!-- toc -->"
for dirpath, dirnames, files in os.walk(topdir):
for ix, name in enumerate(files):
if name.endswith(exten):
file_path = f"{dirpath}/{name}"
print(f"!!!! ---Processing " +
f"{file_path} {ix + 1} out of {len(files)}--- !!!")
with open(file_path) as f:
file_content = "".join(f.readlines())
if toc_start not in file_content:
print(f"!!!! ---TOC mark {toc_start} not present -!!!")
file_content = lines + file_content
with open(file_path, "w") as f:
print('!!!! ---Adding TOC mark to file content -!!!')
# print(file_content)
f.writelines(file_content)
print(f"!!!! --- Generating TOC for {file_path} - !!!!")
subprocess.call(
[f"/usr/local/bin/markdown-toc -i {file_path}"],
shell=True)