From b5e92523daaa46ce1b5cbcb3168a8ea38422351a Mon Sep 17 00:00:00 2001 From: Karim Hamidou Date: Wed, 9 Mar 2016 20:09:12 -0800 Subject: [PATCH] generate a metadata file --- src/render_rfc.py | 61 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/src/render_rfc.py b/src/render_rfc.py index 93ff5f32..d5e67f91 100644 --- a/src/render_rfc.py +++ b/src/render_rfc.py @@ -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 """

{}

{}

{}

""".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'

\1

', 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 '{} {}
'.format(anchor, indent, - section_name, title) + return """{} {}
""".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'
\1\n
\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
to make the code more readable. @@ -107,17 +120,24 @@ def add_line_breaks_legends(data): data = legends_linebreak.sub(r'\1
\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

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

', data) + lvl4_template = r"""

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

+ raw + """.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

\1.\2.\3 \4

', data) + lvl3_template = r"""\t

\1.\2.\3 \4

""" + 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

\1.\2 \3

', 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() -- 2.43.0