prepare for table of contents

This commit is contained in:
tildearrow 2023-09-11 19:07:31 -05:00
parent e9bcb67930
commit bd8c469d91

View file

@ -73,6 +73,9 @@ if __name__ == "__main__":
for file_ in filter(lambda x: x.lower().endswith('.md'), files): for file_ in filter(lambda x: x.lower().endswith('.md'), files):
file_list.append(os.path.join(base_dir, file_)) file_list.append(os.path.join(base_dir, file_))
#-- then, create the index --#
index = '<h2>contents</h2><ol>'
#-- then, create the document --# #-- then, create the document --#
html = '' html = ''
@ -83,6 +86,11 @@ if __name__ == "__main__":
with open(my_file, 'r') as md: with open(my_file, 'r') as md:
LOGGER.info("processing file %s" % my_file) LOGGER.info("processing file %s" % my_file)
data = md.read() data = md.read()
# retrieve title
pageTitle = data.partition('\n')[0].replace("# ","")
pageLink = my_file.replace(os.path.sep, "__"),
index += '<li><a href="#%s">%s</a></li>' % ( pageLink, pageTitle )
# perform link fixing # perform link fixing
data = re.sub(r'\[(.+?)\]\((.+?)\)', fix_links, data) data = re.sub(r'\[(.+?)\]\((.+?)\)', fix_links, data)
@ -94,6 +102,9 @@ if __name__ == "__main__":
markdown.markdown(data, extensions=['nl2br', 'mdx_breakless_lists', GithubFlavoredMarkdownExtension()]) markdown.markdown(data, extensions=['nl2br', 'mdx_breakless_lists', GithubFlavoredMarkdownExtension()])
) )
# finish index
index += '</ol>'
# build html # build html
final_html = (''' final_html = ('''
<!DOCTYPE html> <!DOCTYPE html>
@ -282,11 +293,16 @@ if __name__ == "__main__":
<i>for version 0.6</i> <i>for version 0.6</i>
</div> </div>
</section> </section>
<section id="blank">
</section>
<section id="index">
%s
</section>
%s %s
</body> </body>
</html> </html>
''' % ( ''' % (
html index, html
) )
) )