]> khamidou — GitHub mirrors - rfcspot.git/commitdiff
anchor titles
authorKarim Hamidou <karim@MacBook.local>
Fri, 4 Mar 2016 05:59:09 +0000 (21:59 -0800)
committerKarim Hamidou <karim@MacBook.local>
Fri, 4 Mar 2016 05:59:09 +0000 (21:59 -0800)
src/render_rfc.py

index a763dfa331727c9f79c20074af8a205da8d479a3..0b06fb365dd8794647a81f2dd15f41829ea7366e 100644 (file)
@@ -4,26 +4,48 @@
 # 2. add links to the table of contents
 import re
 import sys
+from jinja2 import Environment, FileSystemLoader
 
 
 def remove_top_space(data):
-    topspace_regexp = re.compile(r"^(\n){4,}", re.MULTILINE)
+    topspace_regexp = re.compile(r"(\n){6}", re.MULTILINE)
     data = topspace_regexp.sub('', data, count=1)
     return data
 
+
 def remove_page_breaks(data):
     pagebreak_regexp = re.compile(r"((\n){2,}^.*$\n\f$\n^.*$(\n){3,})", re.MULTILINE)
     data = pagebreak_regexp.sub(r'\n', data)
     return data
 
 
+def anchor_titles(data):
+    lvl1_title_rx = re.compile(r"^(\d+\.)(.*)$", re.MULTILINE)
+    data = lvl1_title_rx.sub(r'\t<a name="\1"><h2>\1 \2</h2></a>', data)
+
+    lvl2_title_rx = re.compile(r"^(\d+\.\d+\.)(.*)$", re.MULTILINE)
+    data = lvl2_title_rx.sub(r'\t<a name="\1"><h3>\1 \2</h3></a>', data)
+
+    lvl3_title_rx = re.compile(r"^(\d+\.\d+\.\d+\.)(.*)$", re.MULTILINE)
+    data = lvl3_title_rx.sub(r'\t<a name="\1"><h4>\1 \2</h4></a>', data)
+
+    return data
+
+
 def render_rfc(filename):
     with open(filename) as fd:
         data = fd.read()
 
-    for fn in [remove_top_space, remove_page_breaks]:
-        out = fn(data)
+    out = data
+    for fn in [remove_top_space, remove_page_breaks, anchor_titles]:
+        out = fn(out)
+
+    env = Environment(loader=FileSystemLoader('templates'))
+    template = env.get_template('rfc.html')
 
+    dct = dict(rfc=out)
+    rendered = template.render(**dct)
+    # return rendered
     return out