from jinja2 import Environment, FileSystemLoader
+def cleanup_toc(data):
+ # Clean up the table of contents
+ toc_regexp = re.compile(r'^\s+(.+?)\s+(.+?)(\.+)\s+\d+$', re.MULTILINE)
+ def format_match(match):
+ section_name = match.group(1)
+ title = match.group(2)
+
+ anchor = section_name
+ if anchor[-1] == '.':
+ anchor = anchor[:-1]
+
+ return '<a href="#section-{}">{} {}</a><br>'.format(anchor, section_name, title)
+
+ data = toc_regexp.sub(format_match, data)
+
+ hl_toc_regexp = re.compile(r'^(Table of Contents)', re.IGNORECASE | re.MULTILINE)
+ data = hl_toc_regexp.sub(r'<h2>\1</h2>', data)
+ return data
+
+
def remove_top_space(data):
topspace_regexp = re.compile(r"(\n){6}", re.MULTILINE)
data = topspace_regexp.sub('', data, count=1)
def anchor_titles(data):
# Reverse section order because lvl1_title_rx matches the beginning of lvl2_title_rx
# and lvl3_title_rx.
+ lvl4_title_rx = re.compile(r"^(\d+)\.(\d+)\.(\d+)\.(\d+)\.(.*)$", re.MULTILINE)
+ data = lvl4_title_rx.sub(r'\t<a name="section-\1.\2.\3.\4"><h4>\1.\2.\3.\4 \5</h4></a>', data)
+
lvl3_title_rx = re.compile(r"^(\d+)\.(\d+)\.(\d+)\.(.*)$", re.MULTILINE)
data = lvl3_title_rx.sub(r'\t<a name="section-\1.\2.\3"><h4>\1.\2.\3 \4</h4></a>', data)
out = data
for fn in [remove_top_space, remove_page_breaks,
anchor_titles, create_paragraphs, create_diagram_blocks,
- add_line_breaks_legends]:
+ add_line_breaks_legends, cleanup_toc]:
out = fn(out)
env = Environment(loader=FileSystemLoader('templates'))