-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaders.py
41 lines (39 loc) · 1.4 KB
/
headers.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
import argparse
import sys
def parse_args():
parser = argparse.ArgumentParser(description="This program is used to convert "
"headers into a table of contents")
parser.add_argument("-f", "--filename", type=str, default="input.txt", help="Input filename")
return parser
parser = parse_args()
args = parser.parse_args()
list = []
try:
out = open("output.txt", "w")
out.write("<style>\n"
"\ttd {\n"
"\t\tpadding-right: 10px;\n"
"\t}\n"
"</style>\n\n\n\n")
out.write("<table class=\"fixed\" width=\"100%\">\n"
"<tbody>\n"
"<col width=\"4%\">\n")
with open(args.filename) as f:
i = 0
for line in f:
list.append(line.strip().split(' ', 1))
# list[i][1] = ''.join(j for j in list[i][1] if j.isalnum())
out.write("<tr>\n"
"<td style=\"text-align:right\">{}</td>\n"
"<td><a href=\"#{}\">{}</a></td>\n"
"</tr>\n".format(list[i][0], ''.join(j for j in list[i][1] if j.isalnum()), list[i][1]))
print list[i]
i = i + 1
out.write("</tbody>\n"
"</table>\n\n")
for item in list:
# print item
out.write("<h2 id=\"{}\">{} {}</h2>\n".format(''.join(j for j in item[1] if j.isalnum()), item[0], item[1]))
except:
print parser.print_help()
raise