From: Karim Hamidou Date: Sun, 6 Mar 2016 03:00:56 +0000 (-0800) Subject: better parsing X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=0f54a6dccc2a0ab890a8db2a7df9f59eb7425d0e;p=rfcspot.git better parsing --- diff --git a/rfc.css b/rfc.css index 0b9087cb..4e165446 100644 --- a/rfc.css +++ b/rfc.css @@ -82,7 +82,6 @@ table td { .docs pre { font-size: 16px; line-height: 24px; - font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif; } .docs p tt, .docs p code { background: #f8f8ff; diff --git a/src/render_rfc.py b/src/render_rfc.py index 54310d20..1cd20761 100644 --- a/src/render_rfc.py +++ b/src/render_rfc.py @@ -4,6 +4,7 @@ # 2. add links to the table of contents import re import sys +from bs4 import BeautifulSoup, Tag, Comment from jinja2 import Environment, FileSystemLoader @@ -20,10 +21,18 @@ def remove_page_breaks(data): def create_paragraphs(data): - paragraph_regexp = re.compile(r"^((\w|\"|\').+(\.|\:))\n\n", re.DOTALL) - data = paragraph_regexp.sub(r'

\1

', data) + paragraph_regexp = re.compile(r"(^.+?(\.|\:|;))\n\n", re.MULTILINE | re.DOTALL) + + # replace the \n inside those paragraphs by
tags. + data = paragraph_regexp.sub(r'

\1

', data) + return data +def create_diagram_blocks(data): + # A diagram always 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) + return data def anchor_titles(data): # Reverse section order because lvl1_title_rx matches the beginning of lvl2_title_rx @@ -46,7 +55,7 @@ def render_rfc(filename): out = data for fn in [remove_top_space, remove_page_breaks, - anchor_titles, create_paragraphs]: + anchor_titles, create_paragraphs, create_diagram_blocks]: out = fn(out) env = Environment(loader=FileSystemLoader('templates')) @@ -54,6 +63,18 @@ def render_rfc(filename): dct = dict(rfc=out) rendered = template.render(**dct) + + # A little post-processing. + soup = BeautifulSoup(rendered, 'html.parser') + for match in soup.findAll('p', class_='rfcparagraph'): + try: + match.contents = [s.replace('\n', '
') + for s in match.contents if s is not None] + except: + continue + + rendered = soup.prettify() + return rendered # return out