|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +from docutils import nodes |
| 6 | +import sphinx.writers.html5 as html5 |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from sphinx.builders import html |
| 10 | + |
| 11 | + |
| 12 | +class PEPTranslator(html5.HTML5Translator): |
| 13 | + """Custom RST -> HTML translation rules for PEPs.""" |
| 14 | + |
| 15 | + def __init__(self, document: nodes.document, builder: html.StandaloneHTMLBuilder): |
| 16 | + super().__init__(document, builder) |
| 17 | + self.compact_simple: bool = False |
| 18 | + |
| 19 | + @staticmethod |
| 20 | + def should_be_compact_paragraph(node: nodes.paragraph) -> bool: |
| 21 | + """Check if paragraph should be compact. |
| 22 | +
|
| 23 | + Omitting <p/> tags around paragraph nodes gives visually compact lists. |
| 24 | +
|
| 25 | + """ |
| 26 | + # Never compact paragraphs that are children of document or compound. |
| 27 | + if isinstance(node.parent, (nodes.document, nodes.compound)): |
| 28 | + return False |
| 29 | + |
| 30 | + # Check for custom attributes in paragraph. |
| 31 | + for key, value in node.non_default_attributes().items(): |
| 32 | + # if key equals "classes", carry on |
| 33 | + # if value is empty, or contains only "first", only "last", or both |
| 34 | + # "first" and "last", carry on |
| 35 | + # else return False |
| 36 | + if any((key != "classes", not set(value) <= {"first", "last"})): |
| 37 | + return False |
| 38 | + |
| 39 | + # Only first paragraph can be compact (ignoring initial label & invisible nodes) |
| 40 | + first = isinstance(node.parent[0], nodes.label) |
| 41 | + visible_siblings = [child for child in node.parent.children[first:] if not isinstance(child, nodes.Invisible)] |
| 42 | + if visible_siblings[0] is not node: |
| 43 | + return False |
| 44 | + |
| 45 | + # otherwise, the paragraph should be compact |
| 46 | + return True |
| 47 | + |
| 48 | + def visit_paragraph(self, node: nodes.paragraph) -> None: |
| 49 | + """Remove <p> tags if possible.""" |
| 50 | + if self.should_be_compact_paragraph(node): |
| 51 | + self.context.append("") |
| 52 | + else: |
| 53 | + self.body.append(self.starttag(node, "p", "")) |
| 54 | + self.context.append("</p>\n") |
| 55 | + |
| 56 | + def depart_paragraph(self, _: nodes.paragraph) -> None: |
| 57 | + """Add corresponding end tag from `visit_paragraph`.""" |
| 58 | + self.body.append(self.context.pop()) |
| 59 | + |
| 60 | + def depart_label(self, node) -> None: |
| 61 | + """PEP link/citation block cleanup with italicised backlinks.""" |
| 62 | + if not self.settings.footnote_backlinks: |
| 63 | + self.body.append("</span>") |
| 64 | + self.body.append("</dt>\n<dd>") |
| 65 | + return |
| 66 | + |
| 67 | + # If only one reference to this footnote |
| 68 | + back_references = node.parent["backrefs"] |
| 69 | + if len(back_references) == 1: |
| 70 | + self.body.append("</a>") |
| 71 | + |
| 72 | + # Close the tag |
| 73 | + self.body.append("</span>") |
| 74 | + |
| 75 | + # If more than one reference |
| 76 | + if len(back_references) > 1: |
| 77 | + back_links = [f"<a href='#{ref}'>{i}</a>" for i, ref in enumerate(back_references, start=1)] |
| 78 | + back_links_str = ", ".join(back_links) |
| 79 | + self.body.append(f"<span class='fn-backref''><em> ({back_links_str}) </em></span>") |
| 80 | + |
| 81 | + # Close the def tags |
| 82 | + self.body.append("</dt>\n<dd>") |
| 83 | + |
| 84 | + def unknown_visit(self, node: nodes.Node) -> None: |
| 85 | + """No processing for unknown node types.""" |
| 86 | + pass |
0 commit comments