Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use a triple single quote representation
  • Loading branch information
AA-Turner committed Jan 21, 2025
commit 1d278ae35abbb2b0d7bd559bc1027858a6b5bff7
17 changes: 15 additions & 2 deletions Doc/tools/extensions/pydoc_topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

from pprint import pformat
from time import asctime
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -147,11 +146,17 @@ def write_documents(self, _docnames: Set[str]) -> None:
self.topics[topic_label] = visitor.body

def finish(self) -> None:
topics_repr = "\n".join(
f" '{topic}': {_repr(self.topics[topic])},"
for topic in sorted(self.topics)
)
topics = f"""\
# Autogenerated by Sphinx on {asctime()}
# as part of the release process.

topics = {pformat(dict(sorted(self.topics.items())))}
topics = {{
{topics_repr}
}}
"""
self.outdir.joinpath("topics.py").write_text(topics, encoding="utf-8")

Expand All @@ -164,6 +169,14 @@ def _display_labels(item: tuple[str, Sequence[tuple[str, str]]]) -> str:
return ", ".join(labels)


def _repr(text: str, /) -> str:
"""Return a triple-single-quoted representation of text."""
if "'''" not in text:
return f"r'''{text}'''"
text = text.replace("\\", "\\\\").replace("'''", r"\'\'\'")
return f"'''{text}'''"


def setup(app: Sphinx) -> ExtensionMetadata:
app.add_builder(PydocTopicsBuilder)

Expand Down