From: Karim Hamidou Date: Sun, 6 Mar 2016 05:08:14 +0000 (-0800) Subject: show a nicer toc X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=04f763806345512b1015e2c05372520068f632c2;p=rfcspot.git show a nicer toc --- 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'))