linux_introduction/generate_toc.py

50 lines
1.6 KiB
Python
Raw Normal View History

2022-07-14 11:46:16 +02:00
#!/usr/bin/python3
import subprocess
LINK = "https://github.com/ekalinin/github-markdown-toc"
OUTPUT = "readme.md"
OUTLINE = "outline.md"
INPUT = [
"essential/introduction_to_linux.md",
"essential/introduction_to_the_commandline.md",
"essential/introduction_to_administration.md",
2022-07-14 11:46:16 +02:00
"advanced/learning_bash_scripting.md",
"advanced/learning_shells.md",
]
CMD = "gh-md-toc"
FILTER = "(#"
TITLE = "Table of Contents"
if __name__ == "__main__":
try:
2022-07-14 11:46:16 +02:00
p = subprocess.Popen(CMD.split(), 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:
2022-07-14 11:46:16 +02:00
cmd = CMD.split()
cmd.append(f)
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate()
output = output.decode().split("\n")
for line in output:
if FILTER in line:
line = line.replace(FILTER, "(./{}#".format(f))
if TITLE in line:
title = " ".join(f.replace(".md", "").split("_")).capitalize()
title = title.replace("/", ": ")
line = title
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:
for line in CONTENT:
fp.write("{}\n".format(line))
print("done...")