-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Infra: Add a Sphinx extension to highlight custom keywords #5084
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
Open
ZeroIntensity
wants to merge
1
commit into
python:main
Choose a base branch
from
ZeroIntensity:highlighting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+101
−11
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
pep_sphinx_extensions/pep_processor/parsing/pep_soft_keyword.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from docutils import nodes | ||
| from pygments.lexers.python import PythonLexer | ||
| from pygments.token import Keyword, Name | ||
| from sphinx import addnodes | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterator | ||
|
|
||
| from sphinx.application import Sphinx | ||
| from sphinx.environment import BuildEnvironment | ||
|
|
||
| _LANG_PREFIX = "python+soft-keywords:" | ||
|
|
||
|
|
||
| def _keywords_from_language(language: str) -> tuple[str, ...] | None: | ||
| if not language.startswith(_LANG_PREFIX): | ||
| return None | ||
| keywords = tuple(w for w in language[len(_LANG_PREFIX) :].split(",") if w) | ||
| return keywords or None | ||
|
|
||
|
|
||
| def _soft_keyword_lexer(keywords: tuple[str, ...]) -> type[PythonLexer]: | ||
| words = frozenset(keywords) | ||
|
|
||
| class SoftKeywordPythonLexer(PythonLexer): | ||
| name = f"Python (+ {', '.join(keywords)})" | ||
| aliases: list[str] = [] | ||
| filenames: list[str] = [] # don't shadow the real Python lexer | ||
| mimetypes: list[str] = [] | ||
| url = "" | ||
|
|
||
| def get_tokens_unprocessed( | ||
| self, text: str, stack: tuple[str, ...] = ("root",) | ||
| ) -> Iterator[tuple[int, object, str]]: | ||
| for index, token, value in super().get_tokens_unprocessed(text, stack): | ||
| if value in words and token in Name: | ||
| yield index, Keyword, value | ||
| else: | ||
| yield index, token, value | ||
|
|
||
| return SoftKeywordPythonLexer | ||
|
|
||
|
|
||
| def _init_env(app: Sphinx, env: BuildEnvironment, docnames: list[str]) -> None: | ||
| if not hasattr(env, "pep_soft_keywords"): | ||
| env.pep_soft_keywords = {} | ||
|
|
||
|
|
||
| def _collect_languages(app: Sphinx, doctree: nodes.document) -> None: | ||
| found = set() | ||
| for node in doctree.findall(nodes.literal_block): | ||
| if keywords := _keywords_from_language(node.get("language", "")): | ||
| found.add(keywords) | ||
| for node in doctree.findall(addnodes.highlightlang): | ||
| if keywords := _keywords_from_language(node.get("lang", "")): | ||
| found.add(keywords) | ||
|
|
||
| if found: | ||
| app.env.pep_soft_keywords[app.env.docname] = found | ||
| else: | ||
| app.env.pep_soft_keywords.pop(app.env.docname, None) | ||
|
|
||
|
|
||
| def _merge_info( | ||
| app: Sphinx, | ||
| env: BuildEnvironment, | ||
| docnames: list[str], | ||
| other: BuildEnvironment, | ||
| ) -> None: | ||
| env.pep_soft_keywords.update(getattr(other, "pep_soft_keywords", {})) | ||
|
|
||
|
|
||
| def _register_lexers(app: Sphinx, env: BuildEnvironment) -> None: | ||
| for keyword_sets in env.pep_soft_keywords.values(): | ||
| for keywords in keyword_sets: | ||
| app.add_lexer(_LANG_PREFIX + ",".join(keywords), _soft_keyword_lexer(keywords)) | ||
|
|
||
|
|
||
| def setup(app: Sphinx) -> dict[str, bool]: | ||
| app.connect("env-before-read-docs", _init_env) | ||
| app.connect("doctree-read", _collect_languages) | ||
| app.connect("env-merge-info", _merge_info) | ||
| app.connect("env-updated", _register_lexers) | ||
| return {"parallel_read_safe": True, "parallel_write_safe": True} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why “soft”? Aren't they always highlighted as keywords?