]> khamidou — GitHub mirrors - rfcspot.git/commitdiff
show a nicer toc
authorKarim Hamidou <karim@MacBook.local>
Sun, 6 Mar 2016 05:08:14 +0000 (21:08 -0800)
committerKarim Hamidou <karim@MacBook.local>
Sun, 6 Mar 2016 05:08:14 +0000 (21:08 -0800)
src/render_rfc.py

index 7a4c51984670e1255e5434ef1789629f694f8eda..cb7fb1ebe189b85e11437735c39e93cd6db67a3c 100644 (file)
@@ -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 '<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)
@@ -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<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)
 
@@ -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'))