diff options
author | 2018-05-18 16:00:31 +0100 | |
---|---|---|
committer | 2018-05-18 16:00:31 +0100 | |
commit | e1846928439aa2a7e660d870a083872c415c274d (patch) | |
tree | e716f3466ca3914f80b2ca102d5d345658af7bc8 /pysite/rst | |
parent | Update wiki footer in line with main site (diff) |
[Jams] Huge amount of work on code jam admin area
Diffstat (limited to 'pysite/rst')
-rw-r--r-- | pysite/rst/__init__.py | 99 |
1 files changed, 51 insertions, 48 deletions
diff --git a/pysite/rst/__init__.py b/pysite/rst/__init__.py index 97a77f40..744dd269 100644 --- a/pysite/rst/__init__.py +++ b/pysite/rst/__init__.py @@ -13,8 +13,10 @@ CONTENTS_REGEX = re.compile(r"""<div class=\"contents topic\" id=\"contents\">(. HREF_REGEX = re.compile(r"""<a class=\"reference internal\" href=\"(.*?)\".*?>(.*?)</a>""") -def render(rst: str): - rst = RST_TEMPLATE.format(rst) +def render(rst: str, link_headers=True): + if link_headers: + rst = RST_TEMPLATE.format(rst) + html = publish_parts( source=rst, writer_name="html5", settings_overrides={ "halt_level": 2, "syntax_highlight": "short", "initial_header_level": 3 @@ -26,68 +28,69 @@ def render(rst: str): "headers": [] } - match = CONTENTS_REGEX.search(html) # Find the contents HTML + if link_headers: + match = CONTENTS_REGEX.search(html) # Find the contents HTML - if match: - data["html"] = html.replace(match.group(0), "") # Remove the contents from the document HTML + if match: + data["html"] = html.replace(match.group(0), "") # Remove the contents from the document HTML - depth = 0 - headers = [] - current_header = {} + depth = 0 + headers = [] + current_header = {} - group = match.group(1) + group = match.group(1) - # Sanitize the output so we can more easily parse it - group = group.replace("<li>", "<li>\n") - group = group.replace("</li>", "\n</li>") - group = group.replace("<p>", "<p>\n") - group = group.replace("</p>", "\n</p>") + # Sanitize the output so we can more easily parse it + group = group.replace("<li>", "<li>\n") + group = group.replace("</li>", "\n</li>") + group = group.replace("<p>", "<p>\n") + group = group.replace("</p>", "\n</p>") - for line in group.split("\n"): - line = line.strip() # Remove excess whitespace + for line in group.split("\n"): + line = line.strip() # Remove excess whitespace - if not line: # Nothing to process - continue + if not line: # Nothing to process + continue - if line.startswith("<li>") and depth <= 2: - # We've found a header, or the start of a header group - depth += 1 - elif line.startswith("</li>") and depth >= 0: - # That's the end of a header or header group + if line.startswith("<li>") and depth <= 2: + # We've found a header, or the start of a header group + depth += 1 + elif line.startswith("</li>") and depth >= 0: + # That's the end of a header or header group - if depth == 1: - # We just dealt with an entire header group, so store it - headers.append(current_header.copy()) # Store a copy, since we're clearing the dict - current_header.clear() + if depth == 1: + # We just dealt with an entire header group, so store it + headers.append(current_header.copy()) # Store a copy, since we're clearing the dict + current_header.clear() - depth -= 1 - elif line.startswith("<a") and depth <= 2: - # We've found an actual URL - match = HREF_REGEX.match(line) # Parse the line for the ID and header title + depth -= 1 + elif line.startswith("<a") and depth <= 2: + # We've found an actual URL + match = HREF_REGEX.match(line) # Parse the line for the ID and header title - if depth == 1: # Top-level header, so just store it in the current header - current_header["id"] = match.group(1) + if depth == 1: # Top-level header, so just store it in the current header + current_header["id"] = match.group(1) - title = match.group(2) + title = match.group(2) - if title.startswith("<i"): # We've found an icon, which needs to have a space after it - title = title.replace("</i> ", "</i> ") + if title.startswith("<i"): # We've found an icon, which needs to have a space after it + title = title.replace("</i> ", "</i> ") - current_header["title"] = title - else: # Second-level (or deeper) header, should be stored in a list of sub-headers under the current - sub_headers = current_header.get("sub_headers", []) - title = match.group(2) + current_header["title"] = title + else: # Second-level (or deeper) header, should be stored in a list of sub-headers + sub_headers = current_header.get("sub_headers", []) + title = match.group(2) - if title.startswith("<i"): # We've found an icon, which needs to have a space after it - title = title.replace("</i> ", "</i> ") + if title.startswith("<i"): # We've found an icon, which needs to have a space after it + title = title.replace("</i> ", "</i> ") - sub_headers.append({ - "id": match.group(1), - "title": title - }) - current_header["sub_headers"] = sub_headers + sub_headers.append({ + "id": match.group(1), + "title": title + }) + current_header["sub_headers"] = sub_headers - data["headers"] = headers + data["headers"] = headers return data |