python_introduction/generate_toc.py

48 lines
1.5 KiB
Python
Raw Normal View History

2022-05-02 22:57:37 +02:00
import subprocess
LINK = "https://github.com/ekalinin/github-markdown-toc"
OUTPUT = "readme.md"
OUTLINE = "outline.md"
2022-05-02 22:57:37 +02:00
INPUT = [
"learning_python3.md",
2022-05-23 19:22:45 +02:00
"introduction_to_solid.md",
2022-05-02 22:57:37 +02:00
"learning_python3_gui.md",
"learning_git.md",
2022-05-05 23:09:49 +02:00
"what_is_next.md",
2022-05-02 22:57:37 +02:00
]
CMD = "gh-md-toc"
FILTER = "(#"
TITLE = "Table of Contents"
2022-05-02 22:57:37 +02:00
if __name__ == "__main__":
try:
p = subprocess.Popen([CMD], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate()
except Exception as e:
print("please install {}".format(LINK))
exit()
CONTENT = []
for f in INPUT:
p = subprocess.Popen([CMD, f], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate()
output = output.decode().split("\n")
for line in output:
2022-05-23 19:22:45 +02:00
if CMD in line:
continue
2022-05-02 22:57:37 +02:00
if FILTER in line:
line = line.replace(FILTER, "(./{}#".format(f))
if TITLE in line:
title = " ".join(f.replace(".md", "").split("_")).capitalize()
2022-05-23 19:22:45 +02:00
title = title.replace("/", ": ")
line = title
2022-05-02 22:57:37 +02:00
CONTENT.append(line)
p = subprocess.Popen(["cp", OUTLINE, OUTPUT], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
print("writing")
with open(OUTPUT, "a") as fp:
2022-05-02 22:57:37 +02:00
for line in CONTENT:
fp.write("{}\n".format(line))
print("done...")