Skip to content

Commit 750b08d

Browse files
committed
Add custom lexers for PEP 823 + 824
1 parent 94a7775 commit 750b08d

4 files changed

Lines changed: 85 additions & 0 deletions

File tree

‎pep_sphinx_extensions/__init__.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
get_from_doctree,
1515
pep_abstract,
1616
)
17+
from pep_sphinx_extensions.lexers import pep_lexers
1718
from pep_sphinx_extensions.pep_processor.html import (
1819
pep_html_builder,
1920
pep_html_translator,
@@ -105,6 +106,10 @@ def setup(app: Sphinx) -> dict[str, bool]:
105106
app.add_directive("superseded", pep_banner_directive.SupersededBanner)
106107
app.add_directive("withdrawn", pep_banner_directive.WithdrawnBanner)
107108

109+
# Register custom lexers
110+
for lexer in pep_lexers:
111+
app.add_lexer(lexer.name, lexer)
112+
108113
# Register event callbacks
109114
app.connect("builder-inited", _update_config_for_builder) # Update configuration values for builder used
110115
app.connect("env-before-read-docs", create_pep_zero) # PEP 0 hook
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This file is placed in the public domain or under the
2+
# CC0-1.0-Universal license, whichever is more permissive.
3+
4+
from __future__ import annotations
5+
6+
from typing import TYPE_CHECKING
7+
8+
from .pep823_lexer import Py823ConsoleLexer, Py823Lexer
9+
from .pep824_lexer import Py824Lexer
10+
11+
if TYPE_CHECKING:
12+
from pygments.lexer import Lexer
13+
14+
15+
pep_lexers: list[type[Lexer]] = [
16+
Py823ConsoleLexer,
17+
Py823Lexer,
18+
Py824Lexer,
19+
]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This file is placed in the public domain or under the
2+
# CC0-1.0-Universal license, whichever is more permissive.
3+
4+
"""Custom lexer for PEP 823."""
5+
6+
from pygments.lexer import DelegatingLexer, inherit
7+
from pygments.lexers.python import (
8+
_PythonConsoleLexerBase,
9+
PythonLexer,
10+
PythonTracebackLexer,
11+
)
12+
from pygments.token import Operator, Other
13+
14+
15+
class Py823Lexer(PythonLexer):
16+
name = "py823"
17+
18+
tokens = {
19+
"expr": [
20+
(r"maybe\b", Operator.Word),
21+
(r"\?", Operator),
22+
inherit,
23+
],
24+
}
25+
26+
27+
class Py823ConsoleLexer(DelegatingLexer):
28+
name = "py823-console"
29+
30+
def __init__(self, **options):
31+
pylexer = Py823Lexer
32+
tblexer = PythonTracebackLexer
33+
34+
class _ReplaceInnerCode(DelegatingLexer):
35+
def __init__(self, **options):
36+
super().__init__(
37+
pylexer, _PythonConsoleLexerBase, Other.Code, **options
38+
)
39+
40+
super().__init__(tblexer, _ReplaceInnerCode, Other.Traceback, **options)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This file is placed in the public domain or under the
2+
# CC0-1.0-Universal license, whichever is more permissive.
3+
4+
"""Custom lexer for PEP 824."""
5+
6+
from pygments.lexer import inherit
7+
from pygments.lexers.python import PythonLexer
8+
from pygments.token import Operator
9+
10+
11+
class Py824Lexer(PythonLexer):
12+
name = "py824"
13+
14+
tokens = {
15+
"expr": [
16+
(r"\?\?(?=\s)", Operator.Word),
17+
(r"otherwise\b", Operator.Word),
18+
(r"\?", Operator),
19+
inherit,
20+
],
21+
}

0 commit comments

Comments
 (0)