-
Notifications
You must be signed in to change notification settings - Fork 10
/
topic.py
38 lines (29 loc) · 1.06 KB
/
topic.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
#!/usr/bin/env python3
# (c) Xavier Combelle here made in public domain
# NOTE: we now use gen-toc-based-on-docbook5.py instead, but this program
# may have other use-cases.
import argparse
import subprocess
import sys
# * https://github.com/jch/html-pipeline/
# blob/master/lib/html/pipeline/toc_filter.rb
def add_toc(fn):
out_s = subprocess.Popen(
["bash", "-c", "./github-markdown-toc/gh-md-toc - < " + fn],
stdout=subprocess.PIPE).stdout.read().decode('utf-8')
with open(fn, "rt") as fh:
out_s += fh.read()
return out_s
def main(argv):
parser = argparse.ArgumentParser(
prog='PROG',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--output', type=str, required=True,
help='output filename')
parser.add_argument('--input', type=str, required=True,
help='Input filename')
args = parser.parse_args(argv[1:])
output_text = add_toc(args.input)
with open(args.output, "wt") as ofh:
ofh.write(output_text)
main(sys.argv)