]> khamidou — GitHub mirrors - rfcspot.git/commitdiff
generate a metadata file
authorKarim Hamidou <karim@MacBook.local>
Thu, 10 Mar 2016 04:09:12 +0000 (20:09 -0800)
committerKarim Hamidou <karim@MacBook.local>
Thu, 10 Mar 2016 04:09:12 +0000 (20:09 -0800)
src/render_rfc.py

index 93ff5f3290111231653671ad02b70a1e80ea06e2..d5e67f91b0bc054206bfa2d48a9c8a95f7e2d7ad 100644 (file)
@@ -4,11 +4,21 @@
 # 2. add links to the table of contents
 import re
 import sys
-from bs4 import BeautifulSoup, Tag, Comment
+import json
 from jinja2 import Environment, FileSystemLoader
 
+# A dictionary containing metadata about the RFC. We
+# fill it out as we go through the file.
+metadata = dict()
+
+
+def cleanup_author_header(data, **kwargs):
+    rfc_number_regexp = re.compile(r'^Request for Comments:\s+(\d+)', re.MULTILINE)
+    rfc_number = rfc_number_regexp.match(data)
+
+    if rfc_number is not None:
+        metadata['rfc'] = int(rfc_number.group(1))
 
-def cleanup_author_header(data):
     header_regexp = re.compile('(^Network Working Group.+?\n\n)(.+)\n\n(Status of this Memo)', re.MULTILINE | re.DOTALL)
 
     def format_match(match):
@@ -16,12 +26,15 @@ def cleanup_author_header(data):
         title = match.group(2)
         status = match.group(3)
 
+        metadata['title'] = title.strip()
+
         return """<p>{}</p>
                   <h1>{}</h1>
                   <h2>{}</h2>""".format(header, title, status)
 
     data = header_regexp.sub(format_match, data)
 
+
     # Clean up abstract and copyright notice
     copyright_regex = re.compile('(^Copyright Notice)', re.MULTILINE)
     data = copyright_regex.sub(r'<h2>\1</h2>', data)
@@ -32,10 +45,11 @@ def cleanup_author_header(data):
     return data
 
 
-def cleanup_toc(data):
+def cleanup_toc(data, **kwargs):
     # Clean up the table of contents
     # toc_regexp = re.compile(r'^\s+(.+?)\s*(.+?)(\.+)\s+\d+$', re.MULTILINE)
     toc_regexp = re.compile(r'^\s+([\d\.]+)\s+(.+)(\.+)\s*(\d+)$', re.MULTILINE)
+
     def format_match(match):
         section_name = match.group(1)
         title = match.group(2)
@@ -51,8 +65,7 @@ def cleanup_toc(data):
         elif anchor.count('.') == 2:
             indent = 'indent-2'
 
-        return '<a href="#section-{}" class="{}">{} {}</a><br>'.format(anchor, indent,
-                                                                       section_name, title)
+        return """<a href="#section-{}" class="{}">{} {}</a><br>""".format(anchor, indent, section_name, title)
 
     data = toc_regexp.sub(format_match, data)
 
@@ -61,19 +74,19 @@ def cleanup_toc(data):
     return data
 
 
-def remove_top_space(data):
+def remove_top_space(data, **kwargs):
     topspace_regexp = re.compile(r"(\n){6}", re.MULTILINE)
     data = topspace_regexp.sub('', data, count=1)
     return data
 
 
-def remove_page_breaks(data):
+def remove_page_breaks(data, **kwargs):
     pagebreak_regexp = re.compile(r"((\n){2,}^.*$\n\f$\n^.*$(\n){3,})", re.MULTILINE)
     data = pagebreak_regexp.sub(r'\n', data)
     return data
 
 
-def create_paragraphs(data):
+def create_paragraphs(data, **kwargs):
     paragraph_regexp = re.compile(r"(^.+?(\.|\:|;))\n\n", re.MULTILINE | re.DOTALL)
 
     def format_match(match):
@@ -88,7 +101,7 @@ def create_paragraphs(data):
     return data
 
 
-def create_diagram_blocks(data):
+def create_diagram_blocks(data, **kwargs):
     # A boxed diagram usually starts by +-- and ends by ---+.
     diagram_regexp = re.compile(r"(\s*\+(-){2,}.+(-){2,}\+)", re.MULTILINE | re.DOTALL)
     data = diagram_regexp.sub(r'<pre>\1\n</pre>\n', data)
@@ -99,7 +112,7 @@ def create_diagram_blocks(data):
     return data
 
 
-def add_line_breaks_legends(data):
+def add_line_breaks_legends(data, **kwargs):
     # RFC diagrams also have legends, of the form:
     # (1) ...comment...\n
     # This function replace \n by <br> to make the code more readable.
@@ -107,17 +120,24 @@ def add_line_breaks_legends(data):
     data = legends_linebreak.sub(r'\1<br>\n', data)
     return data
 
-def line_breaks_indented_blocks(data):
+def line_breaks_indented_blocks(data, **kwargs):
     pass
 
-def anchor_titles(data):
+def anchor_titles(data, **kwargs):
+    filename = kwargs['filename']
+
     # 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)
+    lvl4_template = r"""<a name="section-\1.\2.\3.\4"><h4>\1.\2.\3.\4 \5</h4></a>
+                        <a href="raw/{}" class="rawLink">raw</a>
+                    """.format(filename)
+
+    data = lvl4_title_rx.sub(lvl4_template, 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)
+    lvl3_template = r"""\t<a name="section-\1.\2.\3"><h4>\1.\2.\3 \4</h4></a>"""
+    data = lvl3_title_rx.sub(lvl3_template, data)
 
     lvl2_title_rx = re.compile(r"^(\d+)\.(\d+)\.*(.*)$", re.MULTILINE)
     data = lvl2_title_rx.sub(r'\t<a name="section-\1.\2"><h3>\1.\2 \3</h3></a>', data)
@@ -133,10 +153,12 @@ def render_rfc(filename):
         data = fd.read()
 
     out = data
+    opts = dict(filename=filename)
+
     for fn in [remove_top_space, cleanup_author_header, remove_page_breaks,
                anchor_titles, create_paragraphs,
                add_line_breaks_legends, cleanup_toc, create_diagram_blocks]:
-        out = fn(out)
+        out = fn(out, **opts)
 
     env = Environment(loader=FileSystemLoader('templates'))
     template = env.get_template('rfc.html')
@@ -149,8 +171,15 @@ def render_rfc(filename):
 
 
 def main():
-    print render_rfc(sys.argv[1])
+    infile = sys.argv[1]
+    outfile = sys.argv[2]
+
+    with open(outfile, 'w+') as fd:
+        fd.write(render_rfc(infile))
 
+    json_filename = outfile.split('.')[0] + '.json'
+    with open(json_filename, 'w+') as fd:
+        json.dump(metadata, fd)
 
 if __name__ == '__main__':
     main()