Skip to content

Instantly share code, notes, and snippets.

@filipsPL
Created September 27, 2017 13:02
Show Gist options
  • Save filipsPL/91eab98b8ba9f7454a49661f1cce71ba to your computer and use it in GitHub Desktop.
Save filipsPL/91eab98b8ba9f7454a49661f1cce71ba to your computer and use it in GitHub Desktop.
Super simple TOC generator from a ready HTML file.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from re import sub
fname = "FAQ.html"
header="<h4>"
outfile = "FAQ+TOC.html"
#-----------------------------------------#
with open(fname) as f:
content = f.readlines()
content = [x.strip() for x in content]
body = ""
toc = "<ul>\n"
for line in content:
if line.startswith(header):
plainText = sub('<[^<]+?>', '', line)
kotwica = sub(' |,|\.|\+|\?|\!|/', '', plainText)
#print kotwica
body += "<a name='%s'></a>%s\n" % (kotwica, line)
toc += " <li><a href='#%s'>%s</a></li>\n" % (kotwica, plainText)
else:
body += line + "\n"
toc += "</ul>\n\n"
#print "---------------------"
#print toc
# replace <body> with <body> + TOC
content = body.replace("<body>", "<body>\n\n" + toc)
f = open( outfile, 'w' )
f.write(content)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment