Skip to content

gh-127833: Fix grammar snippet formatting for help() output #129692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion Doc/tools/extensions/grammar_snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def __init__(
self['classes'].append('sx')


class grammar_snippet(nodes.literal_block): # noqa: N801 (snake_case)
"""Node for a grammar snippet"""

grammar_snippet_content: Sequence[str]


class GrammarSnippetBase(SphinxDirective):
"""Common functionality for GrammarSnippetDirective & CompatProductionList."""

Expand All @@ -58,11 +64,14 @@ def make_grammar_snippet(
# To get around this, we set it to this non-empty string:
rawsource = 'You should not see this.'

literal = nodes.literal_block(
literal = grammar_snippet(
rawsource,
'',
classes=['highlight'],
)
# Save a copy of the "input" content. For plain text, we want to
# output this verbatim.
literal.grammar_snippet_content = list(content)

grammar_re = re.compile(
r"""
Expand Down
12 changes: 11 additions & 1 deletion Doc/tools/extensions/pydoc_topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from time import asctime
from typing import TYPE_CHECKING

from docutils import nodes
from sphinx.builders.text import TextBuilder
from sphinx.util import logging
from sphinx.util.display import status_iterator
Expand Down Expand Up @@ -103,6 +104,15 @@
})


class PydocTextTranslator(TextTranslator):
def visit_grammar_snippet(self, node: nodes.Element) -> None:
"""For grammar snippets, return the "input" as is."""
self.new_state()
self.add_text(self.nl.join(node.grammar_snippet_content))
self.end_state(wrap=False)
raise nodes.SkipNode


class PydocTopicsBuilder(TextBuilder):
name = "pydoc-topics"

Expand Down Expand Up @@ -141,7 +151,7 @@ def write_documents(self, _docnames: Set[str]) -> None:
for topic_label, label_id in label_ids:
document = new_document("<section node>")
document.append(doc_ids[label_id])
visitor = TextTranslator(document, builder=self)
visitor = PydocTextTranslator(document, builder=self)
document.walkabout(visitor)
self.topics[topic_label] = visitor.body

Expand Down
Loading
Loading