From 9a5e6016146bcc87968ce8d7746bd22295dab231 Mon Sep 17 00:00:00 2001 From: Karim Hamidou Date: Sat, 19 Mar 2016 18:29:05 -0700 Subject: [PATCH] turn rfc references to links --- app.py | 2 +- src/render_rfc.py | 25 +++++++++++++++++++++---- templates/index.html | 2 +- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 76cedb96..2e5826cf 100644 --- a/app.py +++ b/app.py @@ -31,7 +31,7 @@ def home(): @app.route('/rfc.html') def render_text_rfc(rfc_number): filename = 'text/rfc{}.txt'.format(rfc_number) - rendered = render_html_rfc(filename) + rendered = render_html_rfc(filename, metadata) return render_template('rfc.html', **rendered) @app.route('/browse') diff --git a/src/render_rfc.py b/src/render_rfc.py index 6378f4b8..a18d3b60 100644 --- a/src/render_rfc.py +++ b/src/render_rfc.py @@ -86,7 +86,6 @@ def create_paragraphs(data, **kwargs): return """

{}

""".format(paragraph) - data = paragraph_regexp.sub(format_match, data) return data @@ -138,16 +137,34 @@ def anchor_titles(data, **kwargs): return data -def render_html_rfc(filename): +def replace_rfc_by_link(data, **kwargs): + rfc_metadata = kwargs['rfcs'] + + def format_match(match): + rfc_num = match.group(1) + title = "" + if rfc_num in rfc_metadata: + title = rfc_metadata[rfc_num].get('subject', '') + + return """RFC {num}""".format(num=rfc_num, + subject=title) + + rfc_regexp = re.compile(r'RFC\s*(\d+)', re.MULTILINE | re.IGNORECASE) + data = rfc_regexp.sub(format_match, data) + return data + + +def render_html_rfc(filename, rfc_metadata): with open(filename) as fd: data = fd.read() out = data - opts = dict(filename=filename) + opts = dict(filename=filename, rfcs=rfc_metadata) 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]: + add_line_breaks_legends, cleanup_toc, create_diagram_blocks, + replace_rfc_by_link]: out = fn(out, **opts) dct = dict(rfc=out) diff --git a/templates/index.html b/templates/index.html index 14739b4e..5ceb3967 100644 --- a/templates/index.html +++ b/templates/index.html @@ -41,7 +41,7 @@
Who is behind this?
-

Karim Hamidou. I'm a backend engineer and have to spend a lot of time reading RFCs. Unfortunately, most of them are really ugly and hard to understand without proper context.

+

Hi, I'm Karim! I'm a backend engineer and have to spend a lot of time reading RFCs. Unfortunately, most of them are really ugly and hard to understand without proper context.

Special thanks:
-- 2.43.0