From 04f763806345512b1015e2c05372520068f632c2 Mon Sep 17 00:00:00 2001 From: Karim Hamidou Date: Sat, 5 Mar 2016 21:08:14 -0800 Subject: [PATCH] show a nicer toc --- src/render_rfc.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/render_rfc.py b/src/render_rfc.py index 7a4c5198..cb7fb1eb 100644 --- a/src/render_rfc.py +++ b/src/render_rfc.py @@ -8,6 +8,26 @@ from bs4 import BeautifulSoup, Tag, Comment 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 '{} {}
'.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'

\1

', data) + return data + + def remove_top_space(data): topspace_regexp = re.compile(r"(\n){6}", re.MULTILINE) data = topspace_regexp.sub('', data, count=1) @@ -52,6 +72,9 @@ def line_breaks_indented_blocks(data): 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

\1.\2.\3.\4 \5

', data) + lvl3_title_rx = re.compile(r"^(\d+)\.(\d+)\.(\d+)\.(.*)$", re.MULTILINE) data = lvl3_title_rx.sub(r'\t

\1.\2.\3 \4

', data) @@ -71,7 +94,7 @@ def render_rfc(filename): 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')) -- 2.43.0