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 2 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
11 changes: 10 additions & 1 deletion Doc/tools/extensions/pydoc_topics.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
"""Support for building "topic help" for pydoc."""

from __future__ import annotations

from time import asctime
from typing import TYPE_CHECKING

from sphinx.builders.text import TextBuilder
from sphinx.util import logging
from sphinx.util.display import status_iterator
from sphinx.util.docutils import new_document
from sphinx.writers.text import TextTranslator
from docutils import nodes

if TYPE_CHECKING:

Check failure on line 15 in Doc/tools/extensions/pydoc_topics.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (I001)

Doc/tools/extensions/pydoc_topics.py:3:1: I001 Import block is un-sorted or un-formatted
from collections.abc import Sequence, Set

from sphinx.application import Sphinx
Expand Down Expand Up @@ -102,6 +103,14 @@
"yield",
})

class PydocTextTranslator(TextTranslator):

Check failure on line 106 in Doc/tools/extensions/pydoc_topics.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (E302)

Doc/tools/extensions/pydoc_topics.py:106:1: E302 Expected 2 blank lines, found 1
def visit_grammar_snippet(self, node: Element) -> None:

Check failure on line 107 in Doc/tools/extensions/pydoc_topics.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (F821)

Doc/tools/extensions/pydoc_topics.py:107:43: F821 Undefined name `Element`
"""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 +150,7 @@
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