python_introduction/generate_toc.py

35 lines
1.0 KiB
Python

import subprocess
LINK = "https://github.com/ekalinin/github-markdown-toc"
OUTPUT = "readme.md"
INPUT = [
"learning_python3.md",
"learning_python3_gui.md",
"learning_git.md",
]
CMD = "gh-md-toc"
FILTER = "(#"
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:
if FILTER in line:
line = line.replace(FILTER, "(./{}#".format(f))
CONTENT.append(line)
with open(OUTPUT, "w") as fp:
for line in CONTENT:
fp.write("{}\n".format(line))
print("done...")