From: Karim Hamidou Date: Mon, 7 Mar 2016 04:30:20 +0000 (-0800) Subject: better table of contents X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=911d6571cf25e36ef9f16f430199e25ceb303faa;p=rfcspot.git better table of contents --- diff --git a/rfc.css b/rfc.css index 4e165446..134dae92 100644 --- a/rfc.css +++ b/rfc.css @@ -119,6 +119,14 @@ table td { } +.indent-1 { + margin-left: 4em; +} + +.indent-2 { + margin-left: 8em; +} + .homepage { display: flex; align-items: center; diff --git a/src/render_rfc.py b/src/render_rfc.py index cb7fb1eb..2087bb64 100644 --- a/src/render_rfc.py +++ b/src/render_rfc.py @@ -8,9 +8,34 @@ from bs4 import BeautifulSoup, Tag, Comment from jinja2 import Environment, FileSystemLoader +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): + header = match.group(1).replace('\n', '
') + title = match.group(2) + status = match.group(3) + + 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) + + abstract_regex = re.compile('(^Abstract)', re.MULTILINE) + data = abstract_regex.sub(r'

\1

', data) + + return data + + def cleanup_toc(data): # Clean up the table of contents - toc_regexp = re.compile(r'^\s+(.+?)\s+(.+?)(\.+)\s+\d+$', re.MULTILINE) + # 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) @@ -19,7 +44,15 @@ def cleanup_toc(data): if anchor[-1] == '.': anchor = anchor[:-1] - return '{} {}
'.format(anchor, section_name, title) + indent = '' + # Indent parts depending on their section number. + if anchor.count('.') == 2: + indent = 'indent-1' + elif anchor.count('.') == 3: + indent = 'indent-2' + + return '{} {}
'.format(anchor, indent, + section_name, title) data = toc_regexp.sub(format_match, data) @@ -52,9 +85,13 @@ def create_paragraphs(data): def create_diagram_blocks(data): - # A diagram always starts by +-- and ends by ---+. + # 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) + + # Some diagrams aren't boxed but contain a lot of ---- + #diagram_regexp = re.compile(r"(^(.+?)-{4,}(.+?)$)", re.MULTILINE) + #data = diagram_regexp.sub(r'
\1\n
\n', data) return data @@ -92,9 +129,9 @@ def render_rfc(filename): data = fd.read() out = data - for fn in [remove_top_space, remove_page_breaks, - anchor_titles, create_paragraphs, create_diagram_blocks, - add_line_breaks_legends, cleanup_toc]: + 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) env = Environment(loader=FileSystemLoader('templates'))