#!/usr/bin/python3
import re
# get list of chapters
chapters = []
for line in open('index.html', encoding="utf-8"):
if not line.count('
', 1)[0])
sections = {}
for filename in chapters:
chapter_id = filename.split(".", 1)[0]
with open(filename, encoding="utf-8") as f: data = f.read()
sections[chapter_id] = re.findall("(.*?)')[1].split('')[0]
full_toc = ['', '']
chapter_id = ''
for line in short_toc.splitlines():
if not line.count('- ', '>', line)
line = line.replace('
- ', '
- '.format(chapter_id))
full_toc.append(line)
section_number = 0
previous_section_level = section_level = 1
for section_level, section_id, section_title in sections[chapter_id]:
section_level = int(section_level)
if section_level < previous_section_level:
full_toc.append('
')
elif section_level > previous_section_level:
full_toc.append('')
section_number += 1
full_toc.append('- {2}'.format(chapter_id, section_id, section_title))
previous_section_level = section_level
section_level = int(section_level)
while (section_level > 1):
full_toc.append('
')
section_level -= 1
full_toc.append('')
full_toc.append('')
with open('table-of-contents.html', encoding="utf-8") as f: data = f.read()
with open('table-of-contents.html', mode="w", encoding="utf-8") as f:
f.write(data.split('')[0] + "\n".join(full_toc) + data.split('')[1])